Rails 3:通过 AJAX 分别检索每个关联的内容

发布于 2024-12-29 13:41:44 字数 1842 浏览 1 评论 0原文

我的系统设置了以下关联:

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

在我的 Book 模型的“显示”视图上,我想为用户提供一个选项(从下拉框中)选择他们想要查看的视图。例如,如果用户从此下拉框中选择“建议”,则部分内容将重新加载建议。其他 3 个选项也类似。 为了实现这一目标,我在模型 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

我尝试在 book_controller.rb 的“显示”操作中访问此方法,如下所示

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

:在 Show 视图中,我有以下形式来向此方法发出 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* 的部分中,我正在访问返回的值,如下所示:

- if !@book_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'

请帮助我明白如何解决这个问题。如果有正确且更好的方法来实现这一目标,请指导我。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2025-01-05 13:41:44

你的表单标签有 select_content_type 并且它说它是未定义的,但是你应该有类似 book_path(@book) 的东西

Your form tag has select_content_type and as it says it's undefined, but there you should have something like book_path(@book)

ι不睡觉的鱼゛ 2025-01-05 13:41:44

不是答案,但你的 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] 但你从不传入参数。您应该将此方法保留在控制器中,不确定为什么需要将其保留在模型中。

Not an answer, but your self.select_content_type method could be much DRYer:

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

Also, you use params[:page] but you never pass in params. You should just leave this method in the controller, not sure why it needs to be in the model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文