How Rails render finds variable - ruby-on-rails

Simple example:
index.html.erb:
<% #posts.each do |post| %>
<%= post.title %>
<% end %>
I can refactor this and move content to the _post.html.erb partial:
index.html.erb:
<%= render #posts %>
_post.html.erb:
<%= post.title %>
So how Rails passes attributes of every post without creating a block?
My posts_controller index action has only #posts = Post.all defined.
I thought that maybe by the name of the partial (post), but looks like its another Rails convention (plural and singular naming). Is it?
Many thanks for sharing!

Rails determines the name of the partial to use by looking at the model name in the collection. The full syntax to render a collection is as follows:
<%= render partial: "post", collection: #posts %>
However there is a shorthand for this:
<%= render #posts %>
According to docs:
When a partial is called with a pluralized collection, then the
individual instances of the partial have access to the member of the
collection being rendered via a variable named after the partial. In
this case, the partial is _post, and within the _post partial, you can
refer to post to get the instance that is being rendered.
You may find detailed explanation in rails guides

Related

RoR: How to define a method for a partial?

I am trying to render a partial on a page...like so
../devise/sessions/new.html.erb
<%= render partial: '/tweets/latest_tweets' %>
All good
In the partial I would like to do "each do" like so...
../tweets/_latest_tweets.html.erb
<% #tweets.each do |tweet| %>
<%= tweet.content %>
<% end %>
I get undefined method `each' for nil:NilClass
How do I define this method for my partial in my tweets controller?
I have index like so
tweets_controller.rb
def index
#tweets = Tweet.all
end
Which is fine for the index but I don't know how to do same for my partial (I tried just defining my partial like the index but that did not seem to work)
Anyone have a solution I could try?
ty
You need to pass the collection when rendering the partial. It'll give something like this:
devise/sessions/new.html.erb
<%= render partial: 'tweets/latest_tweets', collection: #tweets, as: :tweet %>
tweets/_latest_tweets.html.erb
<%= tweet.content %>
Just like that, and it will iterate through your collection automatically.
Fore more info, please read Rails - Layout and Rendering

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. 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.
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.
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.
Your problem is probably that the file needs to be named _index.html.erb. You can have another file named index.html.erb which just renders _index.html.erb.
If you need a full guide on using AJAX, look up the railscast. If you're not using AJAX and you just want to render it, then you don't use link_to. You just do <%= render :index %>.

Cannot render partial of another module and pass variables

I am sorry in advance as I know this should be an easy one but I am stuck. I have a show view for "Category" in which I am trying to display related has_many "Subcategories". I am calling the partial by using the following:
<%= render partial: 'subcategories/subcategory', locals: {category: #category }%>
I have an html file in the Subcategories view folder properly named and the partial view loads. I know this because the partial has the code
<%= #category.name %></p>
which shows the correct Category name within the partial. However, when I try to load any of the subcategory data by calling
<% #subcategories.each do |subcategory| %>
<%= subcategory.name%>
<% end %>
I get the error: NoMethodError in Categories#show, undefined method `each' for nil:NilClass
I'm sorry to ask such a basic question but I will be using partials from related modules extensively in this project.
Replace this:
<%= render partial: 'subcategories/subcategory', locals: {category: #category }%>
with this:
<%= render #category.subcategories %>
Then in the partial app/views/subcategories/_subcategory.html.erb do this:
<%= subcategory.name %>

Insert ad code after n partials?

Rails 4.2
I have a collection displaying from a partial - is there a way to add a fragment of code in after a certain number in the collection?
So I have a 10 blog posts being shown in a list, each post's formatting from the partial list. I'd like to insert my AdSense code after every 3 posts
<%= render partial: "list", collection: #posts %>
Is there a way to do that automatically, or would I have to do something like assign a numbered id to each post and have "if (id/3) % 1 == 0", and conditionally display it?
Well inside the partial _list.html.erb, you can do check the counter variable provided by Rails list_iteration. So your check will be inside the _list.html.erb partial as:
render 'ad' if list_iteration.index % 3 == 0
You can also use list_counter helper.
Read this Rendering a collection of partials
<%= render partial: "ad", collection: #advertisements %>
This will render “advertiser/_ad.html.erb” and pass the local variable ad to the template for display. An iteration object will automatically be made available to the template with a name of the form partial_name_iteration. The iteration object has knowledge about which index the current object has in the collection and the total size of the collection. The iteration object also has two convenience methods, first? and last?. In the case of the example above, the template would be fed ad_iteration. For backwards compatibility the partial_name_counter is still present and is mapped to the iteration's index method.
Something like:
<% #posts.each_slice(3) do |posts| %>
<%= render partial: "list", collection: posts %>
<%= render partial: 'ad_sense' %>
<% end %>
Might work
each_slice docs for further reading
You could do something with the loop index like
<% #posts.each_with_index do |post, index| %>
<%= render partial: 'list', locals:{post: post} %>
<% if index % 3 == 2 %>
#adsense code here
<% end %>
<%end %>

Strange routing in rails render partials

I'm new to rails and thought I had finally figured out some of this routing stuff, but have been going in circles with this bit all day.
I was following a tutorial about building a twitter like service, and I've got the basics working from the tutorial, but with Mongo instead of mySql.
I've got 3 types of pages.
The home page which is showing all the posts ordered by date
The user page which is showing the posts from a specific user
The posts page which is showing posts from a users friends.
So for each page, I've done the following
1) created a method in the corresponding controller to get the correct posts
2) created a _posts.html.erb page with the display parameters, which are slightly different on each page
3) referenced the partial in the index.html.erb page for each view.
The controller entries look like this
def index
#posts = Post.all(:order => 'created_at DESC')
end
or
def posts
#posts = Post.all(:conditions => {'user_id' => params[:id]}, :order => 'created_at DESC')
end
and the partials are
<%= render :partial => #posts %>
In each view is a _posts.html.erb file, and each is slightly different
home/_posts.html.erb looks like this
<%= div_for post do %>
Posted <%= time_ago_in_words(post.created_at) %> ago
Posted By <%= post.user_id %>
<%= post.text %>
<% end %>
while posts/_post.html.erb looks like this
<%= div_for post do %>
Posted By <%= post.user_id %>
<%= post.text %>
<% if post.created_at > 52.hours.since %>
<%= distance_of_time_in_words_to_now(post.created_at) %>
<% else %>
<%= post.created_at.strftime("%c") %>
<% end %>
<% end %>
Now the strange part is that on all the pages index.html.erb, users/show.html.erb, posts/index.html.erb, the partial that is being displayed is the posts/_post.html.erb. The others are being completely ignored.
my understanding was that render :partial would take the #posts and render _posts.html.erb from the current view. But this isn't happening, as only the posts/_post.html.erb is being rendered from all views.
I've looked in the routes.rb file, but don't have anything in there that would cause this problem.
Can anybody tell me why I am not displaying the proper partials?
-----------Edited --------------------------------
The directory structure for views is as follows
views
- home
-_post.html.erb
-index.htlm.erb
- layouts
- posts
-_post.html.erb
-index.html.erb
-posts.html.erb
- sessions
- users
-_post.html.erb
-new.html.erb
-show.html.erb
I hope that helps.
"post", :collection => #posts%>
maybe rails automatically defines path to the partial when you pass only collection
You're passing the collection as the argument that rails is expecting to be the name of the partial. Your call to render should look like this
<%= render partial: "post", collection: #posts %>
This will render app/views/posts/_post.html.erb, passing the local variable post to the partial.
Additionally, (is sometimes handy) there's an iteration object that is made available to this view, partial_name_iteration, that has information about the total size of the #posts collection, and the index of the current object.

Resources