2017-05-22 92 views
0

嘗試在基本has_many和belongs_to關聯中學習accep_nested_attributes_for。顯示頁面無法工作Accepts_Nested_Attributes_For

問題是,它不是在展示頁面的工作...

我得到的錯誤是:

NoMethodError in Authors#show 

undefined method `each' for nil:NilClass 

關於這一行:<%= @book.each do |b| %>

基本設置:

作者

class Author < ApplicationRecord 
    has_many :books 

    accepts_nested_attributes_for :books 
end 

class Book < ApplicationRecord 
    belongs_to :author 
end 

作者_form.html.erb

<%= form_with(model: author, local: true) do |form| %> 
    <% 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"> 
    <%= form.label :name %> 
    <%= form.text_field :name, id: :author_name %> 
    </div> 

    <h3>Book </h3> 
    <%= form.fields_for :books do |b| %> 
    <div class="field"> 
     <%= b.label :title %><br /> 
     <%= b.text_field :title %> 
    </div> 
    <% end %> 

    <div class="actions"> 
    <%= form.submit %> 
    </div> 
<% 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 
    @book = @author.books.build 
    end 

    # GET /authors/1/edit 
    def edit 
    end 

    # POST /authors 
    # POST /authors.json 
    def create 
    @author = Author.new(author_params) 

    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, books_attributes: [:id, :title]) 
    end 
end 

作者的show.html.erb

<p id="notice"><%= notice %></p> 

<p> 
    <strong>Name:</strong> 
    <%= @author.name %> 
</p> 

<p> 
    <strong>Book(s)</strong> 
    <%= @book.each do |b| %> 
     <%= "#{b.name}" %><br /> 
    <% end %> 
</p> 

<%= link_to 'Edit', edit_author_path(@author) %> | 
<%= link_to 'Back', authors_path %> 

回答

0

顯示行動把這個

def show 
@books = @author.books 
end 

show.html.erb

<strong>Book(s)</strong> 
<%= @books.each do |b| %> 
    <%= b.title %><br /> 
<% end %> 

行動刪除行

@book = @author.books.build