简体   繁体   中英

accepting multiple options rails

Thanks in advance for who tries or solve the issue.

I am not able to add multiple options.. I have a model PriestProfile and Area both have many to many relationship .. so what I want is user when creating a new PriestProfile should be able to select multiple areas but failing to do so..

here is my code..

(app/controllers/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

(app/views/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 %>

(app/models/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

creating a new PriestProfile should be able to select multiple areas

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

The way to troubleshoot this is two-fold:

  1. Check whether the HTML form is being rendered correctly
  2. Check whether the right params are being sent

Because Rails just renders an HTML form, you need to ensure your field names are correct, and that you're building the right form controls.

Your current setup could be simplified massively by using collection_select - this should populate a select box automatically with the values from the variables you defined in your controller.

The big point to observe will be city_id and area_ids -- if they are correct, the HTML form should be sufficient.

-

The second step is to look at the params your form is passing to your controller. This can be done from the console or development log /log/development.log -- it will show you the params sent through the form submission.

If you have the right params, it should save. I suspect you don't have the right params permitted. I have included area_ids - a standard Rails practice for collection params; this may be wrong but can only be shown when you observe which parmas have been passed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM