开发者

Use your own default value when using form builders in Rails

I am trying to write the edit/update methods for one of my forms. The only time this model needs to be changed is to go from an 'inactive state' to an 'active state.' I would like to do this with a button, but I cannot figure out how to make it work. My form with the button looks this:

<% form_for [league, league_relati开发者_如何学运维on], :html => {:method => :put} do |form| %>
<%= form.error_messages %>
  <fieldset>
    <%= form.hidden_field :state, LeagueRelation::ACTIVE %>
    <%= form.submit 'Activate User' %>
  </fieldset>
<% end %>

This form is to update my LeagueRelation model, which is a nested resource of the League model. The default state is 1, which means inactive. Here, I am trying to create a form with only one button that says Activate. Since this is a form builder, Rails is trying to put the value of league_relation.state into the hidden field, instead of the value I want. I know using hidden_field_tag would work, but I do not want to use this approach. Any suggestions?

Thanks


In this case I would probably use the button_to helper instead of form_for. Perhaps something like this:

button_to "Activate User", 
          :url => path_to_update_url, 
          :method => :put, 
          :state => LeagueRelation::ACTIVE

The button_to helper will automatically create a button wrapped inside a form with all the parameters that you choose to supply.

Edit:

Though if you still want to use the form_for then you can manually specify a value like this:

<%= form.hidden_field :state, :value => LeagueRelation::ACTIVE %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜