My system has the following associations set up:
class Book < ActiveRecord::Base
has_many : suggestions
has_many : comments
has_many : references
On the Show view of my Book model, I want to give user an option to choose (from a drop box) the view they would like to see. So for example if a user selects suggestions from this drop box then the partial reloads with the suggestions. Similarly for the other 3 options.
In order to achieve this, I wrote the following method in my Model, book.rb:
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
end
end
I am trying to access this method in the “Show” action of my book_controller.rb as follows:
@book_content = Book.select_content_type(params[:content_type])
In the Show view, I have the following form to make a get request to this method:
- 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"
And in a partial named *_content* I am accessing the value returned as follows:
- if !@book_content.nil?
- @issue_relations.each do |relation|
...
I get the following error:
NoMethodError (undefined method `suggestions' for #<Class:0x1177f4b8>):
app/models/book.rb:93:in `select_content_type'
app/controllers/books_controller.rb:21:in `show'
Kindly help me understand how I can fix this. If there’s a correct and better way to achieve this, please guide me. Thanks.
Your form tag has select_content_type and as it says it’s undefined, but there you should have something like book_path(@book)