嵌套属性在 rails 4 中不起作用

nested attributes not working in rails 4

所以我有两个模型:艺术家和艺术品。 艺术家有一个 :name 参数 艺术家 have_many 艺术作品和艺术作品 has_one 艺术家。

我不明白为什么我在作品索引中调用@artworks.artiste.name时出现这个错误:

undefined method `artist'

这是我的索引:

<div class="col-xs-12 col-md-6 col-xs-offset-0 col-md-offset-4 col-lg-offset-5">
          <p id="notice"><%= notice %></p>
          <h1><%= @artworks.artist.name %></h1>
            <% @artworks.each do |artwork| %>
            <div class="artwork-container">
              <div class="artwork-info">
                <span><%= artwork.title %></span>
                <p><%= artwork.description %></p>
                <span><%= artwork.price %></span>
                <span><%= link_to 'Edit', edit_artist_artwork_path(:artist, artwork) %></span>
                <span><%= link_to 'Destroy', artist_artwork_path(:artist, artwork), method: :delete, data: { confirm: 'Are you sure?' } %>
                </span>
              </div>
              <div class="artwork-photo-container">
              <div class="artwork-photo" style="background-image: url('<%= artwork.photo.url %>');"></div>
              </div>         
           </div>
        <% end %> 
      <br>
   <%= link_to 'New Artwork', new_artist_artwork_path %>
</div>

这是我的艺术控制器:

class ArtworksController < ApplicationController
  before_action :set_artwork, only: [:show, :edit, :update, :destroy]

  # GET /artworks
  # GET /artworks.json
  def index
    @artworks = Artwork.all
  end

  # GET /artworks/1
  # GET /artworks/1.json
  def show
    @artwork = Artwork.find(params[:id])
  end

  # GET /artworks/new
  def new
    @artwork = Artwork.new
  end

  # GET /artworks/1/edit
  def edit
  end

  # POST /artworks
  # POST /artworks.json
  def create
    @artwork = Artwork.new(artwork_params)

    respond_to do |format|
      if @artwork.save
        format.html { redirect_to artist_artwork_url(:artist, @artwork), notice: 'Artwork was successfully created.' }
        format.json { render :show, status: :created, location: artist_artwork_url(:artist, @artwork) }
      else
        format.html { render :new }
        format.json { render json: @artwork.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /artworks/1
  # PATCH/PUT /artworks/1.json
  def update
    respond_to do |format|
      if @artwork.update(artworks_params)
        format.html { redirect_to artist_artworks_url(:artist, @artworks), notice: 'Artwork was successfully updated.' }
        format.json { render :show, status: :ok, location: artist_artwork_url(:artist, @artworks) }
      else
        format.html { render :edit }
        format.json { render json: @artwork.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /artworks/1
  # DELETE /artworks/1.json
  def destroy
    @artwork.destroy
    respond_to do |format|
      format.html { redirect_to artist_artworks_url(:artist, @artworks), notice: 'Artwork was successfully destroyed.' }
      format.json { head :no_content }
    end
  end



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

    # Never trust parameters from the scary internet, only allow the white list through.
    def artwork_params
      params.require(:artwork).permit(:title, :description, :ref, :dateof, :price, :stock, :front, :photo, artist_attributes: [ :name, :surname])
    end
end

这是我的 2 个模型:

class Artist < ActiveRecord::Base
    has_many :artworks
end

class Artwork < ActiveRecord::Base
    has_one :artist
    accepts_nested_attributes_for :artist
    has_attached_file :photo, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    validates_attachment_presence :photo
    validates_presence_of :title, :description, :stock, :front, :price
end

has_one :artist 更改为 belongs_to :artist。原因是 has_one 不存储 id,因此这两个模型实际上没有任何关联。 belongs_to 添加一个 artist_id 字段。

好的,所以我通过替换 <h1><%= @artworks.artist.name %></h1>

解决了错误

来自 <%= @artworks.first.artist %>

谢谢你