Saving radio buttons values in rails

我只是一个虾纸丫 提交于 2020-01-07 04:32:26

问题


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_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>

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!


回答1:


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" }.




回答2:


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!



来源:https://stackoverflow.com/questions/7140176/saving-radio-buttons-values-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!