Rails f.radio_button没有将值传递给params

时间:2017-07-31 14:02:03

标签: ruby-on-rails ruby-on-rails-5

我有一个带有单选按钮的标准form_for,它们没有将值传递给params。

这是表格(减去额外内容):

<%= form_for(switch) do |f| %>
  <%= f.radio_button :referrer, 0, checked: true, class: 'di' %> 
  <%= f.label :referrer, "Facebook", class: 'di mr20' %>

  <%= f.radio_button :referrer, 1, class: 'di' %>
  <%= f.label :referrer, "Email", class: 'di mr20' %>

  <%= f.radio_button :referrer, 2, class: 'di' %>
  <%= f.label :referrer, "Through a charity", class: 'di mr20' %>

  <%= f.radio_button :referrer, 3, class: 'di' %>
  <%= f.label :referrer, "Other", class: 'di mr20' %>

  <%= f.submit "SWITCH NOW" %>
<% end %>

在params中它识别出引用者的存在,但不保留值:"referrer"=>""

输出的HTML看起来很好:

<input type="radio" value="0" checked="checked" name="switch[referrer]" id="switch_referrer_0"> 
<label for="switch_referrer">Facebook</label>

<input type="radio" value="1" name="switch[referrer]" id="switch_referrer_1">
<label for="switch_referrer">Email</label>

<input type="radio" value="2" name="switch[referrer]" id="switch_referrer_2">
<label for="switch_referrer">Through a charity</label>

<input type="radio" value="3" name="switch[referrer]" id="switch_referrer_3">
<label for="switch_referrer">Other</label>

我已经阅读了7-8个关于此的其他问题并且没有找到解决方案,所以任何帮助都会很棒。

干杯

修改

从控制台(中间有一个撬):

Started POST "/switches" for 127.0.0.1 at 2017-08-01 15:55:30 +0100
Processing by SwitchesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Iln5+K3NadD8xzdzQdwV3h7A5QINfuczOqXov6Yv+u4f7oM3OrxZSaJaeBYL7sWLPWYwpsLJn+EgkC3sgI+pSQ==", "switch"=>{"first_name"=>"", "last_name"=>"", "email"=>"", "phone"=>"", "referrer"=>"", "partner_id"=>"15", "charity_id"=>"19"}, "commit"=>"SWITCH NOW"}

From: /Users/dannysantos/projects/EthicalSwitch/app/controllers/switches_controller.rb @ line 11 SwitchesController#create:

    10: def create
 => 11:   binding.pry
    12:   @switch = Switch.new(switch_params)
    13:
    14:   if @switch.save
    15:     redirect_to get_redirect_path(@switch)
    16:   else
    17:     render :new
    18:   end
    19: end

[1] pry(#<SwitchesController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"Iln5+K3NadD8xzdzQdwV3h7A5QINfuczOqXov6Yv+u4f7oM3OrxZSaJaeBYL7sWLPWYwpsLJn+EgkC3sgI+pSQ==", "switch"=>{"first_name"=>"", "last_name"=>"", "email"=>"", "phone"=>"", "referrer"=>"", "partner_id"=>"15", "charity_id"=>"19"}, "commit"=>"SWITCH NOW", "controller"=>"switches", "action"=>"create"} permitted: false>
[2] pry(#<SwitchesController>)>
   (0.2ms)  BEGIN
  Partner Load (0.2ms)  SELECT  "partners".* FROM "partners" WHERE "partners"."id" = $1 LIMIT $2  [["id", 15], ["LIMIT", 1]]
  SQL (1.3ms)  INSERT INTO "switches" ("first_name", "last_name", "email", "phone", "partner_id", "charity_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["first_name", ""], ["last_name", ""], ["email", ""], ["phone", ""], ["partner_id", 15], ["charity_id", 19], ["created_at", "2017-08-01 14:57:04.704624"], ["updated_at", "2017-08-01 14:57:04.704624"]]
   (1.2ms)  COMMIT
Redirected to http://www.powershop.com.au/ethicalswitch/
Completed 302 Found in 93785ms (ActiveRecord: 3.0ms)

0 个答案:

没有答案