所以我有两个模特:艺术家和艺术品。 艺术家有一个:名称参数 艺术家has_many艺术品和艺术品has_one艺术家。
我不明白为什么当我在艺术品索引中打电话给@ artworks.artiste.name时我有这个错误:
未定义的方法`艺术家'
这是我的索引:
<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
答案 0 :(得分:0)
将has_one :artist
更改为belongs_to :artist
。原因是has_one不存储id,因此这两个模型实际上没有任何关联。 belongs_to
添加artist_id
字段。
答案 1 :(得分:0)
好的,我通过替换<h1><%= @artworks.artist.name %></h1>
<%= @artworks.first.artist %>
谢谢你