开发者

Saving radio buttons values in rails

I'm trying to use radio buttons in my rails application. Currently the buttons are showing up on the page as expected, but the value is not being saved upon submission. I think the problem is actually with my submit button - nothing is happening when it is pressed.

Here is the code for my page with the radio buttons:

<div class="form_row>
<%= form_fo开发者_如何学Pythonr @term, :url=>{ :action =>"update_status" } do |f| %>
  <%= f.radio_button :status, 'on' %><b>On</b> <br/>
  <%= f.radio_button :status, 'off' %><b>Off</b> <br/>
  <%= f.radio_button :status, 'leave' %><b>Leave</b> <br/>
  <div class="actions">
    <%= f.submit "Change term status" %>
  </div>
<% end %>
</div>

I corrected my typo (':actionsto:action`) but it's still not working. Here's some more information...

The radio buttons are on the top of the page, and the rest of the form is below them. I have two different submit buttons, one for the radio buttons, and one for the fill in the blank information at the bottom of the page. The second form works perfectly, but when I click the "Change term status button" (the button that is supposed to submit the radio buttons by calling update_status, nothing happens.

Here is all of the code for my page view:

<h1> <%= @title %> </h1>

<div class="form_row>
<%= form_for @term, :url=>{ :action =>"update_status" } do |f| %>
  <%= f.radio_button :status, 'on' %><b>On</b> <br/>
  <%= f.radio_button :status, 'off' %><b>Off</b> <br/>
  <%= f.radio_button :status, 'leave' %><b>Leave</b> <br/>
  <div class="actions">
    <%= f.submit "Change term status" %>
  </div>
<% end %>
</div>

<%= form_for @term, :url=>{ :action=>"update" } do |f| %>     
  <div class="field">
    <%= f.label :course1, "Course 1" %><br />
    <%= f.text_field :course1 %>
  </div>
  <div class="field">
    <%= f.label :course2, "Course 2" %><br />
    <%= f.text_field :course2 %>
  </div>
  <div class="field">
    <%= f.label :course3, "Course 3" %><br />
    <%= f.text_field :course3 %>
  </div>
  <div class="field">
    <%= f.label :course4, "Course 4" %><br />
    <%= f.text_field :course4 %>
  </div>
  <div class="actions">
       <%= f.submit "Update" %>
  </div>
<% end %>

And here are both definitions:

 def update
    @term = Term.find(params[:id])
    @dplan=@term.dplan
    if @term.update_attributes(params[:term])
        flash[:success] = "Edit successful."
        redirect_to @dplan
    else
        flash[:success] = "Error"
        redirect_to @dplan
    end 
end 

def update_status
    @term = Term.find(params[:id])
    @dplan=@term.dplan
    if @term.update_attributes(params[:term])
        flash[:success] = "Term status changed."
        redirect_to @term  
    else
        flash[:success] = "Error"
        redirect_to @term 
    end 
end 

Thank you!


I think there is a mistake in your form_for call: Instead of using :url => { :actions => "update_status" } it should be :url => { :action => "update_status" }.


Figured it out thanks to http://railscasts.com/episodes/38-multibutton-form! I guess something weird happens when you try to use two forms on one page, the best way to do it was to combine the forms but use an if statement in my terms controller update definition to distinguish between the two buttons. Thanks for all your help!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜