【问题标题】:How do you input the value of a radio button form with multiple input values as a string and input to the database in Ruby on Rails?如何将具有多个输入值的单选按钮表单的值作为字符串输入并输入到 Ruby on Rails 中的数据库?
【发布时间】:2016-10-05 21:26:14
【问题描述】:

所以基本上,我有这个 A 网站的申请表,我正在尝试添加单选按钮,以便人们可以从给定的选项中选择正确的答案。我还希望能够将表单的值作为字符串输入到数据库中,例如“HOME OWNER”。

<%= form_for @applicant do |f| %>

顶部的“电子邮件”字段效果很好。所有操作都在申请者控制器中到位,并且数据库已设置,这意味着我可以将电子邮件表单字段的输出保存到数据库中。但是单选按钮不起作用。以下是我的观点/applicants/new.html.erb

 <%= f.label :email %>
 <%= f.email_field :email, class: 'form-control' %>

   <%= radio_button_tag(:property_relationship, "Home-Owner") %>
   <%= label_tag(:Home_owner, "Home Owner") %>

   <%= radio_button_tag(:property_relationship, "Private-Tennant") %>
   <%= label_tag(:private_tennant, "Private Tennant") %>

   <%= radio_button_tag(:property_relationship, "Landlord") %>
   <%= label_tag(:landlord, "Landlord") %>

   <%= radio_button_tag(:property_relationship, "Council-Tennant") %>
   <%= label_tag(:council_tenant, "Council Tennant") %>

   <%= f.submit 'Submit', class: 'btn btn-default' %>

<% end %>

以下行在我的 routes.rb 文件中,它使路由发生

resources :applicants

这是我的申请者控制器.rb 文件

class ApplicantsController < ApplicationController
  def new
    @applicant = Applicant.new
  end

  def create
      @applicant = Applicant.new(applicant_params)

      if @applicant.save

          flash[:success] = 'Form submitted'
          redirect_to apply_path
      else
          flash[:danger] = 'Error occured, message has not been sent'
          redirect_to apply_path
      end
  end

  private

      def applicant_params
          params.require(:applicant).permit(:property_relationship, :email)
      end


end

我的数据库架构.. 我表单中的单选按钮用于将表单中的“property_relationship”信息输入数据库。是不是应该是 t.somethingelse 而不是 t.string?

ActiveRecord::Schema.define(version: 20160928204355) do

  create_table "applicants", force: :cascade do |t|

   t.string   "property_relationship"
   t.string   "email"
   t.datetime "created_at",                null: false
   t.datetime "updated_at",                null: false
  end

我在架构中还有另一个表用于联系人 - 如下所示。但这不应该干扰,因为 form_for 是针对 @applicant 的。

  create_table "contacts", force: :cascade do |t|

    t.string   "name"
    t.string   "email"
    t.text     "comments"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end

这是日志/development.log 文件

Started POST "/applicants" for 88.106.10.152 at 2016-10-05 21:49:59 +0000
Processing by ApplicantsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"UBXlJ5gzz+BEYk+SDgzSlR8AVctsLJkyKW51NhGbjTLf2tFzzuUvhJ5ahn2R/kCWBZVNRnl5DebP4GG0QxNKwA==", "property_relationship"=>"Home-Owner", "applicant"=>{"email"=>"daniel@example.com"}, "commit"=>"Submit"}
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "applicants" ("email", "created_at", "updated_at") VALUES (?, ?, ?)  [["email", "daniel@example.com"], ["created_at", "2016-10-05 21:49:59.209017"], ["updated_at", "2016-10-05 21:49:59.209017"]]
   (15.7ms)  commit transaction
Redirected to https://boilerleadgen-danthedangerman.c9users.io/apply
Completed 302 Found in 25ms (ActiveRecord: 16.5ms)


Started GET "/apply" for 88.106.10.152 at 2016-10-05 21:49:59 +0000
Processing by ApplicantsController#new as HTML
  Rendered applicants/new.html.erb within layouts/application (1.9ms)
Completed 200 OK in 89ms (Views: 87.7ms | ActiveRecord: 0.0ms)

这里是使用申请人.all 命令的 rails 控制台。如您所见,property_relationship 的值为零,但实际上应该是我输入的值(房主)

(user_application) $ rails c
Loading development environment (Rails 4.2.5)
2.3.0 :001 > Applicant.all
  Applicant Load (3.4ms)  SELECT "applicants".* FROM "applicants"
 => #<ActiveRecord::Relation #<Applicant id: 8, email: "daniel@example.com", property_relationship: nil, created_at: "2016-10-05 21:48:17", updated_at: "2016-10-05 21:48:17">

我真的被困在这里,而且我也是 Rails 的新手,所以非常感谢所有帮助。 再一次,我想要做的是将单选按钮的输出作为字符串保存到数据库中。

感谢您的阅读

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。查看您的服务器日志(log.development.log 或您的终端窗口)并查看在您提交表单时打印的行。当您点击提交时,它们通常会显示完整的params。编辑您的问题并发布您在那里看到的内容,以便我们可以看到它,这将有助于我们调试您的问题(不要将其发布在 cmets 中,因为格式可怕且难以阅读):)
  • 注意:目前我最好的猜测是单选按钮的名称不正确......您使用的是radio_button_tag而不是f.radio_button,这意味着它们可能不会嵌套在@下987654333@他们应该的方式......我们可以通过查看params中的内容来确认这一点
  • 我让它工作了!我只需要将 radio_button_tag 更改为 f.radio_button 谢谢!
  • 酷,我会回答这个问题的:)

标签: ruby-on-rails ruby database forms radio-button


【解决方案1】:

我认为您的单选按钮只是命名不正确...您使用的是 radio_button_tag 而不是 f.radio_button 这意味着它们可能不会以应有的方式嵌套在 applicant 下(并且您的许可/require 方法正在等待)。

您可以通过查看服务器日志中的 params 来确认这一点。

【讨论】:

    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 2019-06-18
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2016-01-15
    • 1970-01-01
    相关资源
    最近更新 更多