Jseb
Jseb

Reputation: 1934

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

Upvotes: 0

Views: 1683

Answers (2)

Thilo
Thilo

Reputation: 17735

Your nested routes requires two parameters, :event_id and :id, like so:

edit_event_eventcomment_path(@event, comment)

Upvotes: 1

carpamon
carpamon

Reputation: 6623

You are only passing an Event instance to the edit_event_eventcomment_path method and you should be passing the Eventcomment instance also.

Try with edit_event_eventcomment_path(@event, comment)

Note: Rename your Eventcomment class to EventComment or just Comment.

Upvotes: 1

Related Questions