我无法访问图片模型设置,如:
请求has_one
图库has_many
图片
从请求模型的展示视图中,我需要访问其图库的图片,并且它会向我显示错误undefined method 'pictures' for nil:NilClass
。
型号:
class Request < ActiveRecord::Base
belongs_to :user
has_one :gallery
has_many :pictures, through: :gallery
end
class Gallery < ActiveRecord::Base
has_many :pictures, :dependent => :destroy
belongs_to :request
end
class Picture < ActiveRecord::Base
belongs_to :gallery
has_attached_file :image,
:path => ":rails_root/public/images/:id/:filename",
:url => "/images/:id/:filename"
do_not_validate_attachment_file_type :image
end
请求控制器:
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
def show
@request = Request.find(params[:id])
@gallery = @request.gallery
@pictures = @gallery.pictures
end
def new
@request = Request.new
@gallery = Gallery.new(params[:request_id])
@gallery = @request.gallery
@pictures = @gallery.pictures
end
def create
@request = Request.new(request_params)
respond_to do |format|
if @request.save
format.html { redirect_to @request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: @request }
else
format.html { render :new }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:title, :description, :username, gallery_attributes: [:id, :name, :description]).merge(user_id: current_user.id)
end
end
图库控制器:
class GalleriesController < ApplicationController
def show
@gallery = Gallery.find(params[:id])
@pictures = @gallery.pictures
respond_to do |format|
format.html # show.html.erb
format.json { render json: @gallery }
end
end
def new
@gallery = Gallery.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @gallery }
end
end
def create
@gallery = Gallery.new(gallery_params)
respond_to do |format|
if @gallery.save
if params[:images]
params[:images].each { |image|
@gallery.pictures.create(image: image)
}
end
format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
format.json { render json: @gallery, status: :created, location: @gallery }
else
format.html { render action: "new" }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
def gallery_params
params.require(:gallery).permit(:name, :description, :cover, :token)
end
end
图片控制器:
class PicturesController < ApplicationController
def show
@picture = Picture.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @picture }
end
end
def new
@gallery = Gallery.find(params[:gallery_id])
@picture = @gallery.pictures.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @picture }
end
end
def create
@picture = Picture.new(params[:picture])
if @picture.save
respond_to do |format|
format.html {
render :json => [@picture.to_jq_upload].to_json,
:content_type => 'text/html',
:layout => false
}
format.json {
render :json => [@picture.to_jq_upload].to_json
}
end
else
render :json => [{:error => "custom_failure"}], :status => 304
end
end
请求展示视图 (缩短)
<%- model_class = Request -%>
<% unless @pictures.empty? %>
<% @pictures.each do |pic| %>
<li class="span3" id="picture_<%= pic.id %>">
<div class="thumbnail">
<%= image_tag pic.image.url %>
<div class="caption">
</div>
</div>
</li>
<% end %>
<% end %>
在请求展示视图中,我试图访问@pictures,它应该会向我提供请求图库的所有图片。我讨厌发布这么多代码,但我是新手,不想留下任何东西。有什么想法吗?
答案 0 :(得分:1)
这部分有很多问题:
def show
@request = Request.find(params[:id])
@gallery = @request.gallery
@pictures = @gallery.pictures
end
def new
@request = Request.new
@gallery = Gallery.new(params[:request_id])
@gallery = @request.gallery
@pictures = @gallery.pictures
end
在show
中,您需要检查请求是否包含图库(如果您未在关联上添加验证,则可以执行以下操作:validates_presence_of :gallery
模型中的Request
。另一种方法是添加零检查:
def show
@request = Request.find(params[:id])
unless @request.gallery.nil?
@gallery = @request.gallery
@pictures = @gallery.pictures
end
end
但是如果您期望这个模型,请添加关联:
class Request < ActiveRecord::Base
belongs_to :user
has_one :gallery
validates_presence_of :gallery
has_many :pictures, through: :gallery
end
同样在新动作中,只需创建@request
,就没有与之关联的图库。你定义@gallery
2次,我不知道原因。你可以建立它:
def new
@request = Request.new
@gallery = @request.build_gallery
@pictures = @gallery.pictures
end
我不明白什么是params[:request_id]
。如果它是新的请求您已经拥有什么ID?
<强>更新强>
正如我现在所知,您只需要更改new
操作。
def new
@request = Request.new
end