简体   繁体   English

Rails:如何从另一个控制器呈现部分视图

[英]Rails: How to render partial view from another controller

I am making a reddit clone.我正在制作一个 reddit 克隆。 Right now I have a voting system that works, but it refreshes the page each time a user votes.现在我有一个有效的投票系统,但每次用户投票时它都会刷新页面。 I want to make this async by using AJAX.我想通过使用 AJAX 使这个异步。

Making buttons respond to Jquery is really hard though because they change state depending on how a user has voted on a post.让按钮响应 Jquery 真的很难,因为它们会根据用户对帖子的投票方式改变状态。 The solution I came up with is this: Have 2 upvote buttons in the _upvote partial, and have 2 downvote buttons in the _downvote partial.我想出的解决方案是:在 _upvote 部分中有 2 个 upvote 按钮,在 _downvote 部分中有 2 个 downvote 按钮。 Each partial will have one voting button that is activated when the case is already true, and clicking on it again will destroy it (for example, if a post is already upvoted on, then reclicking the upvote button will destroy it).每个部分都将有一个投票按钮,当案例已经为真时会激活该按钮,再次单击它会破坏它(例如,如果帖子已经被点赞,则再次单击点赞按钮将破坏它)。 The other button(s) in the partial will be activated in the case that the button has not been used yet (in which case clicking the button will create it).如果尚未使用该按钮(在这种情况下单击该按钮将创建它),则部分中的其他按钮将被激活。

Example: ****The _upvote partial should have the following buttons in it:****示例:****_upvote 部分中应包含以下按钮:****

1)create_upvote 1)create_upvote

2)destroy_upvote 2)destroy_upvote

****The downvote partial should have the following buttons in it:**** ****downvote 部分应包含以下按钮:****

1)create_downvote 1)create_downvote

2)destroy_downvote 2)destroy_downvote

Whichever buttons are activated will depend on the controller.激活哪个按钮取决于控制器。

My question(s) are this:我的问题是这样的:

1) What is the actual syntax to a render partial view from another controller? 1)从另一个控制器渲染局部视图的实际语法是什么? For example: posts is rendering _post, and within that I want _upvote or _downvote to be rendered.例如:posts 正在渲染 _post,在其中我希望 _upvote 或 _downvote 被渲染。 How to render _upvote or _downvote from the controller.如何从控制器渲染 _upvote 或 _downvote。 Again, the reason I WANT to render these partials from a controller is because the controller will be changing their state.同样,我想从控制器渲染这些部分的原因是因为控制器将改变它们的状态。

Downvotes partial反对部分

app/views/posts/_downvote.html.erb app/views/posts/_downvote.html.erb

_downvotes 部分

Upvotes partial赞成部分

app/views/posts/_downvote.html.erb app/views/posts/_downvote.html.erb

_upvotes 部分

Posts partial帖子部分

app/views/posts/_post.html.erb app/views/posts/_post.html.erb

_post 部分

Downvotes controller反对票控制器

反对票控制器

Upvotes controller点赞控制器

点赞控制器

Line 9 in the controllers is where my syntax is off!控制器中的第 9 行是我的语法关闭的地方! What is the syntax to render _upvote and _downvote partials, within the _posts partial?在 _posts 部分中呈现 _upvote 和 _downvote 部分的语法是什么? Thanks mates!谢谢小伙伴们!

As far as syntax goes, what you want is the following:就语法而言,您想要的是以下内容:

render partial: "posts/upvote"

Instead of sending html data (the buttons you mentioned) from controller to the partial, you should have a conditional statement in each partial that activates one button or another.不是将 html 数据(您提到的按钮)从控制器发送到部分,您应该在每个部分中都有一个条件语句来激活一个或另一个按钮。 From there create event handlers for each respective button.从那里为每个相应的按钮创建事件处理程序。 That takes care of the issue of Jquery handling internal state.这就解决了 Jquery 处理内部状态的问题。

Just a small note for the special cast of partials inside views/layouts/_upvote.html.erb , you should use对于views/layouts/_upvote.html.erb内的特殊部分的特殊演员views/layouts/_upvote.html.erb ,您应该使用

render partial: "layouts/upvote"

(not application/upvote ) (不是application/upvote

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM