Link_to在嵌套资源中编辑

时间:2012-08-20 17:59:29

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

我对嵌套资源应该如何工作以及如何访问它们感到困惑 这是我的模型

event                      eventcomments
id                           id
Title                        body
Content                      event_id
...

这是我的路线文件

resources :events do
  resources :eventcomments
  end
end

这里的关系

Article
  has_many :eventcomments
Comments
  belongs_to event

但是当我在show.html.erb的活动中时,我无法获得编辑评论的链接。这里生产的佣金路线

edit_event_eventcomment GET    /events/:event_id/eventcomments/:id/edit(.:format) eventcomments#edit

和我的链接

<h2>Comments</h2>
<div>
<% @comments.each do |comment| %>
    <div>
      <%= image_tag (comment.customer.avatar).url(:thumb) %>
      <%= comment.customer.incomplete_name %> said: 
      <%= comment.description %>
      <div>Posted: <%= time_ago_in_words(comment.created_at) %></div>
      <% if current_customer.isadmin? %>
        <%= link_to 'Edit', edit_event_eventcomment_path(@event) %>
        <%= link_to 'Destroy', '#' %>
      <% end %>
    </div><br />

这是我得到的错误

NoMethodError in Eventcomments#edit

Showing /home/jean/rail/voyxe/app/views/eventcomments/_form.html.erb where line #1 raised:

undefined method `eventcomment_path' for #<#<Class:0xb5e73d84>:0xb5e7c8f8>
Extracted source (around line #1):

1: <%= form_for(@eventcomment) do |f| %>
2:   <% if @eventcomment.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@eventcomment.errors.count, "error") %> prohibited this eventcomment from being saved:</h2>

2 个答案:

答案 0 :(得分:1)

您只是将Event实例传递给edit_event_eventcomment_path方法,并且您也应该传递Eventcomment实例。

尝试使用edit_event_eventcomment_path(@event, comment)

注意:将您的Eventcomment课程重命名为EventCommentComment

答案 1 :(得分:1)

您的嵌套路由需要两个参数:event_id和:id,如下所示:

edit_event_eventcomment_path(@event, comment)
相关问题