简体   繁体   English

Link_to在嵌套资源中编辑

[英]Link_to edit in nested resources

I am confused on how nested resources should works and how can i access them Here my model in question 我对嵌套资源应如何工作以及如何访问它感到困惑。在这里我的模型有问题

event                      eventcomments
id                           id
Title                        body
Content                      event_id
...

Here my route file 这是我的路线档案

resources :events do
  resources :eventcomments
  end
end

Here the relationship 在这里的关系

Article
  has_many :eventcomments
Comments
  belongs_to event

But when i am in show.html.erb of events, I can't have the link to edit the comment. 但是当我在show.html.erb的事件中,我没有链接来编辑评论。 here the rake route produced 这里产生了耙路线

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

and my link to 和我的链接

<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 />

Here the error i am getting 这是我得到的错误

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>

You are only passing an Event instance to the edit_event_eventcomment_path method and you should be passing the Eventcomment instance also. 您只是将一个Event实例传递给edit_event_eventcomment_path方法,您也应该传递Eventcomment实例。

Try with edit_event_eventcomment_path(@event, comment) 尝试使用edit_event_eventcomment_path(@event, comment)

Note: Rename your Eventcomment class to EventComment or just Comment . 注意:将Eventcomment类重命名为EventComment或仅Comment

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

edit_event_eventcomment_path(@event, comment)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM