How to make image_tag hyperlink using link_to in Rails, with mouseover? - mouseover

I am placing *image_tag* images within *link_to* elements, as well as trying to do a mouseover image swap. It's all working except for the mouseover, which is not grabbing the alternate image. On a more general note, am I doing hyperlinked images correctly? Sorry, new to Rails. Much appreciated!
<%= link_to (image_tag("#{product.prod_img}.jpg", alt: product.prod_name, class: 'img_prod_thumb', mouseover: "#{product.prod_img}_over.jpg")), "/products/#{product.id}" %>

Couple of pointers:
Don't dynamically add img extensions to your code (esp. in the view) unless there is a very good reason to do so(?)
You can use simple product as the path, instead of a hardcoded url
Here's what I'd do:
>>> move product image name construction to the model, helper, or decorator
# model (for simplicity)
def prod_img_full
"#{prod_img}.jpg"
end
def prod_img_over
"#{prod_img}_over.jpg"
end
>>> update and clean up link_to
<%= link_to image_tag(product.prod_img_full,
alt: product.prod_name,
class: 'img_prod_thumb',
mouseover: product.prod_img_over),
product %>
You could easily move the image_tag to a decorator, which would allow this within your views:
<%= link_to product.hover_image, product %>

Related

Rails 5 rendering partials and passing data

I'm having trouble getting traction on what the general ways data gets passed around from and is made available to partials.
For example:
I have a controller handing off an instance variable to a template that renders a partial:
static_pages_controller.rb:
def home
#feed_items = current_user.feed
end
home.html.erb:
<%= render 'shared/feed' %>
_feed.html.erb:
<%= render #feed_items %>
Now, inside my User model is an instance method that reaching into the database to get her posts:
user.rb:
def feed
Micropost.where("user_id = ?", id)
end
So somehow because Micropost.where(...) returns a collection of microposts is that how Rails knows to go from _feed.html.erb to another partial where the <li> is defined for how microposts want to be defined?
_micropost.html.erb:
<li id="micropost-<%= micropost.id %>">
<%= link_to adorable_avatar_for(micropost.user, size: 50), micropost.user %>
</li>
And also is it just a convention that because I'm really handling a collection of microposts that Rails knows to give the micropost variable?
Your questions are answered in the Ruby on Rails Guides on Layouts and Rendering. It's worth reading the information on partials that comes before the quoted passages below as well:
Every partial also has a local variable with the same name as the
partial (minus the underscore). You can pass an object in to this
local variable via the :object option:
<%= render partial: "customer", object: #new_customer %>
Within the customer partial, the customer variable will refer to
#new_customer from the parent view. (Earlier the Guide instructs that to specify other options for render(), e.g. object:, you have to explicitly specify partial: and the name of the partial.)
If you have an instance of a model to render into a partial, you can
use a shorthand syntax:
<%= render #customer %>
Assuming that the #customer instance variable contains an instance of
the Customer model, this will use _customer.html.erb to render it and
will pass the local variable customer into the partial which will
refer to the #customer instance variable in the parent view.
3.4.5 Rendering Collections
Partials are very useful in rendering collections. When you pass a
collection to a partial via the :collection option, the partial will
be inserted once for each member in the collection:
index.html.erb:
<h1>Products</h1>
<%= render partial: "product", collection: #products %>
_product.html.erb:
<p>Product Name: <%= product.name %></p>
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 _product, and within the _product partial,
you can refer to product to get the instance that is being rendered.
There is also a shorthand for this. Assuming #products is a collection
of product instances, you can simply write this in the index.html.erb
to produce the same result:
<h1>Products</h1>
<%= render #products %>
Rails determines the name of the partial to use by looking at the
model name in the collection. In fact, you can even create a
heterogeneous collection and render it this way, and Rails will choose
the proper partial for each member of the collection:
index.html.erb:
<h1>Contacts</h1>
<%= render [customer1, employee1, customer2, employee2] %>
customers/_customer.html.erb:
<p>Customer: <%= customer.name %></p>
employees/_employee.html.erb:
<p>Employee: <%= employee.name %></p>
In this case, Rails will use the customer or employee partials as
appropriate for each member of the collection.
In the event that the collection is empty, render will return nil, so
it should be fairly simple to provide alternative content.
<h1>Products</h1>
<%= render(#products) || "There are no products available." %>

rails sending code block to partial

Rails 3.2.13 & ERB
I am trying to get send some link_to items to a partial. I am sending in a title to the partial successfully as below.
<%= render :partial =>'form',
:locals => {:page_title => t(:'<h2>Editing Feature</h2>')}
What I dont like is that I am currently doing this:
<%= link_to 'Show', #feature %> |
<%= link_to 'Back', features_path %>
as part of the primary page. I would like to take this codeblock and send it to the partial for rendering there. The primary reason is the simple_form is defined in the partial and I have a well that contains everything on that page. Except for the two link_to items down on the bottom of the page. If I could pass them into the partial somehow (I assume as a code block) then I can decide where to place them and how to render them in the form itself instead of kind of as an afterthought.
Thanks.
Typically we use partials to break off HTML chunks into separate modular components and reduce repetition. Partials have the same variable context as the template that rendered them, so as-is, you wouldn't have any trouble simply relocating your links to your partial and everything should just work.
I think the better practice, however, would be to pass your instance variable as a local to reduce coupling with your controller. Something like:
Your view:
<%= render :partial =>'form',
:locals => {:page_title => t(:'<h2>Editing Feature</h2>'),
:feature => #feature} %>
_form.html.erb:
<%= link_to 'Show', feature %> |
<%= link_to 'Back', features_path %>
This way, if you were to render the partial elsewhere in your application you don't need to have an instance variable handy. An example situation where this could be useful would be looping through multiple "features" (a la an index view) and spitting out the relevant HTML as defined in the partial.

rails change path of partial collection rendering

I have an STI relationship where a conversation is composed of both messages and images.
Now when I go to render them i use:
<%= render conversation %>
which works perfect. It finds the given template for the given object and renders it.
Now for my mobile site I want to use the same thing only now it should find say:
/mobile/message/_message.html.erb
instead of
/message/_message.html.erb
So in my controller i said:
if mobile?
prepend_view_path "mobile"
end
Which does get called, and it "prepends my view path" which i can see is working when i do:
raise view_paths.inspect
However now when i do my
<%= render conversation %>
It is still looking in the default location i.e. /views/ for the partial
Well, this should work, but it is distressingly inelegant:
<% conversation.each do |c| %>
<% c_class = c.class.to_s.downcase.underscore %>
<%= render :partial => "mobile/#{c_class}/#{c_class}", :object => c %>
<% end %>

How can I render a view that is a part of my user controller on a static page like my home page?

I'm looking to place the signup form for an app I'm building on the app's home page, however I don't know how to render the view app/views/users/new.html.erb on my home page. Is there a method I can use to accomplish this? Thanks a bunch!
You can use a content_for block and then yield it on your application layout. Create a partial with your users/new page. (users/_new).
In your homepage.html.erb file add
<% content_for :homepage do %>
<%= render users/new %>
<% end %>
Then in your application.html.erb file add
<%= yield :homepage %>

Rails 3 create a link to image_url and show image_url(:medium)

I am trying to create a link to an image #user.image_url. This will be the full sized image. I want the medium resized image to be shown #user.image_url(:medium). But what I also want is for the #user.image_url to open in FancyBox.
My current code at the moment is:
<%= link_to image_tag #user.image_url(:medium), :style => "width:717px;height:409px" %>
This links to the Users profile page, which is what I don't want.
Please help :)
Use brackets for nesting methods
<%= link_to image_tag( #user.image_url(:medium), :style => "width:717px;height:409px"), #user.image_url %>

Resources