Roman Kanafotskiy
Roman Kanafotskiy

Reputation: 25

No route matches {:action=>"edit", :controller=>"comments", :id=>nil, :ribbit_id=>"18"} missing required keys: [:id]

Here's the routes file:

resources :ribbits do
  resources :comments
end

Here's the comments_controller.rb edit action:

def edit
  @ribbit = Ribbit.find(params[:ribbit_id])
  @comment = @ribbit.comments.find(params[:id])
  redirect_to @user unless @user == current_user
end

And that's the view:

<% @ribbit.comments.each do |comment| %>
  <div class="comment">
    <% comment_user = comment.user%>
    <img src="<%= comment_user.avatar_url unless comment_user.blank?%>">
    <%= comment.user.name if comment.user %>
    <p>
      <%= comment.body if comment %>
    </p>
    <%= link_to "Edit", edit_ribbit_comment_path(@ribbit, comment) %>
  </div>
<% end %>

I am getting the error:

No route matches {:action=>"edit", :controller=>"comments", :id=>nil, :ribbit_id=>"18"} missing required keys: [:id]

Would be grateful for help!

Upvotes: 0

Views: 442

Answers (2)

Qaisar Nadeem
Qaisar Nadeem

Reputation: 2424

Issue is in your Rabbit Controller Action.

In your Show action you are creating a new comment for rabit This Newly created Comment is not saved in Database and so does not have an ID yet. So in your view error occurs that there is not any ID for this comment.

I don't know why are you creating this Comment if you need it then you should have a check for Edit link that Create a link if record is already created. Because this make no sense to Edit a record which is not created yet.

So this would work

<%= link_to "Edit", edit_ribbit_comment_path(@ribbit, comment) unless comment.new_record?%>

Upvotes: 0

Pavan
Pavan

Reputation: 33542

No route matches {:action=>"edit", :controller=>"comments", :id=>nil, :ribbit_id=>"18"} missing required keys: [:id]

You need to change

<%= link_to "Edit", edit_ribbit_comment_path(@ribbit) %>

to

<%= link_to "Edit", edit_ribbit_comment_path(@ribbit, comment) %>

Your route expects :id and :ribbit_id as required keys. Since :id is nil, so is the error.

Upvotes: 1

Related Questions