2015-07-19 74 views
1

我正在使用回形針& Rails 4在訪問我的畫廊索引頁時出現此錯誤。在Rails 4中獲取未定義的方法錯誤

以下是錯誤: enter image description here

我有索引頁面的代碼是這樣的:

<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 

回答

1

首先

@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 
+0

你是怎麼知道一個屬性'has_many'畫廊的?只是好奇知道! –

+0

:)從模型名稱:屬性有一些圖像,將被保存爲我猜畫廊。其次是問題中的這一行。 '在那裏,有一個@galleries = Gallery.all,這當然是拉動與該物業相關的所有畫廊',你會看到相關顯示這種關係。 – Athar

+0

感謝Athar。 。 。所以我做了這些改變,但現在得到這個:「未定義的方法'畫廊'爲零:NilClass」 – zkidd

0

@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> 
+0

我只是改變了畫廊的畫廊在控制器和索引視圖,並仍然得到同樣的錯誤。 – zkidd

+0

@zkidd看到我更新的答案。 –

相關問題