2014-09-21 57 views
0

我正在構建一個ActiveAdmin的簡單設置,以便在RoR中使用基本管理編碼工作。沒有什麼花哨。我生成了一個「後」控制器和資源和模型,如下所示。我的問題是,我仍然得到以下錯誤:什麼是「未定義的方法`rails」「,我該如何解決它?

NoMethodError in Admin::PostsController#index 
undefined method `rails' for #<Admin::PostsController:0x00000107ad3038> 

這是正確的,我沒有定義的rails方法,但我也沒有叫一個。事實上,我的任何代碼中都沒有調用任何方法的rails方法。這導致我相信我有一個語法問題,但我似乎無法找到它。請幫我理解這一點。


/admin/post.rb

ActiveAdmin.register Post do 

    scope_to :rails 

    permit_params :title, :slug, :blurb, :content, :category_id 

    index do 
    column :title 
    column :slug 
    column :blurb 
    column :created_at 
    actions 
    end 

    form :html => {:enctype => "multipart/form-data"} do |f| 
    f.inputs "Details" do 
     f.input :title 
     f.input :slug 
     f.input :blurb 
     f.input :category 
     f.input :content, :as => :text 
    end 

    f.inputs "Images" do 
     f.input :image, :label => 'Post Image', :as => :file 
    end 
    f.actions 
    end 

end 

/controllers/posts_controller.rb

class PostsController < ApplicationController 

    def index 
    @posts = Post.all 
    end 

    def show 
    @post = Post.find(params[:id]) 
    end 

end 

/models/post.rb

class Post < ActiveRecord::Base 

    belongs_to :category 
    scope :rails, -> { where(category_id: 1) } 

end 

正如你所看到的,我從來不叫我的代碼中任何rails方法。那個錯誤來自哪裏?

回答

1

這個錯誤可能是從該行即將在視圖管理/ post.rb:

scope_to :rails 

嘗試評論了這一點。

然後嘗試這樣的事情,而不是:

ActiveAdmin.register Post do 
    controller do 
    def resource 
     Post.where(category_id: 1) 
    end 
    end 
end 
+0

註釋掉'scope_to:rails'線工作就像一個魅力!爲什麼這個工作?你添加的控制器塊是什麼? – elersong 2014-09-21 22:16:41

+0

好吧,您已將範圍限制爲您的模型中的category_id爲1。我不知道你爲什麼這樣做。我只是給了你一些代碼來完成這個工作。 – manishie 2014-09-21 22:18:57

+0

我想你剛剛從某處複製了這段代碼。所以如果你不需要修改範圍,只需要去掉它。 – manishie 2014-09-21 22:19:31

相關問題