Francky
Francky

Reputation: 205

Problems with rails helper link_to concerning class

I'm having difficulties with rails. In fact, I wrote these lines :

<%= link_to :controller => "offers", :action => "get_offer", :offer_slug => offer.friendly_id, {:class => "media"} do %>

. . .

<% end %>

My problem is that everything that is inside link_to is getting the css effects of a hyperlink (no padding, gets underlined when hover), instead of custom properties.

It's like the :class property isn't being taken into account.

Just in case, here's the full part :

 <<%= link_to :controller => "offers", :action => "get_offer", :offer_slug => offer.friendly_id, {:class => "media"} do %>

                        <li id="<%= offer.id %>" class="media" style="border: 1px solid #696969">
                          <div class="col-lg-3" style="margin-bottom:0px; font-size:12px; text-align:center; margin-right:10px;">

                            <% if offer.subcategory.nil? %>
                                <img class="media-object" src="<%= asset_path "other-icon.png" %>" style="margin-bottom:5px;">
                            <% elsif offer.subcategory.category.icon.empty? %>

                            <% else %>
                                <img src="<%= asset_path offer.subcategory.category.icon %>" style="margin-bottom:5px; width:110px;">
                            <% end %>

                            <br/>

                            <% if offer.subcategory.present? %>
                                <b><%= offer.subcategory.category.name %></b><br/> <%= offer.subcategory.name %>
                            <% else %>
                                <b>Autres</b><br/> <%= offer.subcategory_other %>
                            <% end %>

                          </div>

                          <div class="media-body">
                            <!--Titre annonce + type contrat-->

                            <div style="text-align:right; margin-bottom: 10px;">
                              <% if offer.id >= @new_limit %>
                                  <span class="label label-warning">New</span>
                              <% end %>
                              <span class="label label-success"><%= (offer.max_price * 0.85).ceil %> euros</span>
                              <span class="label label-success" style="background-color: grey"><%= (offer.customer_objective) %>
                                succès </span>
                            </div>
                            <h4 class="media-heading">
                              <%= raw(offer.name) %>
                            </h4>


                            <p>
                              <% if offer.description %>
                                  <%= offer.description.split[0..60].join(" ") %>

                                  <% if offer.description.split[0..60].count >= 59 %>
                                  <% end %>

                              <% end %>
                            </p>

                            <% if offer.company_context.present? %>
                                <p>
                                  <%= offer.company_context.split[0..60].join(" ") %>
                                  <% if offer.company_context.split[0..60].count >= 59 %>
                                  <% end %>
                                </p>
                            <% end %>

                          </div>

                          <div class="offer-tags">
                            <% offer.offer_tags.each do |offer_tag| %>
                                <span class="badge"><%= offer_tag.tag.name %></span>
                            <% end %>
                          </div>
                        </li>
                    <% end %>

Upvotes: 0

Views: 171

Answers (1)

steve klein
steve klein

Reputation: 2629

Try to wrap the class in curly braces:

<%= link_to :controller => "offers", :action => "get_offer", :offer_slug => offer.friendly_id, { :class => "media" } %>

The Rails link_to helper is pretty flexible and allows a few different call signatures, which can lead to confusion. You are needing this one:

link_to(body, url_options = {}, html_options = {})

Without the wrapping braces, Rails is not clear that :class is in fact an HTML option.

Docs here. I should mention I also dropped your trailing do which implied a block that didn't seem to be there (nor was it needed).

UPDATE

I did not understand that the block was intended as the link body. In that case, the right call signature would be:

link_to(options = {}, html_options = {}) do
  # name
end

So, your body is fine, but in your link_to, you want to group the URL options, followed by the HTML options. Plus I think you may need parentheses around the whole call for it to work correctly:

<%= link_to({ :controller => "offers", :action => "get_offer", :offer_slug => offer.friendly_id }, { :class => "media" }) do %>

Upvotes: 2

Related Questions