Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-populating a form field from a link

I'm trying to pre-populate a string field in a form when a link is clicked. I've tried:

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

and

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

Both didn't work. Anyone has any idea?

like image 635
Ruby Ku Avatar asked Dec 26 '22 16:12

Ruby Ku


1 Answers

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

def new
@product = Product.new(:name=>params[:name])
end
<%= f.text_field :name %>
like image 86
Amar Avatar answered Jan 07 '23 14:01

Amar