Access to value into a link_to data confirm sentence - ruby-on-rails

I'm trying to access to the pseudo of my user into my confirmation message. I've try to use #{} parenthesis, and also another <%=%> tag to did it. But is not working. Do you have any idea of what I can use to achieve it ?
<%= link_to expel_group_path(group, idz: user.id), method: :patch, remote: true, :data => {:confirm => 'you want to kick out #{user.pseudo} of the group #{group.name}'} do %>

Interpolation is possible only between double quotes (or heredoc syntax)
"you want to kick out #{user.pseudo} of the group #{group.name}"

Related

create a link with parameters, but show param only if condition occurs

How can I create a link_to and have a certain param to be in the url only if some condition occurs?
For example, I want to have someurl.com/1111?foo=clown
But the foo should be there only if a condition is true.
If not, it should be just:
someurl.com/1111
<%= link_to 'My Link', your_path(:foo => ("clown" if your_condition)) %>
In this exemple, :param equals to "clown" if your_condition is true, else :param is nil and if the param is nil, Ruby don't consider there is a parameter and ignore it.
Maybe a simple condition like this could work.
if (myCondition)
link_to :myPage, myVariable: var
else
link_to :myPage
end

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

passing large arrays via link_to function (or by other means) in Rails

I know that link_to uses get action by default and also you can change the method to post by passing :method => :post to link_to function, but it does not seem to work. Here is the syntax that I am using:
= link_to "Export" export_path(:data_array => d_array), :method => 'post'
But this does not seem to work. The array is being passed by as a query parameter which I can see in the URL box and it bombs my application since it blows the string length limit in the url string.
Try using form instead:
= form_tag export_path do
- d_array.each do |val|
= hidden_field_tag 'data_array[]', val
= submit_tag 'Export'
Notice that in a controller params[:data_array] will be an array of strings.

How do I specify the format for url_for in a nested route?

The following link_to statement:
<%= link_to image_tag("icons/document_24.png"),
[program_code.program, program_code],
:class => :no_hover,
:alt => "Print Tracking Code",
:title => "Print Tracking Code",
:target => :new
%>
will generate a url like /programs/1/program_codes/1
If I want the url to be /programs/1/program_codes/1.svg, how do I specify the format in the array that is being passed to url_for? I've searched the Rails API documentation and looked at several examples but have been unable to find anything like this.
I think your looking for the :format option. It will append the file extension to the link e.g. '.svg'
Make sure you put the :format option in the path building hash of the link_to method.
<%= link_to 'somewhere', {somewhere_to_path(#var), :format => "svg"},:title => "Print Tracking Code", :target => "_blank" %>
Hope this helps.
If you are dealing with a specific class and can use a named route, that is the most efficient option. But if you're dealing with nested resources and the parent resource isn't fixed (for instance, in a polymorphic association), AND you want to specify a format, url_for doesn't meet your needs.
Fortunately you can use polymorphic_url.
For instance, if server could be an instance of ManagedServer or UnmanagedServer, and both can have alerts, you can do this:
polymorphic_url([server, :alerts], :format => :xml)
and that will give you
/managed_server/123/alerts.xml
/unmanaged_server/123/alerts.xml

Using hyphen in link_to property?

In my Rails app, I need to set a value for a custom data-* attribute on an anchor tag. However, hashes can't have hyphens if I'm not mistaken. I essentially want to do this:
<%= link_to 'Example', example_path, :class => 'something', :data-id => '15' %>
:data-id is not valid, however. What can I do to work around this?
IIRC, for such purposes hashes and strings are equivalent, so you can use "data-id" instead of :data-id. Never checked for this particular method, though, so no guarantees.
I think in Rails 3 you can do
link_to "Click Here", root_path, :data => { :id => #model.id }
for all data attributes.

Resources