提前感谢谁试图或解决问题。
我无法添加多个选项..我有一个模型 PriestProfile 和区域都有多对多关系..所以我想要的是用户创建时一个新的PriestProfile应该能够选择多个区域但是没有这样做..
这是我的代码..
(应用程序/控制器/ priest_profiles_controller.rb)
class PriestProfilesController < ApplicationController
before_action :set_priest_profile, only: [:show, :edit, :update, :destroy]
before_action :set_areaids, only: [:create]
def update_cities
@areas = Area.where("city_id = ?", params[:city_id])
respond_to do |format|
format.js
end
end
# GET /priest_profiles
# GET /priest_profiles.json
def index
@priest_profiles = PriestProfile.all
end
# GET /priest_profiles/1
# GET /priest_profiles/1.json
def show
@listOfCities= City.all
end
# GET /priest_profiles/new
def new
@priest_profile = PriestProfile.new
@cities = City.all.order(:name)
@areas = Area.where("city_id = ?", City.first.id).order(:name)
end
# GET /priest_profiles/1/edit
def edit
end
# POST /priest_profiles
# POST /priest_profiles.json
def create
@priest_profile = PriestProfile.new(priest_profile_params)
respond_to do |format|
if @priest_profile.save
@priest_profile.areas << @areaids
format.html { redirect_to @priest_profile, notice: 'Priest profile was successfully created.' }
format.json { render :show, status: :created, location: @priest_profile }
else
format.html { render :new }
format.json { render json: @priest_profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /priest_profiles/1
# PATCH/PUT /priest_profiles/1.json
def update
respond_to do |format|
if @priest_profile.update(priest_profile_params)
format.html { redirect_to @priest_profile, notice: 'Priest profile was successfully updated.' }
format.json { render :show, status: :ok, location: @priest_profile }
else
format.html { render :edit }
format.json { render json: @priest_profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /priest_profiles/1
# DELETE /priest_profiles/1.json
def destroy
@priest_profile.destroy
respond_to do |format|
format.html { redirect_to priest_profiles_url, notice: 'Priest profile was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_priest_profile
@priest_profile = PriestProfile.find(params[:id])
end
def set_areaids
x = params[:priest_profile][:area_id]
x.shift(0) #due to hidden field one empty initial element
@areaids = Area.find(x)
end
# Never trust parameters from the scary internet, only allow the white list through.
def priest_profile_params
params.require(:priest_profile).permit(:name, :phone_wrk, :phone_pr, :religion, :icon, :brief, :description, :area_ids, :area_id, :city_id)
end
end
(应用/视图/ priest_profile / _form.html.erb)
<%= form_for(@priest_profile) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :phone_wrk %><br>
<%= f.text_field :phone_wrk %>
</div>
<!--=================================================-->
<h3> <%= f.select :city_id, options_for_select(@cities.collect { |city|
[city.name.titleize, city.id] }), {include_blank: "(Select City)"}, { id: 'cities_select'} %>
<%= f.select :area_id, options_for_select(@areas.collect { |area|
[area.name.titleize, area.id] }, 0), {},
{id: 'areas_select',multiple: true}%></h3>
<!--=================================================-->
<br />
<div class="actions">
<%= f.submit %>
</div>
<% end %>
(应用/模型/ priest_profile.rb)
class PriestProfile < ActiveRecord::Base
validates :phone_wrk,:name, presence: true
has_many :priest_areaships
has_many :areas, through: :priest_areaships
attr_accessor :city_id, :area_id, :area_ids
end
答案 0 :(得分:0)
创建一个新的PriestProfile应该能够选择多个区域
#app/controllers/priests_profiles_controller.rb
class PriestsProfilesController < ApplicationController
def new
@priest_profile = PriestProfile.new
@cities = City.all.order(:name)
@areas = @cities.first.areas.order(:name)
end
def create
@priest_profile = PriestProfile.new profile_params
@priest_profile.save
end
private
def profile_params
params.require(:priest_profile).permit(:name, :phone_wrk, :city_id, :area_ids)
end
end
#app/views/priests_profiles/new.html.erb
<%= render "form", locals: { priest_profile: @priest_profile } %>
#app/views/priests_profiles/_form.html.erb
#Never use instance vars in partials; always pass locals
<%= form_for priest_profile do |f| %>
<%= f.text_field :name %>
<%= f.text_field :phone_wrk %>
<%= f.collection_select :city_id, @cities, :id, :name, { prompt: "(Select City)" }, { id: 'cities_select'} %>
<%= f.collection_select :area_ids, @areas, :id, :name, {}, {id: 'areas_select', multiple: true } %>
<%= f.submit %>
<% end %>
解决这个问题的方法有两个方面:
- 检查HTML表单是否正确呈现
- 检查是否正在发送正确的参数
醇>
因为Rails只是呈现HTML表单,所以您需要确保您的字段名称正确,并且您正在构建正确的表单控件。
使用collection_select
可以大大简化当前设置 - 这应该使用控制器中定义的变量值自动填充选择框。
要注意的重点是city_id
和area_ids
- 如果它们是正确的,那么HTML表单应该足够了。
-
第二步是查看表单传递给控制器的参数。这可以通过控制台或开发日志/log/development.log
完成 - 它将显示通过表单提交发送的参数。
如果你有正确的参数,它应该保存。我怀疑你没有允许合适的参数。我已经包含area_ids
- 收集参数的标准Rails实践;这可能是错误的,但只有当你观察到哪些帕尔马已经通过时才能显示。