[英]Ruby on Rails: Multiple Input Fields in The Same Form - Change ID/Value
[英]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?
因此,基本上,我有一份適用於A網站的申請表,正在嘗試添加單選按鈕,以便人們可以從給定的選項中選擇正確的答案。 我還希望能夠將表單的值作為字符串輸入數據庫,例如“ HOME OWNER”。
<%= form_for @applicant do |f| %>
頂部的“電子郵件”字段效果很好。 所有操作均已在applicants_controller中進行,數據庫已建立,這意味着我可以將電子郵件表單字段中的輸出保存到數據庫中。 但是單選按鈕不起作用。 以下是我的觀點/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
這是我的applicants_controller.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
這是log / develop.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)
這是使用Applicant.all命令的Rails控制台。 如您所見,property_relationship的值為nil,但實際上應該是我輸入的值(房主)
(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還是陌生的,因此非常感謝所有幫助。 再一次,我想做的是將單選按鈕的輸出作為字符串保存到數據庫中。
感謝您的閱讀
我認為您的單選按鈕只是簡單地命名錯誤...您使用的是radio_button_tag
而不是f.radio_button
,這意味着它們可能不會按照應有的方式嵌套在applicant
下(並且您的允許/要求方法是期望的)。
您可以通過查看服務器日志中作為params
內容來確認這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.