0

我正在尝试在我的 rails 应用程序中使用单选按钮。目前,按钮按预期显示在页面上,但提交时未保存该值。我认为问题实际上出在我的提交按钮上——按下它时什么也没有发生。

这是带有单选按钮的页面的代码:

<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`),但它仍然无法正常工作。这里有更多信息......

单选按钮位于页面顶部,表单的其余部分位于其下方。我有两个不同的提交按钮,一个用于单选按钮,一个用于填写页面底部的空白信息。第二种形式效果很好,但是当我单击“更改术语状态按钮”(应该通过调用提交单选按钮的按钮时update_status,什么也没有发生。

这是我的页面视图的所有代码:

<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 

谢谢!

4

2 回答 2

2

我认为你的电话有一个错误form_for:而不是使用:url => { :actions => "update_status" }它应该是:url => { :action => "update_status" }.

于 2011-08-21T18:38:24.230 回答
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!

于 2011-08-22T17:20:22.797 回答