【问题标题】:How to insert multiselect in a join table如何在连接表中插入多选
【发布时间】:2016-07-18 22:55:14
【问题描述】:

按下提交按钮返回如下错误:

NoMethodError in AuthorsController#create
undefined method `name' for Authorbook:0x007fe6e4284b28

以及请求的参数:

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"/86GsyhAODP55TYKftsW64Lx1Quy5t/hfthlhG9bcto5+B9CwjejJGcMJyr5+IEpA8xi7FehmSv4cMNxvvadUg==",
 "author"=>{"name"=>"hector"},
 "books"=>{"id"=>["",
 "2",
 "3"]},
 "commit"=>"Create Author"}

不知道怎么回事,显然控制器接收到作者的名字作为参数处理不了。

型号:

class Author < ActiveRecord::Base
  validates :name, :presence => true
  has_many :authorbooks
  has_many :books, :through => :authorbooks
end

class Authorbook < ActiveRecord::Base
  validates :name, :presence => true
  belongs_to :book
  belongs_to :author
end

class Book < ActiveRecord::Base
  validates :name, :presence => true
  has_many :authorbooks
  has_many :authors, :through => :authorbooks
end

控制器:

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

    @all_books = Book.all

    @author_book = @author.authorbooks.build
  end

  # GET /authors/1/edit
  def edit
  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

    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
    respond_to do |format|
      if @author.update(author_params)
        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)
    end
end

和_form:

<%= form_for(@author) do |f| %>
  <% if @author.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@author.errors.count, "error") %> prohibited this author from being saved:</h2>

      <ul>
      <% @author.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

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

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

【问题讨论】:

  • 请删除显示错误图片的链接,然后将错误复制/粘贴到您的问题中。图像迫使我们输入信息,这将阻止对您的帮助。此外,链接可能会腐烂和断裂,当它们这样做时,您的问题将没有任何意义。请参阅“minimal reproducible example”。我们不在乎您的专业知识,我们希望得到充分研究并提出问题。请阅读“How to Ask”和链接页面;他们会帮助你提出更好的问题。
  • 好的,谢谢您的建议。更改已经完成。
  • 您的 Authorbook 表是否有名称列?我可能正在尝试验证名称的存在,这可能会引发错误
  • 不,authorbook 表只有 2 列; author_id 和 book_id。 name 列存在于 authors 表和 books 表中。

标签: ruby-on-rails ruby forms ruby-on-rails-4 multi-select


【解决方案1】:

只需删除

validates :name, :presence => true #Authorbook    

在“Authorbooks”中,因为您的模型没有该字段,所以基本上您只是要求 Rails 验证一个不存在的字段。

【讨论】:

  • 谢谢各位!是一个非常愚蠢的错误。你的回复解决了问题。见你。
猜你喜欢
  • 2018-07-22
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多