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.
Related
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.
I am new to RoR and still don't have enough experience on solving the different errors that may appear to me. In this case I am designing a blog where I can post articles. More specifically, my problem is related to deleting these articles.
As far as I know, writing:
resources :articles
in the routes file is an alternative for writing:
get "/articles" #index
post "/articles" #create
delete "/articles/:id" #delete
get "/articles/:id" #show
get "/articles/new" #new
get "/articles/:id/edit" #edit
patch "/articles/:id" #update
put "/articles/:id" #update
When I try to delete an article I get the following error:
No route matches [POST] "/articles/1"
The code I wrote was:
View
<% #articles.each do |art| %>
<%= art.title %>
<div>
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
</div>
<% end %>
Controller
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
It sounds like you have this in your view:
<%= art.body %> - <%= link_to "Delete", art, method: :destroy %>
But you actually need:
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
I'd advise double-checking this in your app based on your reply to a comment from #GonzaloRobaina.
It looks to me like you're missing the correct path in your code. It should work with something like this :)
<%= link_to "Delete, article_path(art), method: :delete %>
I have a "Category" resource, and am trying to link to Categories#show in its index.html.erb file using the link_to method and the _path routes. When I view the index in my browser I see the following error:
Routing Error No route matches {:action=>"show", :controller=>"categories"}
But I do have a show action defined in my Categories controller, and removing the URI part of link_to causes the index to display normally without having an exception thrown! I can't figure out where I'm going wrong with the URI.
Here's /categories/index.html.erb:
<h1>My Links</h1>
<% #categories.each do |c| %>
<h2><%= link_to "#{c.category}", category_path %></h2>
<p><%= c.description %></p>
<% end %>
<%= link_to "Add new category", new_category_path %>
Here's /categories/show.html.erb:
<h1><%= #category.category %></h1>
<p><%= #category.description %></p>
<p><%= link_to "Back to index", root_path %></p>
Here's /config/routes.rb:
LinkManager::Application.routes.draw do
resources :categories do
resources :links
end
root :to => 'categories#index'
Here's part of /controllers/categories_controller.rb:
class CategoriesController < ApplicationController
def index
respond_with(#categories = Category.all)
end
def show
#category = Category.find(params[:id])
end
And here's the result of running rake routes (put it on pastebin since I couldn't figure out how to format it nicely here): rake routes
Can anyone spot what's wrong? I've looked at many other similar routing questions here but couldn't get a solution from them. Thank you.
Try to replace:
<h2><%= link_to "#{c.category}", category_path %></h2>
with:
<h2><%= link_to "#{c.category}", category_path(c) %></h2>
See? You have to provide a category for category_path.
Or just use the magic:
<h2><%= link_to "#{c.category}", c %></h2>
And by the way, what's the purpose of interpolation here: "#{c.category}"? Wouldn't simply c.category be enough?
Well, i think if you add other route to action show it works, do this:
do this in your browser:
localhost:3000/categories/show
and in your routes.rb:
match "/categories/show/:id" => categories#show
what is the version of our ruby and the version of rails?
If I visit this page /articles/1/comments
Why won't this work (views/comments/index.html.erb)
<% #comments.each do |comment| %>
<%= link_to "show", article_comment_path(comment)
<% end %>
and this will?
<% #comments.each do |comment| %>
<%= link_to "show", article_comment_path(#article, comment)
<% end %>
routes.rb
resources :articles
resources :comments
end
I would think the route helper would be smart enough to infer I want to use the article in the current context...
Magic is pretty nice except when you spend a lot of time expecting it to be magical and it's not :P
You cannot expect too much. This way you still have the freedom to use an instance variable, a plain parameter. The link_to helper can also be used outside the context of the controller. Furthermore, the list of possible parameters is dynamic. If you give one parameter, it has no way of knowing which you did specify: the article? The comment?
Note that you can just write:
link_to "show article", #article
link_to "show comment", [#article, comment]
Hope this helps.
I have the following rails 3 nested models:
resources :books do
resources :authors
end
I now have a view here: /books/131/authors/
And I want to each record to link to something like: /books/131/authors/3333
<% #authors.each do |author| %>
<%= link_to 'author.name', book_author_path(#book, #author) %>
<% end %>
but that error's with: No route matches {:action=>"destroy", :controller=>"authors"}
I also tried:
<%= link_to 'author.name', [#book, author] %>
Problem is the code keeps linking to /authors/3333, not /books/131/authors/3333
Ideas? thanks!
book needs to be defined in the author controller for def index
<%= link_to "title", book_author_path(#book, author) %>