我正在使用回形針& 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
你是怎麼知道一個屬性'has_many'畫廊的?只是好奇知道! –
:)從模型名稱:屬性有一些圖像,將被保存爲我猜畫廊。其次是問題中的這一行。 '在那裏,有一個@galleries = Gallery.all,這當然是拉動與該物業相關的所有畫廊',你會看到相關顯示這種關係。 – Athar
感謝Athar。 。 。所以我做了這些改變,但現在得到這個:「未定義的方法'畫廊'爲零:NilClass」 – zkidd