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