Evelyn St Evelyn St - 1 year ago 74
Ruby Question

Ruby on Rails get paramenter from URL on View

I'd like to get the paramenter from the URL on my view/html.

For example, I'm showing an specific item from my data base, and the URL is like this: "http://localhost:3000/menus/index.%23%3CMenu::ActiveRecord_Relation:0x007f50ed153250%3E?id=6"

What I want is: when I click on the New Button of that specific item, the form opens with the URL "http://localhost:3000/menus/new", but I want somehow pass that id=6 to this NEW view.

I am using this code on the html to open the form:

<%= link_to 'Novo', new_menu_path %>

I also tried:
<%= link_to 'Novo', new_menu_path(@menus, :id) %>
, but the id param is always null/empty.

How do I do that?
Thank you

Answer Source

well :id is just a symbol, you need to tell the route helper what to bind to it. For example

<%= link_to 'Novo', new_menu_path(id: params[:id]) %>

which should give you something like /menus/new?id=6