简体   繁体   English

如何从ruby-on-rails中的视图内部呈现索引视图

[英]How to render the index view, from inside a view in ruby-on-rails

I'm trying to render a the index view inside my ruby-on-rails application. 我正在尝试在我的ruby-on-rails应用程序中呈现一个索引视图。 How do a I render the index view, from inside a view passing an array of things to display? 我如何通过传递要显示的事物数组的视图内部呈现索引视图? Using the link_to. 使用link_to。

I do not want to re-route to the controller, I just want to link_to a view passing it the variables it needs, how can I do this? 我不想重新路由到控制器,我只想将link_to一个视图传递给它所需的变量,我该怎么做? EDIT: I am trying to create a page type functionality in the index of my article model. 编辑:我试图在我的文章模型的索引中创建页面类型的功能。 So I have around 400 articles for example, and when the index action in the article controller is called, it of course renders the index view which is calling a partial for each article in the '@articles array' passed on by article controller's index action. 因此,举例来说,我大约有400篇文章,并且当在article控制器中调用index动作时,它当然会呈现索引视图,该视图调用由article控制器的index动作传递的“ @articles数组”中每篇文章的局部视图。

So in the view, I'm trying to do something like: 因此,在视图中,我正在尝试执行以下操作:

    <% count = 0 %>
    <% @articles.each do |article| %>
      <% if count <10 %>
        <%= render partial: 'index_articles', locals: {article: article} %>
        <% count = count + 1 %>
      <% end %>
    <% end %>
    <% @articles = @articles.drop(10) %>
    <% if @articles.any? %>
      <%= link_to "Next", 'articles', locals: {@articles => @articles} %>
    <% end %>

Thank you in advanced for all of your help. 在此先感谢您的所有帮助。

You'll need to use the render command , probably with a partial : 您将需要使用render命令可能需要部分使用

<%= render "controller/index", collection: ["your", "array"], as: :object_name %>

You will have to call a controller action to generate this. 您将必须调用控制器动作来生成此动作。 You cannot simply load it on your screen, unless it was preloaded inside your javascript for something: 您不能简单地将其加载到屏幕上,除非它已在javascript中预加载了某些内容:

#View
<%= link_to "Index", controllers_path(ids: ["1","2"]), remote: true %>

#app/controllers/your_controller.rb
class YourController < ApplicationController
   def index
      @posts = request.xhr? Post.find(params[:ids]) : Post.all
      respond_to do |format|
         format.js  #-> app/views/controller/index.js.erb
         format.html
      end
   end
end

#app/views/controller/index.js.erb
$(".element").html("<%=j render 'index' %>");

There are several issues with this approach... 这种方法存在几个问题...


Flow

First of all, your flow of your app should be as structured as possible. 首先,您的应用程序流程应尽可能结构化。

In short, if you're calling the index view inside another action, it's not the index view any more. 简而言之,如果您要在另一个动作中调用index视图,则不再是索引视图。

What you should look at is how to use a partial in your app: 您应该看的是如何在您的应用程序中使用部分代码:

#app/controller/views/_partial.html.erb
<%= post.title %>

This way, you can adapt your index view and your other page to use the partial in their respective action layouts: 这样,您可以调整index视图和其他页面,以在各自的操作布局中使用局部视图:

#app/controller/views/index.html.erb
<%= render "partial", collection: @posts, as: :post %>

This will allow you to "reuse" code much in the way you want. 这将使您可以按照自己的方式“重用”代码。 This will be much more appropriate than trying to invoke other action/views. 这比尝试调用其他操作/视图要合适得多。

- -

Resources 资源

Secondly, you'll want to look at how your app functions. 其次,您需要查看您的应用程序如何运行。

Your index view is meant to show all the items for a particular object. 您的索引视图旨在显示特定对象的所有项目。 Whilst you're free to change this as you want, the fact remains that you have to k eep some structure . 尽管您可以随意更改它,但事实仍然是您必须保持某种结构

在此处输入图片说明

You should read up on the routes for your actions, and how they're meant to work in your application. 您应该阅读操作路线,以及它们在应用程序中的作用方式。 This will give you some perspective on the resourceful nature of Rails routes, and how you'll have to call specific routes with specific actions. 这将使您对Rails路由的resourceful性质以及如何使用特定操作调用特定路由有一些看法。

Your problem is probably that the file needs to be named _index.html.erb. 您的问题可能是该文件需要命名为_index.html.erb。 You can have another file named index.html.erb which just renders _index.html.erb. 您可以拥有另一个名为index.html.erb的文件,该文件仅呈现_index.html.erb。

If you need a full guide on using AJAX, look up the railscast. 如果您需要使用AJAX的完整指南,请查阅railscast。 If you're not using AJAX and you just want to render it, then you don't use link_to. 如果您不使用AJAX,而只想渲染它,则不要使用link_to。 You just do <%= render :index %>. 您只需要<%= render:index%>。

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

相关问题 Ruby-on-Rails:如何在 Rails 视图中显示所有“rake:about”数据? - Ruby-on-Rails : how to display all “rake:about” data inside a Rails view? 在视图中动态更改Ruby-on-Rails变量 - Dynamically changing Ruby-on-Rails variables in a view 如何从其在ruby-on-rails中的索引中获取枚举值? - How to get enum value from its index in ruby-on-rails? Ruby on Rails:如何从相同的控制器渲染具有不同路线的视图? - Ruby on Rails: how to render a view from same controller but with a different route? 如何在 ruby​​ on rails 视图中呈现嵌套 json 对象的内容 - How to render content from nested json object in ruby on rails view Ruby-on-rails 测试在呈现 JS 视图时引发 InvalidCrossOriginRequest - Ruby-on-rails test raising InvalidCrossOriginRequest when rendering a JS view 在Ruby-on-Rails上,可以从控制器传递变量以通过response_to查看吗? - On Ruby-on-Rails it's possible to pass variables from controller to view through respond_to? 如何向授权查看帖子的用户发送电子邮件 - 多态关联 Ruby-on-Rails - How sending emails to users authorised to view a post - polymorphic association Ruby-on-Rails 从Rails中的更新方法渲染索引视图? - Render index view from update method in Rails? Ruby-on-Rails:渲染帮助:layout =&gt; false - Ruby-on-Rails: Help with render: layout => false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM