0

を通じて各協会個別にコンテンツを取得します。Railsの3:私のシステムは、以下の団体が設置しているAJAX

class Book < ActiveRecord::Base 
has_many : suggestions 
has_many : comments 
has_many : references 

を私の本モデルの表示ビューでは、私はユーザーが選択するオプションを与えたいです(ドロップボックスから)表示したいビューを選択します。たとえば、ユーザがこのドロップボックスからの提案を選択すると、部分的に提案が再ロードされます。他の3つのオプションについても同様です。私は「ショーでこのメソッドにアクセスしようとしています

def self.select_content_type(content_type) 
case content_type 

when "suggestions" 
    # get the suggestions 
    @book_suggestions = Book.suggestions.paginate(:per_page => 6, :page => params[:page]) 
    # return this 
    return @book_suggestions 

when "comments" 
    # get the comments 
    @book_comments = Book.comments.paginate(:per_page => 6, :page => params[:page]) 
    # return this 
    return @book_comments 

when "references" 
    # get the references 
    @book_references = Book.references.paginate(:per_page => 6, :page => params[:page]) 
    # return this 
    return @book_references 

エンド

エンド

: は、これを達成するために、私は、私のモデルにbook.rbを、以下の方法を書きました「私のbook_controller.rbのアクションは、次のように:

@book_content = Book.select_content_type(params[:content_type]) 

はを表示しますビュー、私は、このメソッドにGETリクエストを作成するには、以下の形式を持つ:

- form_tag book_path(@book), :id=>"select_rel_form", :remote => true, :method => 'get' do    
     = text_field_tag :content_type, params[:content_type], :id=>"select_rel_type" 
     = submit_tag "submit", :name => nil, :class=>"select_rel_submit" 

と部分*という名前_contentで* Iが値をアクセスしていますが、次のように返さ:

- if [email protected]_content.nil? 
    - @issue_relations.each do |relation| 
    ... 

は私が取得次のエラー:

NoMethodError (undefined method `suggestions' for #<Class:0x1177f4b8>): 
app/models/book.rb:93:in `select_content_type' 
app/controllers/books_controller.rb:21:in `show' 

どうすればこの問題を解決できるか理解できます。これを達成するための正しい、より良い方法があれば、私を案内してください。ありがとう。

+0

book.rbメソッドself.select_content_typeは決して動作しません。モデルのparams [:page]を参照していますが、その変数はありません。 paramsはコントローラ内にあり、どこにモデルに渡すのか分かりません。 – redronin

+0

@redronin、私はwill_paginateを使ってコンテンツをページ付けしています(一度に6つずつ表示します)。 params [:page]はwill_paginateパラメータです。 – rgoraya

答えて

0

あなたのformタグはselect_content_typeを持っており、それが言うように、それは未定義だが、そこにあなたがbook_pathのようなもの(@book)

+0

答えに感謝します。今度はフォームが必要に応じてModelメソッドにアクセスするようです。しかし、私は別のエラー(質問を更新)を取得しています。どういうわけか、.suggestionsの関連付けが正しく認識されていません。 – rgoraya

+0

あなたは '@book = Book.find(params [:id])'と '@book_comments = @ book.comments.paginate(:per_page =>)のようなものを持つべき' Book.comments'を持っているので、 6、:page => params [:page]) ' –

+0

あなたの答えは私が問題を見つけるのを助けてくれてありがとう。 – rgoraya

0

ない答えを持っている必要がありますが、あなたのself.select_content_type方法は非常に乾燥することができます

def self.select_content_type(content_type) 
    return unless ['suggestions', 'comments', 'references'].include?(content_type) 
    Book.send(content_type.to_sym).paginate(:per_page => 6, :page => params[:page]) 
end 

また、params [:page]を使用しますが、決してparamsを渡すことはありません。このメソッドをコントローラに残すだけで、なぜモデルに入れる必要があるのか​​分かりません。

+0

@redorin、私はそれをコントローラメソッドにし、* routes.rb *ファイルにルートを指定することをお勧めしますか?この場合、なぜそれがより良いものになるのか教えてください。 – rgoraya

関連する問題