【发布时间】:2014-06-21 01:56:20
【问题描述】:
我无法访问图片模型设置,例如:
请求has_one图库has_many图片
从 Request 模型的显示视图中,我需要访问其 Gallery 的图片,它给了我错误 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,它应该会为我提供请求图库的所有图片。我讨厌发布这么多代码,但我对此很陌生,不想遗漏任何东西。有什么想法吗?
【问题讨论】:
-
您收到该错误的原因是因为
@request.gallery返回 null,这意味着您的request没有与之关联的gallery。 -
你知道我能做些什么来解决这个问题吗?当我创建一个请求时,我以为我的请求控制器正在创建它。
标签: ruby-on-rails activerecord ruby-on-rails-4 actioncontroller