rails render model with location - ruby-on-rails-3

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.

Related

ransack gem rails 3

i am getting stuck with using the gem ransack. I have placed ransack in my gemfile and then run bundle install ( though i used just bundle, does that make a difference? i didnt think it did?)
Next i have placed this in my Recipe controller
def all_recipes
#q = Recipe.search(params[:q])
#searchresults = #q.result(:distinct => true)
end
Within my view (all_recipes)i have the search form and block to display my results
<%= search_form_for #q do |f| %>
<%= f.label :dish_name_cont %>
<%= f.text_field :dish_name_cont %>
<%= f.submit %>
<% end %>
---------------------
<% #searchresults.each do |r| %>
<tr>
<td><%= r.dish_name %></td>
</tr>
<% end %>
Ive got two problems, without even conducting a search i get this in my view where the block is
fish and chips my first recipe lasagne Lasagne
and then if i search say las it redirects me to my index page after the get request but as i have no block to display the results i get an undefined error, which is expected.
After this i placed my controller code within the index action and the form and block within the index view and now it all works? Why cant i use the all_recipes action and why does it redirect?
In depth demo, may be helpful - http://railscasts.com/episodes/370-ransack?view=asciicast

Activerecord iteration / append an exception at a certain position

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

Rails search functionality

I am taking a rails class at my University and I am trying to create a search form which will show the results on the same page rather than show a different page of results. Is this something simple to do? I am creating a museum app with artifacts for each museum but I want the user to search artifacts from either page.
On my routes.rb I have
resources :artifacts do
collection do
get 'search'
end
end
On my museum index I have the code below that he gave us but not sure how to tweak the get routes for the same page.
<%= form_tag search_artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
<% if #artifacts %>
<p> <%= #artifacts.length %> matching artifacts. </p>
<h2> Matching Artifacts </h2>
<% #artifacts.each do |a| %>
<%= link_to "#{a.name} (#{a.year})", a %><br />
<% end %>
<% end %>
Yes, this is easy. Just have the index page return the search results if params[:search_text] is present - this way you don't need a new route or a different page.
class ArtifactsController < ApplicationController
def index
#artifacts = Artifact.search(params[:search_text])
end
end
class Artifact < ActiveRecord::Base
def self.search(query)
if query
where('name ILIKE ?', "%#{query}%")
else
all
end
end
end
So then your form looks like:
<%= form_tag artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
Edit:
So what you really want to do is any page you want to search, include a form which makes a request to that same page.
Then in each of those controller methods just put this line of code:
#artifacts = Artifact.search(params[:search_text])
and that will populate the #artifcats array with only artifacts that match the search query.
Try using "Ransack" gem. It can also perform some more powerful searches.

Partial in rails 3 application renders :per_page times while using will_paginate

I am using will_paginate in my rails 3 application.
In my Controller i have a line as
def index
#blogs = Blog.paginate(:per_page => 5, :page => params[page])
end
And in my index.html.erb
<div>
<% if #blogs.size.zero? %>
No Blogs Found
<% else %>
<%= render #blogs if #blogs %>
<% end %>
</div>
<%= will_paginate #blogs%>
And in my partial _blog.html.erb
<% #blogs.each do |blog| %>
<%= link_to blog.title, blog_path(blog.id)%>
<% end %>
When i run the app by $rails s
The partial displays the contents per_page times..
For the first page it displays for 5 times ..
How to avoid so ..
Give me some suggestions...
EDIT
I changed the partial code in _blog.html.erb to
<%= link_to blog.title, blog_path(blog.id)%>
which is the right one as already by default rails 3 will send the collection #blogs to the partial noneed for the loop in the partials ..
When render is passed a collection, it automatically renders the partial multiple times.
Therefore you want your partial _blog.html.erb to just be:
<%= link_to blog.title, blog_path(blog.id)%>
You don't need an each statement there because the render statement takes care of this.
See 3.4.5 "Rendering Collections" in http://guides.rubyonrails.org/layouts_and_rendering.html

Is it possible to use partials to be rendered as wrappers in Rails?

I would like to render structures like this:
<tag1>
<tag2 someattribute="somevalue">
<.. lot of things inside ..>
</tag2>
</tag1>
<tag1>
<tag2 someattribute="someothervalue">
<.. different inside things inside ..>
</tag2>
</tag1>
The tag1, tag2 are the same, they are just parametrized. The inner part of the code changes. I tried to implement the thing above like that (haml):
%div{id:['products', id]}
.products_content
%div{id:['products', id, 'content'], class:'products_mask'}
= yield
This was the partial _content_head.html.haml, which is called from a template:
= render 'shared/content_head', id: 'all' do
%h3= Title
%p= Body of the text.
My theory that yield inside the partial would lead to rendering of the passed block did not prove. Is there a way to use partials as code wrappers? Can you suggest me some solution how to reach this? Thank you.
This might be a good use of the capture method.
I'm only familiar with ERB, but here is the general idea:
<% structure = capture do %>
<h3>Title</h3>
<p>Body of text</p>
<% end %>
Then pass the variable into the partial:
<%= render 'shared/content_head', :structure => structure %>
And within the partial, spit out the structure variable:
<%= structure %>
Reset structure multiple times within the view as you render partials (or maybe more appropriately, in a helper?).
I've used the following (Rails 4, but I think it should work with Rails 3 too):
<%# app/views/users/_edit.html.erb %>
<%= render layout: 'modal_wrapping' do |f| %>
<%= f.input :email %>
...
<% end %>
.
<%# app/views/users/_modal_wrapping.html.erb %>
<div id='modal'>
<%= simple_form_for #user do |f| %>
<%= yield f %>
<% end %>
</div>