2015-10-05 49 views
1

預先感謝誰嘗試或解決問題。接受多個選項欄

我不能添加多個選項。我有一個模型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 
+1

刪除問題中不相關的代碼。只留下重要的位。 –

+0

'@areaids = Area.find(x)' - 這隻返回一個區域。我想,你應該從這裏開始你的修復。 –

+0

@SergioTulentsev令人驚歎...感謝提示,我應該在一天前提出這個問題... –

回答

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 %> 

解決這一問題的方法是兩方面的:

  1. 檢查HTML表單是否被正確呈現
  2. 檢查是否正確PARAMS正在發送

因爲Rails只是呈現一個HTML表單,所以您需要確保您的字段名稱是正確的,並且您正在構建鑽井平臺ht表單控件。

您當前的設置可以通過使用collection_select進行大量簡化 - 這應該使用您在控制器中定義的變量的值自動填充選擇框。

要注意的重點將是city_idarea_ids - 如果它們正確,HTML表單應該足夠。

-

第二步是看你的形式傳遞給控制器​​的PARAMS。這可以從控制檯或開發日誌/log/development.log完成 - 它會顯示通過表單提交發送的參數。

如果你有正確的參數,它應該保存。我懷疑你沒有準確的參數。我已經包含了area_ids--標準的Rails練習收集參數;這可能是錯誤的,但只有在您觀察哪些帕爾馬斯已經通過時才能顯示。

+0

感謝您清理代碼並檢查日誌的想法 –