2013-05-09 49 views
1

我试图在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>&nbsp; <%= 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>

+1

感谢您的回复。我改变了路径,我仍然得到相同的错误。我刚刚在上面发布了rake路由的相关部分(输出非常大,所以我只添加了我认为需要看到的部分)。 – winston 2013-05-09 01:39:48

+0

对于额外的帮助,我无法表达谢意。它发生在第48行左右的视图页面上,但我开始认为问题在于工作控制器(评论属于工作;在博客上评论)。我用工作控制器更新了问题 – winston 2013-05-09 02:19:03

+0

不可以。您的'link_to'是错误的。我更新了我的答案。 – Mischa 2013-05-09 02:19:27

回答

1

你应该使用:

toggle_is_contribution_comment_path(comment) 

只要坚持_path什么您在rake routes的输出的第一列中看到。

此外,您的link_to是错误的。相反的:

<%= link_to image_tag("/assets/list_star.png"), comment, toggle_is_contribution_comment_path(comment), etc. 

做:

<%= link_to image_tag("/assets/list_star.png"), toggle_is_contribution_comment_path(comment), etc. 

注意,第二个参数是link_to的URL。所以你在这里不需要comment。作为第二个参数传递toggle_is_contribution_comment_path(comment)就足够了。

+0

感谢您的回答!我认为这应该工作,但现在我得到一个stringify键错误:未定义的方法'stringify_keys'为“/ comments/1/toggle_is_contribution”:字符串 – winston 2013-05-09 02:00:46

+0

我改变了link_to,现在我不再得到stringify键错误。我得到了未定义的方法'toggle_is_contribution_comments_path'for#<#:0x00000004438ba0> – winston 2013-05-09 02:22:16

+0

@bruce哦,抱歉..显然应该是'toggle_is_contribution_comment_path' ...我抄录了您的问题中的代码。再次更新我的答案。 – Mischa 2013-05-09 02:24:05

相关问题