In my project are 2 nested resources:
Rails.application.routes.draw do
resources :posts do
resources :comments
end
root 'posts#index'
end
I'm rendering a collection of comments with the use of a partial _comment.html.erb
<%= render partial: "comments/comment", collection: #post.comments %>
The partial looks like this
<div class="comment_wrapper">
<p class="comment_name"><%= comment.name %></p>
<p class="comment_date"><%= comment.created_at %></p>
<p class="comment_body"><%= comment.body %></p>
<%= link_to "Delete comment", post_comment_path(#post.id, id: comment.id), method: :delete%>
</div>
The problem is with that nested route in the "Delete comment" link.
I keep failing to pass in the :id key. I've tried a couple of different ways to pass the variables in the link, but keep getting the same error, that the :id key is missing. When I replace the link with a paragraph to display comment.id, it gets displayed perfectly, so it's definately available in my view.
No route matches {:action=>"show", :controller=>"comments", :format=>nil, :id=>nil, :post_id=>11} missing required keys: [:id]
As you can see it also tries to call the "show" action, but I bet that will be solved as soon as it passes the ids properly.
Any ideas what I could be doing wrong here?
As you can see the error is
No route matches {:action=>"show", :controller=>"comments", :format=>nil, :id=>nil, :post_id=>11}
And in your link_to has comment.id in it:
<%= link_to "Delete comment", post_comment_path(#post.id, id: comment.id), method: :delete %>
It means that you are passing an object with no id in it (not saved in database). You are probably building your comments and that is the reason they do not have ids yet.
One of the solutions to this problem is to use your link_to like this:
<%= link_to("Delete comment", post_comment_path(#post, comment), method: :delete) unless comment.new_record? %>
Which will not show the link for new records, because you can't delete what does not exist in database yet.
Related
I'm trying to create a link that takes you to a specific users page in my rails app. The link currently looks like this:
<%= link_to track.user.username, user_path(#user) %>
However, I get this error when I try to refresh:
No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id]
Can somebody suggest what I could be missing? I'm not sure how to specifically reference the users id in the code.
Thanks,
Ant
Right apologies! I've figured it out, the link_to is placed inside of this slightly hidden loop in my code:
<% #tracks.each do |track| %>
// lots more code in here
<%= link_to track.user.username, user_path(#user) %>
<% end %>
so what i had to do was reference track first and then find the user like so:
track_path(track.user)
so my code now looks like so:
<% #tracks.each do |track| %>
<%= link_to track.user.username, user_path(track.user) %>
<% end %>
and this works perfectly fine!
Thanks all :)
Ant
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!
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.
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?%>
I am trying to use link_to in rails 4 with controller and html options and a do..end block. I have seen similar posts but have not been able to use any of the answers successfully.
Working code without a do..end block:
<%= link_to 'recommend', { controller: 'recommendations', id: offer.id }, method: :post %>
When I try to use some embedded ruby to add extra information to the link, I cannot get it to work:
<%= link_to( { controller: 'recommendations', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The code compiles but in the rendered, the link that is generated is the following:
<a controller="recommendations" id="38">
<p>Some Html</p>0
</a>
Any help would be appreciated. I think that it is a small problem with the syntax but I have tried all manner of brackets, spaces etc that I could think of without luck.
UPDATE: I have tried the following code without success:
<%= link_to( { controller: 'recommendations', action: 'create', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The HTML output is:
<a action="create" controller="recommendations" id="39">
<p>Some Html</p>0
</a>
This might not be important but as a side note, the create action doesn't have a helper function for links. When I run the
rake routes
command I get the following
...
recommendations GET /recommendations(.:format) recommendations#index
POST /recommendations(.:format) recommendations#create
new_recommendation GET /recommendations/new(.:format) recommendations#new
...
In my opinion this isn't a problem but it is a reason why code such as:
link_to create_recommendation_path
won't work. Finally, the intention of the link is to act as a 'like' button. It creates a recommendation and then displays the current page again. Once again, thanks for the help in advance.
The reason link_to create_recommendation_path doesn't work is because there is no named route for create_recommendation_path, only for recommendations_path. You can see the named routes in your routes list (which you have in your post above). The left most column that comes out of routes shows the named routes. Notice that recommendations#create doesn't have an entry in the list.
You could probably get the path you want with
<%= link_to recommendations_path(:offer_id => offer.id), :method => :post do %>
html stuff
<% end %>
This should post to a path that looks like
/recommendations?offer_id=<the offer id>
(except the post data will be in the headers not on the URL)
This will work if the create method going to do something like
Recommendation.create(params)
and the only parameter you need to create a new Recommendation is an offer_id
What I don't understand is why you're trying to POST with a link? Does creating a recommendation only require an offer id?
In your link_to you're only specifying a controller, you need to also specify the action otherwise it doesn't know where to route it to. Either use:
<%= link_to({ controller: 'recommendations', action: 'show', id: offer.id }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
Or
<%= link_to({ show_recommendations_path(id: offer.id) }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
Good Morning,
I'm having an issues with nested comments. I have a partial which shows these but I want to add a delete snippet at the bottom of .each one.
Here is the partial:
_snippets.html.erb
<% #snippets.each do |snippet| %>
<%= raw(snippet.content) %>
<% if can? :manage, snippet %>
<%= link_to 'delete', book_snippet_path(snippet), :method => :delete %>
<% end %>
<% end %>
Here are my routes:
book_snippets POST /books/:book_id/snippets(.:format) snippets#create
edit_book_snippet GET /books/:book_id/snippets/:id/edit(.:format) snippets#edit
book_snippet PATCH /books/:book_id/snippets/:id(.:format) snippets#update
PUT /books/:book_id/snippets/:id(.:format) snippets#update
DELETE /books/:book_id/snippets/:id(.:format) snippets#destroy
Here is the stack error, showing no route matches update?
No route matches {:action=>"update", :controller=>"snippets", :id=>nil, :book_id=>#<Snippet id: 4, content: "<p>YACHT!</p>\r\n", book_id: 4, created_at: "2013-11-15 09:12:20", updated_at: "2013-11-15 09:12:25", approved: true, user_id: 1>, :format=>nil} missing required keys: [:id]
I know it's probably something stupid I'm missing but would really like some help figuring this one out.
Thanks :)
You are missing book_id . You routes says
DELETE /books/:book_id/snippets/:id(.:format)
needs a book_id in path. So need to pass #book object as well in the arguments.
<%= raw(snippet.content) %>
<% if can? :manage, snippet %>
<%= link_to 'delete', book_snippet_path(#book, snippet), :method => :delete %>
<% end %>
I have a Post model and a Comment model (which is a nested resource of the first model):
resources :posts do
resources :comments
end
posts/show.html.erb:
<%= render #comments %>
I think I have some error in here:
comments/_comment.erb
<%= link_to "Edit Post Comment", [#post, edit_comment_path(comment)] %>
because I get this error:
undefined method `edit_comment_path' for #<#<Class:0xb439d8c>:0xaeaf4c0>
Any suggestions to fix this?
If you run rake routes you can see the route names, in your case the route name should be edit_post_comment_path rather than just edit_comment_path.
Maybe <%= link_to "Edit Post Comment", [#post, :edit, comment] %> or <%= link_to "Edit Post Comment", edit_post_comment_path(#post, comment) %> (untested, can't test here).
Because edit_comment_path is, like rails says, undefined.