Ruby on Rails parameter in url

This is probably a very simple fix, but so far I have not been able to find the answer.

My application has orders and tasks. Orders have many tasks. When the user clicks a new task in the display order view, he passes order.id:

<%= link_to "New Task", new_task_path(:order_id=> @order.id) %> 

The URL displays:

 /tasks/new?order_id=1 

I just don't know how to extract this and use it in my form? I tried:

 <%= f.text_field :order_id, :value => @order_id %> 

But it does not work.

+6
source share
2 answers

You can do :

 <%= f.text_field :order_id, :value => params[:order_id] %> 

Alternatively, write the value (with params ) in the controller and assign it @order_id there.

+2
source

You are doing it wrong, which is very important in Rails, where conditional configuration is such an important ideal.

If the order has many tasks, your route should look like this:

 /orders/:order_id/tasks/new 

And your routes should be configured this way:

 resources :orders do resources :tasks end 

You should [almost] never encounter record identifiers in the query string. In fact, you will almost never find query strings in Rails yourself.

-1
source

Source: https://habr.com/ru/post/890227/


All Articles