Rails renders wrong URL with nested resources - ruby-on-rails

I'm not really sure what going on here. After nesting the resources like this
resources :users do
resources :bookmarks
end
I expected the url should be /users/[user-id]/bookmarks/[bookmark-id]/edit, but rails renders a reverse path /users/[bookmark-id]/bookmarks/[user-id]/edit
This is the view
<%= link_to 'edit', edit_user_bookmark_path(#bookmark) %>
Any idea how to fix it ? Thanks

What you expect is the rule.
Replace with:
<%= link_to 'edit', edit_user_bookmark_path(current_user, #bookmark) %>

Related

Link_to path in each do loop not working

In this Rails app, Users write Stories. A Story may be part of a Collection. Collections belong to the User who created it.
I am trying to show a single Story with links to the Collections it is part of. The collection.name part works but I can't get the collection_path right. Thanks for your help.
stories/show.html.erb
<% #story.collections.each do |collection| %>
<%= link_to collection.name, collection_path %>
<% end %>
rake routes for collections
user_collections GET /users/:user_id/collections(.:format) collections#index
POST /users/:user_id/collections(.:format) collections#create
new_user_collection GET /users/:user_id/collections/new(.:format) collections#new
edit_user_collection GET /users/:user_id/collections/:id/edit(.:format) collections#edit
user_collection GET /users/:user_id/collections/:id(.:format) collections#show
routes.rb
resources :users do
resources :collections
Solved it by using the following with the help of Sebastián Palma who answered this earlier.
<% #story.collections.each do |collection| %>
<%= link_to collection.name, user_collection_path(collection.user, collection), class: 'btn btn-lake' %>
<% end %>

Issue with link_to in index

i am a newbie to rail and try to build my first site but face an issue with a link_to in an index page. The link redirect to /recipes.1 instead of /recipes/1.
The show page work when i try /recipes/1.
Index.html.erb
<% provide(:title, "Recipe") %>
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe%>
<%end%>
route.db
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
resources :users
resources :recipes
recipes_controller.rb
def index
#recipes = Recipe.all
end
def show
#recipe = Recipe.find(params[:id])
end
Remove the following routes from routes.rb:
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
The above routes are not required since resources :recipes generate all these routes for you.
Hope it helps!
Try this code
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe_url(recipe) %>
<%end%>
Use
<%=link_to recipe.label, recipe_path(recipe)%>
Instead of
<%= link_to recipe.label, recipe%>
you have defined show path twice.
1. in manual defined path
get 'recipe' => 'recipes#show'
2. Through
resources :recipes
If you do rake routes, then you will first option create routes for get method with /recipe url and appends parameter to it which result in /recipes.1 path.
Also rails read routes file in top to bottom approach. As uses first routes for show method.

How to link to :id pages rails

Been trying to find out a better way to link to pages that contains /:id's in them. Because the way I'm doing it now works but can easily break if I change something.
Currently I have two of these examples. One to link to your profile done like this:
<li><%= link_to "Your profile", root_url + "users/#{current_user.id}" %></li>
and the other:
<%= link_to "Apply now", root_url + "answers/#{f.id}" %>
how do I make it so that I don't have to rely on the manual change of this. On my rake routes I cant write 'answers_path/#{f.id}' for example as it doesn't show me any path.
So how do I do this? My routes setup for these two are currently:
match '/answers/:id', to: 'answers#show', via: 'get'
match '/users/:id', to: 'users#show', via: 'get'
Instead of using match I would define resources routes
resources :users, :answers
Now you can use:
<%= link_to 'Your Profile', current_user %>
<%= link_to 'Apply Now', f %>
You can learn more about rails path helpers and resource routing here - http://guides.rubyonrails.org/routing.html
Also, if you don't need all the CRUD actions, you can use only:
resources :users, :answers, only: :show
The following should do it for you.
<li><%= link_to "Your profile", user_path(current_user.id) %></li>
and
<%= link_to "Apply now", answer_path(f.id) %>
I do, however, recommend you review the following Rails Guide: http://guides.rubyonrails.org/routing.html

Nested Route link_to loop route helper

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.

undefined method `edit_comment_path' (Rails)?

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.

Resources