将单选按钮值保存在导轨中 [英] Saving radio buttons values in rails

查看:97
本文介绍了将单选按钮值保存在导轨中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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_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>

我更正了我的错字(':actions to:action`),但它仍然无法正常工作.这是更多信息...

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

单选按钮在页面顶部,表单的其余部分在它们下方.我有两个不同的提交按钮,一个用于单选按钮,一个用于填写页面底部的空白信息.第二种形式可以正常工作,但是当我单击更改术语状态按钮"(应该通过调用update_status提交单选按钮的按钮时,什么都没有发生.

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

这是两个定义:

 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 

谢谢!

推荐答案

感谢 http ://railscasts.com/episodes/38-multibutton-form !我想当您尝试在一页上使用两个表单时会发生一些奇怪的事情,最好的方法是合并表单,但在我的条件控制器update定义中使用if语句来区分两个按钮.感谢您的所有帮助!

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!

这篇关于将单选按钮值保存在导轨中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆