Ruby on rails 如何将第一个嵌套对象放入第二级嵌套对象控制器?

Ruby on rails 如何将第一个嵌套对象放入第二级嵌套对象控制器?,ruby-on-rails,partial-views,Ruby On Rails,Partial Views,我有一个角色模型,有一个显示页面。在show页面上,我有一个通过partial动态生成的注释循环。在那部分评论中,我有另一部分投票,其中包含投票按钮。当然,我想允许对评论进行投票 我不确定如何将注释对象放入投票控制器(或VotesController模块,取决于实现)以创建投票 将角色对象id获取到投票控制器非常简单,因为实际视图是角色显示页面,但获取特定注释的id是从局部生成的,通过单击嵌套在评论部分中的部分中的投票按钮,部分将导致我在访问该评论的语法上留下空白 (我使用acts_as_vot

我有一个角色模型,有一个显示页面。在show页面上,我有一个通过partial动态生成的注释循环。在那部分评论中,我有另一部分投票,其中包含投票按钮。当然,我想允许对评论进行投票

我不确定如何将注释对象放入投票控制器(或VotesController模块,取决于实现)以创建投票

将角色对象id获取到投票控制器非常简单,因为实际视图是角色显示页面,但获取特定注释的id是从局部生成的,通过单击嵌套在评论部分中的部分中的投票按钮,部分将导致我在访问该评论的语法上留下空白

(我使用acts_as_votable进行投票,使用acts_as_commentable进行评论。)


app/views/characters/show.html.haml

= render partial: 'comments/comment', collection: @comments, as: :comment
.comment{ :id => "comment-#{comment.id}" }
  %hr

  = render partial: 'votes/vote_comment'

  %h4
    #comment body
.vote-comment-buttons
  = link_to image_tag("upvote.png"), votes_upvote_path(), method: :post, remote: true
  = link_to image_tag("downvote.png"), votes_downvote_path(), method: :post, remote: true
VotesController < ApplicationController

  def upvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

  def downvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

end
app/views/comments/_-form.html.haml

= render partial: 'comments/comment', collection: @comments, as: :comment
.comment{ :id => "comment-#{comment.id}" }
  %hr

  = render partial: 'votes/vote_comment'

  %h4
    #comment body
.vote-comment-buttons
  = link_to image_tag("upvote.png"), votes_upvote_path(), method: :post, remote: true
  = link_to image_tag("downvote.png"), votes_downvote_path(), method: :post, remote: true
VotesController < ApplicationController

  def upvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

  def downvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

end
app/views/vots/\u vote\u comment.html.haml

= render partial: 'comments/comment', collection: @comments, as: :comment
.comment{ :id => "comment-#{comment.id}" }
  %hr

  = render partial: 'votes/vote_comment'

  %h4
    #comment body
.vote-comment-buttons
  = link_to image_tag("upvote.png"), votes_upvote_path(), method: :post, remote: true
  = link_to image_tag("downvote.png"), votes_downvote_path(), method: :post, remote: true
VotesController < ApplicationController

  def upvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

  def downvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

end
app/controllers/vows.html.haml

= render partial: 'comments/comment', collection: @comments, as: :comment
.comment{ :id => "comment-#{comment.id}" }
  %hr

  = render partial: 'votes/vote_comment'

  %h4
    #comment body
.vote-comment-buttons
  = link_to image_tag("upvote.png"), votes_upvote_path(), method: :post, remote: true
  = link_to image_tag("downvote.png"), votes_downvote_path(), method: :post, remote: true
VotesController < ApplicationController

  def upvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

  def downvote
    # Need the specific comment or comment id whose vote button was clicked.
  end

end
VotesController
好吧,以下是一些基本提示:

  • 您不能通过HTTP传递ruby对象,但可以传递其中的
    id
    type
    来在控制器中构建它们
  • 即使键入类似于
    comment\u path(comment)
    的内容,也只会将该注释的
    id
    传递给您的操作。通过观察您的操作代码(它应该包含类似于
    Comment.find(params[:id])
    )的内容)可以轻松地检查这一点
  • 传递任何所需的额外参数,只需将它们提供给路由助手即可,如:
    some\u voting\u path(commentable\u id:14,commentable\u type:'character')
  • 您可以使用
    params['commentable\u type']
    或随URL传递的任何值访问操作中的参数。如果您遵循传递
    id
    type
    方法,您应该能够执行一些元编程:

    def upvote_method
      model = params[:commentable_type].camelize.constantize # => e.g., Post
      object = model.find(params[:commentable_id]) # => post object
      # here goes your inner logics
    end
    
  • 请注意,如果您使用
    GET
    方法发送请求,这些参数将显示在您的浏览器URL中。但是,此处不应使用
    GET
    ,因为投票会更改数据库中对象的状态