How to apply a link to a multiline text string in HAML? - ruby-on-rails

I have a multiline chunk such as this:
%li
=link_to image_tag("image.jpg", :size => "48x48", :alt => "Something", :title => "Something", :class => "some-class"), some_path
%p.testi
"Some text right here that is divided up onto multiple lines &
%br
and just won't stop running on!”
I need a link to surround both the image and the text. Presently the image link works fine, however on the text link, I believe I have to start with a link_to block, but I'm not seeing the syntax that I should follow there.
How is this correctly done so that all of the multiple lines of text also have a link applied?

In slim (wich should be similar):
= link_to some_path, class: "some-class" do
h2 = "#{some_object.title}"
p.testi
"Some text right here that is divided up onto multiple lines &
br/
and just won't stop running on!”

See Multiline string in HAML
You want to use the | at the end of each line you are wrapping.

Related

How to link to a class with selective text in ruby?

So, I'm trying to make a link which has a class and some text with the given link. Here's the code -
= link_to((session[:request_id] ? 'Save & Exit': 'Exit'), "/account/#{#name}", :id => 'cancel-application', :class => "cancel-application")
What I want is that I get a link to this class called "cancel-application", which is actually a small cancel image. In front of it, I want to get "Save & Exit" or "Exit" depending on the session. The "Save & Exit" or "Exit" button should also be linked. Something like this -
[cancel button] "Exit" (where both link to the same place)
Right now, what this code does, is put the image and the text on top of each other, which makes sense. So, what I thought of doing was put the session id in a variable and then put it after the link like this -
= #exit_text = session[:request_id] ? 'Save & Exit': 'Exit'
= link_to("/account/#{#name}", :id => 'cancel-application', :class => "cancel-application") #exit_text
This doesn't work. I was wondering how I could do something like that.
EDIT - I found a solution, although I feel there can be better ones.
You can still do this with one link:
= link_to "/store/#{#name}", :id => 'cancel-application' do
.cancel-application
= session[:request_id] ? 'Save & Exit' : 'Exit'
link_to can accept the block which is captured to build link's inner html. When you pass the block, you do not pass links label.
I just used two link_to. I guess there's no way to do it in one link_to:
= link_to("", "/account/#{#name}", :id => 'cancel-application', :class => "cancel-application")
= link_to((session[:request_id] ? 'Save & Exit': 'Exit'), "/account/#{#name}", :id => 'cancel-application')

Make simple_form honor my blank/empty label

Using simple_form v2.1.0 in a Rails 3.2.13 app. I have a simple_form where I want to have a checkbox show up with an inline label, and I want to have an "empty" normal label. This way, the label's container should remain in place and the checkbox will be properly positioned with the other form elements.
If I set :label => "" or :label => " " or :label => nil or don't provide a :label at all, I end up with the field name as the label.
If I set :label => false, the label and containing elements aren't rendered at all (so the checkbox appears at the far left of the page).
If I set :label => " ", I end up with the literal text , as expected.
It seems there is no way to tell simple form to leave the label there, just don't put any text in its container.
Here is an example form where I'm trying to setup an empty label for a checkbox with an inline label.
<%= simple_form_for #model ... %>
<%= f.input :sticky, :label => "", :inline_label => "Sticky?", :as => :boolean ... %>
<% end %>
But simple_form thinks it's smarter than me and defaults the label. I wish it only did that if I hadn't specifically supplied a label!
Try setting :label value to false.
UPDATE: Since SimpleForm is supposed to rely on ActionView to render the elements, you can use this hackish solution: set the label in your locale file to empty string, like this
en:
activerecord:
attributes:
model_name_underscored:
sticky: ' '
Have you tried removing the :label attribute in your input completely and instead using form.label for the empty label.
http://apidock.com/rails/ActionView/Helpers/FormHelper/label
Instead of using an empty label you could position your check box with CSS.
Was having similar troubles. In your form defaults just do this:
defaults: { :label => 'blanklabel', label_html: { class: 'blanklabel'}
Then in your CSS, just set the label text color to white:
.blanklabel { color: white; }

Correct HAML for linking an image to another page in my Ruby on Rails app

Trying to figure out how to use haml "link_to" with Ruby code in order to make an image on a page link to another page on the site. If possible I'd like to keep the nav static and fade the two pages in and out via a "Forward" and "Back" image. Any ideas? Just want to get the linking right first and then can go in and figure out the JQuery. Currently have the code below...
Thanks!!
.pad-bottom50
%div{:style => "position: absolute; top: 620px; left: 830px;"}
=link_to (image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated") if #page.photos[1].image?
.pad-top20
Syntax of link_to is
=link_to link_text, link_url, options
You missed the link_url. ie. to where the user should be taken when clicking on the image.
Here is a working example
=link_to(image_tag("http://goo.gl/FZUI3"), "http://en.wikipedia.org/wiki/Nyan_Cat", :id => "#fade1", :class => "animated")
You are not specifying the src for the link.
The syntax is:
link_to "Link Text", "/path-to-link"
To put an image in there:
image_tag "path-to-image"
link_to(image_tag("path-to-image"), "/path-to-link")
This code will make an image wrapped by a link pointing to /bacon if #page.photos[1].image?
link_to(image_tag(image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated")), "/bacon", :id => "bacon", :class => "bacon") if #page.photos[1].image?
You should first look at the parameters of a link_to tag . You will get to know about the parameters and what you can do with those parameters .
check this link
api doc
You can check all paramters of a method avilable in API Ruby on Rails

Rails - Render partial inside Twitter Bootstrap popover

I want to use a Twitter Bootstrap popover to display a user avatar and email address, with the possibility of adding in more details at a later date.
I am having an issue getting the partial to render inside the content field of the link. The view code is below (in HAML).
%span.comment-username
=link_to comment.user_name, "#", "title" => comment.user_name,
"data-content" => "=render 'users/name_popover'",
class: "comment-user-name"
Currently, this will only produce the content code as a string.
Is there a way to do this so the partial will be inserted instead of the code as a string?
You want rails to actually evaluate the content of the string and not just show the string. The easiest way to do that would be:
%span.comment-username
=link_to comment.user_name, "#", "title" => comment.user_name,
"data-content" => "#{render 'users/name_popover'}",
class: "comment-user-name"
That should pass the render statement to rails and render your partial as expected.
Thanks for the answer jrc - it helped me find a solution.
My solution might be useful for other non HAML people like me:
`<%= link_to('Service History' , '#', :class => "popover-history", :rel => "popover", :"data-placement" => "bottom", :title => "Service History", :"data-content" => "#{render 'services/service_history'}") %>`
with
`$(function () {
$('.popover-history').popover({ html : true });
});`

Rails: How to escape ampersand in URL formation

I have a link_to helper like the following:
<%= link_to "example & text", url_for(:controller =>'example', :title=>"example & text") %>
It frames the URL http://localhost:3000/example?title=example&amp:text
In the sample controller it calls the index method but params[:title] returns
the value example&amp:text.
I want to have a value like "example & text". So I have tried
CGI::escape() and CGI::escapeHTML() but without luck.
The url needs to be escaped using CGI.escape:
link_to "example & text", :controller => "example", :title => CGI.escape("example & text")
This should generate something like:
example & text
Then, wherever you're wanting to use this, you can unescape it again to get it back to normal:
CGI.unescape params[:title] # => "example & text"
For this there is a nice rails method called raw.
You can use in this way :
<%= link_to raw("example & text"), url_for(:controller =>'example', :title=>raw("example & text")) %>
One more this & should not be used in the URL as it is used as params separator.

Resources