預先感謝誰嘗試或解決問題。接受多個選項欄
我不能添加多個選項。我有一個模型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
刪除問題中不相關的代碼。只留下重要的位。 –
'@areaids = Area.find(x)' - 這隻返回一個區域。我想,你應該從這裏開始你的修復。 –
@SergioTulentsev令人驚歎...感謝提示,我應該在一天前提出這個問題... –