删除按钮不起作用 - 不确定错误是什么

时间:2016-11-05 22:10:59

标签: ruby-on-rails

所有

我试过了:

            <h9>Thursday</h9>
            <% @thursday.each do |chorelist| %><br>
                <p2><%= chorelist.name %></p2>
                <%= button_to "delete", chorelists_destroy_path(:id, chorelist.id) %>
            <% end %>
然而,

它不会起作用。

如您所见,我试图在每次迭代时添加一个删除按钮。我在这里缺少什么?

我还是Rails的新手,所以任何帮助都会受到最高的赞赏。

附录:使用后一种方法时收到此错误消息:

“无法找到名为'id'= {:name =&gt;”Sweep Floors“的Chorelist,:day =&gt;”Sunday“,:user_id =&gt; 2}”

我检查了控制台,那些参数都在那里。

  class SaveListController < ApplicationController

  before_filter :authenticate_user!

  def index
  @chorelist = Chorelist.create(user_id: params[:user_id], chore_id:     params[:chore_id], day: params[:day], name: params[:chore_name])

redirect_to pick_chores_path

end


def display
@chorelist = Chorelist.all

@monday = @chorelist.where("day = 'Monday'", user_id: current_user.id)
@tuesday = @chorelist.where("day = 'Tuesday'", user_id: current_user.id)
@wednesday = @chorelist.where("day = 'Wednesday'", user_id: current_user.id)
@thursday = @chorelist.where("day = 'Thursday'", user_id: current_user.id)
@friday = @chorelist.where("day = 'Friday'", user_id: current_user.id)
@saturday = @chorelist.where("day = 'Saturday'", user_id: current_user.id)
@sunday = @chorelist.where("day = 'Sunday'", user_id: current_user.id)

  end

 def destroy
 @chorelist = Chorelist.find(id: params[:id])
 @chorelist.destroy

redirect_to show_chores_path

end

end

Rails.application.routes.draw做

资源:chorelists

获取'约'=&gt; '欢迎#大约'

获取'联系'=&gt; '欢迎#接触'

root'welcome #index'

获取'约'=&gt; '欢迎#大约'

获取'pick_chores'=&gt; 'pick_chores#索引'

发布'save_list'=&gt; 'save_list#索引'

获取'show_chores'=&gt; 'save_list#显示'

发布'chorelists_destroy'=&gt; 'save_list#破坏'

资源:家务   devise_for:users

1 个答案:

答案 0 :(得分:1)

:id, chorelist.id更改为id: chorelist.id

<h9>Thursday</h9>
<% @thursday.each do |chorelist| %><br>
    <p2><%= chorelist.name %></p2>
    <%= button_to "delete", chorelists_destroy_path(id: chorelist.id) %>
<% end %>

在控制器上

def destroy
    @chorelist = Chorelist.find(params[:id])
    @chorelist.destroy
    # do your things
end