【问题标题】:updating database record with click event使用点击事件更新数据库记录
【发布时间】:2013-02-13 07:13:18
【问题描述】:

您好,我正在尝试通过单击事件远程更新数据库中的元素。 当点击事件被触发时,我收到以下错误,但我相信我使用了正确的方法

Started PUT "/availabilities/131/edit" for 124.168.74.144 at 2013-02-13 08:02:40 +0100

ActionController::RoutingError (No route matches [PUT] "/availabilities/131/edit"):

haml 点击事件

.six.columns#named_players


 %h5 Named Players
  %table.twelve
    %thead
      %tr
        %th Change Status
        %th Attending
    %tbody
      - @availabilities.each do |a|
        %tr
          %td= full_name(a.user)
          %td
            %ul.button-group.two-up.even
              %li= button_to 'Accept', edit_availability_path(a), :remote => true, :method => :put, :class => 'button tiny success', :disable_with => 'Add'
              %li= link_to 'Decline', '#', :remote => true, :method => :put, :class => 'button tiny alert'

控制器

def update
    @availability = Availability.find(params[:id])

    respond_to do |format|
      if @availability.update_attributes(params[:availability])
        format.html { redirect_to @availability, :notice => 'Availability was successfully updated.' }
        format.js
      else
        format.html { render :action => "edit" }
        format.js
      end
    end
  end

【问题讨论】:

    标签: ruby-on-rails jquery rails-activerecord ujs


    【解决方案1】:

    您点击的是[PUT] "/availabilities/131/edit" 而不是[PUT] "/availabilities/131/update"

    【讨论】:

    • 所以我需要改变路线?
    • 你必须添加更新路由(如果它还不存在)并且你的按钮(或链接)必须调用更新控制器方法
    • 更新已经在我的控制器中,如上所述,所以需要更改链接?
    • 试试link_to 'Decline', controller: 'controller_name', action: 'update', :remote => true, :method => :put, :class => 'button tiny alert'
    • 不确定它如何传递需要更新的参数
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多