Dowemo
0 0 0 0


Question:

I'm trying to render a the index view inside my ruby-on-rails application. How do a I render the index view, from inside a view passing an array of things to display? Using the 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? 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.

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.


Best Answer:


You'll need to use the render command, probably with a partial:

<%= 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:

#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.

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:

#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 keep some structure.

enter image description here

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.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs