0
我試圖將chosen-rails通過rails 4
表單創建的字符串數組傳遞給mysql
。但是,我得到的只是mysql中的空列或空列。任何建議都會很棒。未傳遞到數據庫的Rails數組參數
如何我表單的外觀(在現實多了一個字段):
<%= form_for(@form) do |f| %>
<div class="col-md-12">
<%= f.label "Cities", class: "input-desc" %><br>
<div class="clearfix">
<%= f.grouped_collection_select(:city_name, @state.order(:name), :cities, :name, :name, :name, { prompt: true }, { class: "form-control chosen-select", value: "Select Cities", multiple: true }) %>
</div>
</div>
<% end %>
型號:
class Form < ActiveRecord::Base
has_many :city_names
accepts_nested_attributes_for :city_names, allow_destroy: true, reject_if: :all_blank
end
控制器:
def create
@form = Form.new(form_params)
respond_to do |format|
if @form.save
city_names = params[:city_names]
format.html { redirect_to form_url, notice: 'Form was successfully sent.' }
format.json { render :show, status: :created, location: @form }
else
format.html { render :new }
format.json { render json: @form.errors, status: :unprocessable_entity }
end
end
end
Private
def road_show_form_params
params.require(:road_show_form).permit(:name, :adress, :phone, :email, :city_names, :comments, :paid)
end
SQL:
SQL (104.4ms) INSERT INTO `forms` (`name`, `adress`, `phone`, `email`, `comments`, `paid`, `created_at`, `updated_at`)
VALUES ('Corey Vincent', '', '1234567890', '[email protected]', '', 0, '2016-09-23 15:34:29', '2016-09-23 15:34:29')
您可以看到SQL插入內容甚至沒有包含city_name
參數。
但是你可以看到它是由表單傳遞:
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"4ttP4U5+LXyrB8nQr2ka7rJqlk6cO+Fk5rCUMA2GWw2+Ww6QFf6DKW55uXVogfMkD0mvz/0QGJmw+42JQkZAZw==",
"form"=>{"paid"=>"false",
"name"=>"Corey Vincent",
"adress"=>"",
"phone"=>"1234567890",
"email"=>"[email protected]",
"city_name"=>["", "Los Angeles", "Hartford"],
"comments"=>""},
"commit"=>"Submit Vote"}
謝謝,我會試試這個。 – Corey
似乎沒有工作。儘管所有'city_name'屬性都只是城市名稱,所以實際上只有一個屬性,所以那裏不會有屬性? – Corey
你有一個'form has_many city_names'的關係,這意味着在'form_id'的數據庫中有一個'city_names'表。你可以發佈你的'schema.rb'的相關部分嗎? 'city_names'表和'forms'表。 – treiff