导轨 - 如何在Rails控制器和JavaScript刺激控制器之间传递数据以动态更改视图
我有一个Rails应用程序,其中有一个搜索结果页面,该应用程序根据用户的输入从表单中提取结果。
为了简化导轨控制器方法:
def search_results
# [...] ActiveRecord queries to get results
@movies.shuffle!
end
然后,在视图中,我只想显示前三个结果。目前,这是类似的:
<%@movies[0..2].each do |movie|%>
<div class="row d-flex justify-content-center" data-controller="search-result-card">
<%# Visual of the card %>
</div>
<%end%>
我想做的是在此页面上有一个刷新按钮,可以用接下来的三个@movies替换可见的卡,但实际上没有实际重新加载页面。
我已经制定了一个刷新按钮,并且上面有一个数据控制器...但是我不确定如何使用它来更改代码。
<div class="refresh-button" data-controller="refresh">
<button data-action="click->refresh#next">Refresh</button>
</div>
我不确定我是否需要创建卡片的部分...或者是否有一种方法可以简单地更改嵌入式Ruby,以便&lt;%@Movie [0..2]。 |%&gt;
变为&lt;%@tover [3..5]。
我认为渲染并不是可行的,然后隐藏所有结果是可行的,因为可能有成千上万的结果(尽管我可能是错误的,这可能是可以的)。
任何最赞赏的建议!
I have a Rails app where I have a search results page, which pulls results based on a user's input from a form.
To simplify the Rails controller method:
def search_results
# [...] ActiveRecord queries to get results
@movies.shuffle!
end
Then in the view, I only want to display the first three results. At the moment, that's something like:
<%@movies[0..2].each do |movie|%>
<div class="row d-flex justify-content-center" data-controller="search-result-card">
<%# Visual of the card %>
</div>
<%end%>
What I want to do is have a refresh button on this page replaces the visible cards with the next three @movies, but without actually reloading the page.
I've drawn up a refresh button and I've got a data-controller on it... but I'm just not sure how I can use this to change the code.
<div class="refresh-button" data-controller="refresh">
<button data-action="click->refresh#next">Refresh</button>
</div>
I'm not sure whether I need to create a partial of the card... or if there's a way to simply change the imbedded Ruby so that <%@movies[0..2].each do |movie|%>
becomes <%@movies[3..5].each do |movie|%>
and have the view update accordingly.
I don't think that rendering and then hiding all of the results would be feasible, because there could be tens of thousands of them potentially (although I could be wrong and this might be fine to do).
Any suggestions most appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来像是朴素的分页。
如果可能有成千上万的电影,您将成为成千上万部电影的《标记/HTML》。通过分页,您会一次获得3个记录。甚至可以使用涡轮框架来替换接下来的3个。
但是,如果您真的想做自己的想法,则除了前3张外,还将隐藏所有卡,然后使用刺激来获取下一张或最后三张卡。
测试示例
控制器
使用Slim
刺激控制器模板
只是一个粗略的示例。
编辑在JavaScript上不是很好,并清理了刺激控制器,添加了可变数量的元素以显示和使用页面而不是元素。
This sounds like plain old pagination.
If there could be thousands of movies you'd be generation markup/html for thousands of movies. With pagination you'd get get 3 records at a time. Could even use a turbo-frame to replace the next 3.
But if you really want to do what your thinking, you'd have hide all cards except for the first 3, then use stimulus to get the next or last three cards.
A test example
Controller
Template using slim
Stimulus Controller
Just a rough example.
EDIT Not very good at javascript and cleaned up the stimulus controller, added variable number of elements to display and work with pages instead of elements.