我试图在Stackoverflow上发布一个答案,以便在单击图像时更新我应用数据库中的布尔值(Rails - How to update a single attribute in controller)。在Rails中更新布尔值时未定义的方法
不过,我得到以下错误,当我打开包含图像的页面:
未定义的方法`toggle_is_contribution_comments_path”为#<#:0x000000053d8150>
我的路由文件:
resources :comments do
member do
put :toggle_is_contribution
end
end
控制器:
def toggle_is_contribution
@comment = Comment.find(params[:work_id])
@comment.toggle!(:is_contribution)
respond_to do |format|
flash[:success] = "Work updated"
format.html { redirect_to root_path }
format.js
end
end
查看:
<%= image_tag comment.user.photo.url(:avatar) %></span> <%= link_to comment.user.full_name, comment.user if comment.user %>
<% if current_user == @work.user %>
<span class = "contribution">
<%= link_to image_tag("/assets/list_star.png"), comment, toggle_is_contribution_comments_path(comment),
:size => "15x15", :align => "right", :title=> "Add contribution to your work", :method=> :put %>
</span>
<% end %>
为什么应用程序不识别该方法?我做错了什么?我检查了我的模型,并且attr_accessible包含is_contribution
谢谢! -b
编辑1:耙路线:
toggle_is_contribution_comment PUT /comments/:id/toggle_is_contribution(.:format) comments#toggle_is_contribution
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
编辑2:
的编辑方法名米沙的回答后,下面我得到一个字符串化按键错误:
未定义的方法` stringify_keys'为“/ comments/1/toggle_is_contribution”:字符串
编辑3:
修正了的link_to,但现在我得到这个未定义的错误:
未定义的方法`toggle_is_contribution_comments_path”为#<#:0x00000004438ba0>
感谢您的回复。我改变了路径,我仍然得到相同的错误。我刚刚在上面发布了rake路由的相关部分(输出非常大,所以我只添加了我认为需要看到的部分)。 – winston 2013-05-09 01:39:48
对于额外的帮助,我无法表达谢意。它发生在第48行左右的视图页面上,但我开始认为问题在于工作控制器(评论属于工作;在博客上评论)。我用工作控制器更新了问题 – winston 2013-05-09 02:19:03
不可以。您的'link_to'是错误的。我更新了我的答案。 – Mischa 2013-05-09 02:19:27