2016-07-18 25 views
0

das Absenden Durch Drücken der Taste gibt die folgende Fehlermeldung:Wie kommen Sie in einer Tabelle einfügen Multiselect

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

und die Parameter der Anfrage:

Parameters: 

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

Ich weiß nicht, was passiert, offenbar die Controller erhält den Namen des Autors als Parameter und kann damit nicht umgehen. Modelle

:

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 

Controller:

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 

und die _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 %> 
+0

Bitte entfernen Sie den Link zu dem Bild, das den Fehler zeigt, und kopieren Sie den Fehler in Ihre Frage. Bilder zwingen uns, die Informationen einzutippen, was die Hilfe für Sie erschweren wird. Außerdem können die Links verrotten und brechen, und wenn sie Ihre Frage stellen, ergibt das keinen Sinn. Siehe "[mcve]". Wir kümmern uns nicht um Ihr Fachwissen, wir wollen gut recherchiert und Fragen gestellt werden. Bitte lesen Sie "[fragen]" und die verlinkten Seiten; Sie werden Ihnen helfen, bessere Fragen zu stellen. –

+0

ok, danke für den Hinweis. Änderungen wurden bereits vorgenommen. –

+0

Hat Ihre Authorbook-Tabelle eine Namensspalte? Ich könnte versuchen, die Anwesenheit des Namens zu validieren, und dies könnte den Fehler –

Antwort

0

Entfernen Sie einfach die

validates :name, :presence => true #Authorbook  

in "Authorbooks", weil Ihr Modell dieses Feld nicht hat, also fordern Sie im Grunde nur Rails an, ein Feld zu validieren, das nicht existiert.

+0

danke männer !. war ein sehr dummer Fehler. Ihre Antwort löst das Problem. sehen Sie u. –