Rails has_many :through with collection_select form helper

冷暖自知 提交于 2019-12-02 08:00:34

For anyone fighting the same kind of issue, I finally managed to get this to work with the following collection_select:

      <%= f.collection_select(:feature_ids, Feature.all, :id, :name,
                              {include_blank: false, :include_hidden => false, :selected => @property.features.map(&:id)},
                              {:multiple => true}) %>
Hemant Kumar

authors/_form.html.erb:

<%= fields_for(@author_book) do |ab| %>
  <div class="field">
    <%= ab.label "All Books" %><br>
    <%= collection_select(:books, :id, @all_books, :id, :name, {:selected => @author.books.map(&:id)}, {multiple: true}) %>
  </div>
<% end %>

authors_controller.rb:

class AuthorsController < ApplicationController
  before_action :set_author, only: [:show, :edit, :update, :destroy]

  # GET /authors
  # GET /authors.json
  def index
    @authors = Author.all
  end

  # GET /authors/1
  # GET /authors/1.json
  def show
  end

  # GET /authors/new
  def new
    @author = Author.new
    get_books
    respond_to do |format|
    format.html
    format.json { render json: @author }
  end

  end

  # GET /authors/1/edit
  def edit
    get_books
  end
  # POST /authors
  # POST /authors.json
  def create

    @author = Author.new(author_params)
    params[:books][:id].each do |book|
      if !book.empty?
        @author.authorbooks.build(:book_id => book)
      end
    end

    #binding.pry
    respond_to do |format|
      if @author.save

        format.html { redirect_to @author, notice: 'Author was successfully created.' }
        format.json { render :show, status: :created, location: @author }
      else
        format.html { render :new }
        format.json { render json: @author.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /authors/1
  # PATCH/PUT /authors/1.json
  def update
    #binding.pry
    respond_to do |format|
      if @author.update(author_params)

        @author.books = []
        params[:books][:id].each do |book|
          if !book.empty?
            @author.books << Book.find(book)
          end
        end

        format.html { redirect_to @author, notice: 'Author was successfully updated.' }
        format.json { render :show, status: :ok, location: @author }
      else
        format.html { render :edit }
        format.json { render json: @author.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /authors/1
  # DELETE /authors/1.json
  def destroy
    @author.destroy
    respond_to do |format|
      format.html { redirect_to authors_url, notice: 'Author was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_author
      @author = Author.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def author_params
      params.require(:author).permit(:name,:authorbooks_attributes => [:id,:book_ids => []])

    end

    def get_books
      @all_books = Book.all
      @author_book = @author.authorbooks.build
    end

    # def create_params
    #   params.require(:authorbooks).permit(:author_id,book_id: [])
    # end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!