在 Rails 中保存单选按钮值

发布于 2024-11-30 19:50:06 字数 2977 浏览 0 评论 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>

我更正了拼写错误(':actionsto: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])
    @[email protected]
    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])
    @[email protected]
    if @term.update_attributes(params[:term])
        flash[:success] = "Term status changed."
        redirect_to @term  
    else
        flash[:success] = "Error"
        redirect_to @term 
    end 
end 

谢谢!

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])
    @[email protected]
    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])
    @[email protected]
    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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-12-07 19:50:06

我认为您的 form_for 调用中有一个错误:而不是使用 :url => {:动作=> "update_status" } 应该是 :url => {:动作=> “update_status”}

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

独享拥抱 2024-12-07 19:50:06

感谢 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文