在Rails 4中获取未定义的方法错误
我正在使用回形针& Rails 4在访问我的画廊索引页时出现此错误。在Rails 4中获取未定义的方法错误
以下是错误:
我有索引页面的代码是这样的:
<h3> for <%= Property.name %></h3>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @galleries.property.each do |gallery| %>
<tr>
<td><%= link_to 'Show', property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Edit', edit_property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Destroy', property_gallery_path(params[:property_id], gallery), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
和思考后,我的画廊控制器页是
class GalleriesController < ApplicationController
before_action :set_imageable
before_action :set_property, only: [:show, :edit, :update, :destroy]
before_action :set_gallery, only: [:show, :edit, :update, :destroy]
# GET /galleries
# GET /galleries.json
def index
@galleries = Gallery.all
end
# GET /galleries/1
# GET /galleries/1.json
def show
end
# GET /galleries/new
def new
@gallery = Gallery.new
end
# GET /galleries/1/edit
def edit
end
# POST /galleries
# POST /galleries.json
def create
@gallery = @imageable.galleries.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 @imageable, notice: 'Gallery was successfully created.' }
format.json { render :show, status: :created, location: @gallery }
else
format.html { render :new }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /galleries/1
# PATCH/PUT /galleries/1.json
def update
respond_to do |format|
if @gallery.update(gallery_params)
format.html { redirect_to property_gallery_path, notice: 'Gallery was successfully updated.' }
format.json { render :show, status: :ok, location: @gallery }
else
format.html { render :edit }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
# DELETE /galleries/1
# DELETE /galleries/1.json
def destroy
@gallery.destroy
respond_to do |format|
format.html { redirect_to galleries_url, notice: 'Gallery was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gallery
@gallery = Gallery.find(params[:id])
end
def set_property
@property = Property.find(params[:id])
end
def set_imageable
klass = [Property, Space].detect { |c| params["#{c.name.underscore}_id"]}
puts "Klass is #{klass.inspect}"
@imageable = klass.find(params["#{klass.name.underscore}_id"])
puts "Imageable is #{@imageable.inspect}"
end
# Never trust parameters from the scary internet, only allow the white list through.
def gallery_params
params.permit(:gallery)
end
end
现在关于它,我去了我的画廊控制器,并查看了索引行。在那里,有一个@galleries = Gallery.all,这当然是拉动与该物业有关的所有画廊。我知道它拉动了与属性相关的所有画廊,因为我有before_action:set_property和before_action:set_gallery。
我想知道如果没有方法错误必须如何设置属性。我不确定。但我的请求参数中我的property_id被设置为“1”。所以价值正在传递。
任何明显的东西我失踪?而且我一般都在想这是正确的方式?
这里是我的整个galleries_controller.rb
class GalleriesController < ApplicationController
before_action :set_imageable
before_action :set_property, only: [:index, :show, :edit, :update, :destroy]
before_action :set_gallery, only: [:show, :edit, :update, :destroy]
# GET /galleries
# GET /galleries.json
def index
@galleries = @property.galleries
end
# GET /galleries/1
# GET /galleries/1.json
def show
end
# GET /galleries/new
def new
@gallery = Gallery.new
end
# GET /galleries/1/edit
def edit
end
# POST /galleries
# POST /galleries.json
def create
@gallery = @imageable.galleries.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 @imageable, notice: 'Gallery was successfully created.' }
format.json { render :show, status: :created, location: @gallery }
else
format.html { render :new }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /galleries/1
# PATCH/PUT /galleries/1.json
def update
respond_to do |format|
if @gallery.update(gallery_params)
format.html { redirect_to property_gallery_path, notice: 'Gallery was successfully updated.' }
format.json { render :show, status: :ok, location: @gallery }
else
format.html { render :edit }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
# DELETE /galleries/1
# DELETE /galleries/1.json
def destroy
@gallery.destroy
respond_to do |format|
format.html { redirect_to galleries_url, notice: 'Gallery was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gallery
@gallery = Gallery.find(params[:id])
end
def set_property
@property = Property.find(params[:id])
end
def set_imageable
klass = [Property, Space].detect { |c| params["#{c.name.underscore}_id"]}
puts "Klass is #{klass.inspect}"
@imageable = klass.find(params["#{klass.name.underscore}_id"])
puts "Imageable is #{@imageable.inspect}"
end
# Never trust parameters from the scary internet, only allow the white list through.
def gallery_params
params.permit(:gallery)
end
end
首先
@galleries = Gallery.all
,如果你想获取画廊将返回所有的画廊不论财产
对于任何财产做到这一点
@galleries = @property.galleries
其次,你无法获取来自画廊,画廊单一的财产是你在这里做
<% @galleries.property.each do |gallery| %>
你需要做的
<% @galleries.each do |gallery| %>
<%property = gallery.property %>
#what you want to do
<% end %>
也更新before_action数组
before_action :set_property, only: [:index, :show, :edit, :update, :destroy]
并改变这个
def set_property
@property = Property.find(params[:id])
end
到
def set_property
@property = Property.find(params[:property_id])
end
@galleries
是复数 - 基本上是画廊对象的数组,所以你不能直接调用一个方法。您需要遍历每个对象,然后调用单个对象的方法。
可以实现以下方式类似的事情:
<tbody>
<% @galleries.each do |gallery| %>
<tr>
<td><%= link_to 'Show', property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Edit', edit_property_gallery_path(params[:property_id], gallery) %></td>
<td><%= link_to 'Destroy', property_gallery_path(params[:property_id], gallery), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
我只是改变了画廊的画廊在控制器和索引视图,并仍然得到同样的错误。 – zkidd
@zkidd看到我更新的答案。 –
你是怎么知道一个属性'has_many'画廊的?只是好奇知道! –
:)从模型名称:属性有一些图像,将被保存为我猜画廊。其次是问题中的这一行。 '在那里,有一个@galleries = Gallery.all,这当然是拉动与该物业相关的所有画廊',你会看到相关显示这种关系。 – Athar
感谢Athar。 。 。所以我做了这些改变,但现在得到这个:“未定义的方法'画廊'为零:NilClass” – zkidd