user250181
user250181

Reputation: 175

How do I create default values in a Ruby Form?

This might be a really basic question but how do I create default values in forms?

I'm trying to put the <%= params[:id] %> of the page in as a default hidden value in this form.

`<% form_for(@revision) do |f| %>

<%= f.label :background_title %><br />
<%= f.text_field :background_title %><%= params[:id] %>

<%= f.label :title %><br />
<%= f.text_field :title %>

<%= f.submit 'Create' %>

<% end %>`

Thanks.

Upvotes: 0

Views: 362

Answers (2)

Staelen
Staelen

Reputation: 7841

If you are trying to create a new object, you can set the default values when you first instantiate a new object

def new
  @revision = Revision.new(
    :background_title => "Some Background Title",
    :title => "Some Title"
  )
end

then automatically the value of the fields will be set accordingly =) it's just that simple ;-)

Upvotes: 1

Alex Reisner
Alex Reisner

Reputation: 29477

The form is linked to the object you pass to form_for, so set the value on the object before you start the form. For example, in the controller:

@revision.id = params[:id]

then in the form:

<%= f.hidden_field :id %>

However, I hope this is an example and you're not actually setting the ID (primary key) of an object based on a URL parameter...

Upvotes: 1

Related Questions