を通じて各協会個別にコンテンツを取得します。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'
どうすればこの問題を解決できるか理解できます。これを達成するための正しい、より良い方法があれば、私を案内してください。ありがとう。
book.rbメソッドself.select_content_typeは決して動作しません。モデルのparams [:page]を参照していますが、その変数はありません。 paramsはコントローラ内にあり、どこにモデルに渡すのか分かりません。 – redronin
@redronin、私はwill_paginateを使ってコンテンツをページ付けしています(一度に6つずつ表示します)。 params [:page]はwill_paginateパラメータです。 – rgoraya