如何使用rails保存数据库中的单选按钮选择

时间:2012-04-19 11:12:45

标签: ruby-on-rails ruby haml

这是我的控制者:

class ReportNeedsController < ApplicationController
def index
@report_needs = nil
end
  def new
     @report_need = ReportNeed.new
  end    
  def create
     @report_need = ReportNeed.new(params[:report_need])
     @report_need.save
     redirect_to @report_need
  end
end

这是我的new.haml文件:

您为什么要举报?

%form
    %input{:name => "option", :type => "radio", :value => "spam"}
    spam                   
    %br/    

enter code here


    %input{:name => "option", :type => "radio", :value => "product_corrupted"}      
    product_corrupted                   
    %br/                      
    %input{:name => "option", :type => "radio", :value => "product_spoiled"}       
    product_spoiled

1 个答案:

答案 0 :(得分:0)

在视图中进行更改,如:

  %input{:type => "radio", :name => "report_need[product_type]", :id => "report_need[product_type]-1", :value => "salesforce", :checked => true}
  %br/
  %input{:type => "radio", :name => "report_need[product_type]", :id => "report_need[product_type]-2", :value => "salesforce", }

在控制器中,如果 product_type 是表格 report_needs 的字段,则无需更改操作。否则它会改变并存储起来:

  def create
     report_need = ReportNeed.new(params[:report_need])
     report_need.radio_field_name = params[:report_need][product_type]
     report_need.save
     redirect_to report_need
  end