Activerecord iteration / append an exception at a certain position - ruby-on-rails-3

I'm looking for a way to include a specific element at a certain position inside an iteration
Not experienced enough to use the right pattern (hence to search here on stack overflow with the right keywords, afraid of getting some duplicate question with this one)… but the base idea would as the following :
<% Post.all.each do |post| %>
<% if Post.all.index(post) == 5 # or any position %>
# render some html element (some kind of exception)
<% else %>
<%= post.title %>
<% end %>
<% end %>
But just without skipping any records in my post array

I'm not sure I have totally understood your request. each_with_index may help you, and if you don't use the else, you won't skip any records :
<% Post.all.each_with_index do |post, index| %>
<% if index == 5 # or any position %>
# render some html element (some kind of exception)
<% end %>
<%= post.title %>
<% end %>

Related

How to check if an ActiveRecord table entry is empty

I am not sure if the title of this question uses proper jargon, but hopefully this description will help. If you need more information, please let me know.
I am taking Chinese text from a form and splitting it into a 2d array of sentences and words. I then want to define all the words using dictionary entries in my database. Some of the words aren't in the database, so I want to check for that. What I am trying isn't working.
Here is my current code:
<% #lesson.parsed_content.each_with_index do |sentence, si| %> #iterate 1st dimension
<% sentence.each_with_index do |word,wi| %> #iterate 2nd dimension
<% sentence = sentence.to_s %>
<div class="word blt" id="<%= wi %>">
<div class="definition blt">
<% definition = DictionaryEntry.where(:simplified => word) %> #search by simplified chinese
<% definition.find_each do |w| %>
<% if w.definition == nil %> # PROBLEM: this never returns true.
<%= word %>
<% else %>
<%= w.definition %>
<% end %>
<% end %>
</div>
<div class='chinese blt'> <%= word %></div>
</div>
<% end %>
<% end %>
How can I change <% if w.definition == nil %> to return true if there is no definition in my database?
This is a shot in the dark but first I would switch your code around when you are converting the variable sentence to a string and looping through it. (unless you have a reason for it being that way)
<% sentence = sentence.to_s %>
<% sentence.each_with_index do |word,wi| %> #iterate 2nd dimension
Second, depending on how your data was put inside the database it might be an empty string instead of nil. So I would change the condition from
<% if w.definition == nil %> # PROBLEM: this never returns true.
to
<% if w.definition.blank? %> # Checks to see if definition is blank
Blank will check if its false, empty, or a whitespace string.
Finally, indentation is helpful especially when running loops and conditionals. It's easier on the eyes and helps you understand what's going on.
<% #lesson.parsed_content.each_with_index do |sentence, si| %>
<% sentence = sentence.to_s %>
<% sentence.each_with_index do |word,wi| %>
<div class="word blt" id="<%= wi %>">
<div class="definition blt">
<% definition = DictionaryEntry.where(:simplified => word) %>
<% if definition.empty? %>
<% word %>
<% else %>
<% definition.find_each do |w| %>
<%= w.definition %>
<% end %>
<% end %>
</div>
<div class='chinese blt'> <%= word %></div>
</div>
<% end %>
<% end %>
Let me know the results.

No inline blocks in ERB helpers?

I was curious: in an ERB file, when passing a block to a view helper, why does this work:
<%= div_for #thing do |x| %>
<%= x %>
<% end %>
while this doesn't?
<%= div_for #thing {|x| x.to_s} %>
In Ruby, do...end is exactly the same as {...}, so why not in ERB? Note aside: I can use x on its own on the second line above because its .to_s method returns the field I want to render. Sorry if this has been asked before, I wasn't able to find a similar question (found a similar answer though).
I suspect that the second block of code is exactly the same as:
<%= div_for #thing do |x| %>
<% x %>
<% end %>
Because the x doesn't have a "=" on it, it's not going to be output in your view.
Try :
<%= div_for #thing {|x| concat x.to_s} %>

rails render model with location

I have a Picture model and i'd like to use <%= render #pictures %> in my view in order to display them.
I also want the pictures to be arranged as 3 columns across the screen.
If I use the render how can I know which picture I am rendering in order to know where to place it? (such as in a table or some other arrangement that is not 1 dimensional)
Is there a way to make the rendering automation to have a counter?
<% #pictures.each_index do |i| %>
<% #some routine here %>
<%= render #pictures[i] %>
<% end %>
I would suggest using each_with_index instead:
<% #pictures.each_with_index do |picture, i| %>
<%= render picture, :i => i %>
<% end %>
Notice that you can pass index to the partial as well.

Rails syntax error, unexpected ')'

This is going to be a really dumb question, and I almost hate myself for asking it, but here goes.
When I run my Cucumber test, I'm getting a "syntax error, unexpected ')'" with the following code:
inside my user model:
def member?(gallery)
array = []
self.groups.each do |group|
array << group.id
end
if array.include?(gallery.group.id)
true
end
end
And in my view:
<ul>
<% #galleries.each do |gallery| %>
<% if current_user.member?(gallery) %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
EDIT: Here is the important part of the error in full:
~/Coding/Rails/galleryTest/app/views/galleries/index.html.erb:8: syntax error, unexpected ')', expecting keyword_then or ';' or '\n'
... current_user.member? gallery );#output_buffer.safe_concat('
... ^
~/Coding/Rails/galleryTest/app/views/galleries/index.html.erb:13: syntax error, unexpected keyword_end, expecting ')'
'); end
^
EDIT 2: Here is the error when removing the '=':
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id (ActionView::Template::Error)
./app/models/user.rb:18:in `member?'
I have tried a few different things, and I've got to be missing something really trivial. Another pair of eyes would be greatly appreciated.
Thank you very much.
<%= if current_user.member?(gallery) %>
should be:
<% if current_user.member?(gallery) %>
Not that there is no =, it means output and your code is trying to output the response of the if block.
Alright, here is what I have, and how it seems to be working:
In my user model:
def member?(gallery)
array = self.groups.collect { |g| g.id }
if array.include?(gallery.group_id)
true
end
end
In my view:
<% if user_signed_in? %>
<ul id="private_galleries">
<% #galleries.each do |gallery| %>
<% if current_user.member?(gallery) %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
<% end %>
<ul>
<% #galleries.each do |gallery| %>
<% if gallery.group_id == nil %>
<li>
<%= link_to gallery.title, gallery %>
</li>
<% end %>
<% end %>
</ul>
My tests are running alright now, but they aren't passing, which is strange, as when I set up users with groups and galleries with groups and view them with the site running, they seem to be showing appropriately, which just means my tests are probably effed. That'll be a task for the morning, and probably another question on StackOverflow!

Rails - Display a title only once in an .each block

Noob question here :)
I'm testing a variable, and if it exists, I'd like to display an .each loop with a title.
Of course, the title should be displayed only once. Is there a way to do it? Any best practice?
<%
#twitter_friends.each do |u|
if #user = User.is_a_member?(u.id)
%>
# HERE I'D LIKE TO DISPLAY THE TITLE ONLY AT FIRST ITERATION
<% #user.name %> is your twitter friend, and is a member.
<% end %>
<% end %>
Thanks !
I would normally recommend using each_with_index and checking for a zero index, but seeing as you have a conditional in the loop, you should use a check variable like so:
<% shown_title = false %>
<% #twitter_friends.each do |u| %>
<% if #user = User.is_a_member?(u.id) %>
# HERE I'D LIKE TO DISPLAY THE TITLE ONLY AT FIRST ITERATION
<% unless shown_title %>
<h1>My Title</h1>
<% shown_title = true %>
<% end %>
<% #user.name %> is your twitter friend, and is a member.
<% end %>
<% end %>