Pre-filling the form field from the link

I am trying to pre-populate a string field in a form when I click a link. I tried:

$<%= link_to "New product", new_product_path(:product_name => "foo") %> 

and

 $<%= link_to "New product", new_product_path(:name => "foo") %> 

Both did not work. Does anyone have an idea?

+4
source share
2 answers

Try this, <%= f.text_field :name,:value=>(@product.new_record? ? params[:name] : @product.name )%> or in a new action

 def new @product = Product.new(:name=>params[:name]) end <%= f.text_field :name %> 
+11
source

The field you are trying to fill in on the current page or landing page?

If you need javascript on the current page to do this, if on the landing page, you should probably set a default value in your action based on the parameters passed.

-1
source

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


All Articles