I want to use variables in the confirm message in link_to
<%= link_to 'DESTROY!', #user, :confirm => "are you sure you want to delete #{#user.name}"%>
this doesn't seem to work and it prints "are you sure you want to delete #{#user.name}"
How do I use variables in the confirm message?
It's just a ruby string, confirm has nothing to do with it, are you sure you have double quotes in your actual code? It's not interpolating which is what happens when you have single quotes.
Related
So I currently have this code in the html.erb:
<%= f.label :user_id %>
<%= f.field :user_id %>
I want the user to enter an email into the field in which it converts it into user_id using this:
User.find_by_email("xyz#abc.com").id
However, I have no idea how to use it or know whether this will even work. Will appreciate some help on this as I am quite new to rails. Thanks!
Are you trying to get the value of the user's email after they press the submit button?
Update
Try this:
User.find_by(email: params[:user_id])
This will work after the user presses the submit button, but you have to assign it to a variable if you want to use it somewhere else.
I have a link_to image_tag that when clicked should pop up a modal to show a help screen. The button code is:
<%= link_to image_tag('help_sm.png'), '#', :id =>"btnShowHelp_"{current_step} %>
where the {current_step} is the name of the step the user is on that corresponds to the help screen they are accessing.
I am getting a SyntaxError outputting the current_step.
Can someone help with the correct syntax?
Interpolation in strings in ruby looks like this: "#{var}", so you need to do this:
<%= link_to image_tag('help_sm.png'), '#', :id =>"btnShowHelp_#{current_step}" %>
Note that you have to use double quotes(") for string interpolation, if you use single quotes('), it won't interpolate.
I've been looking around how to add an id selector to a link_to helper using haml, is that possible?
a .haml - %a#booked{:href => "index.haml"} Link 1
b .html.erb - booking.html.erb - <%= link_to "Link 1", booking_path, :id => "booked" %>
c .haml.erb - booking.haml.erb - ...??
Which would be the equivalent of b in haml?
link_to works exactly the same way in haml as it does in erb. So this will do what you want:
= link_to "Link 1", booking_path, :id => "booked"
#=> <a id="booked" href="/bookings">Link 1</a>
You can also assign a class attribute in this way:
= link_to "Link 1", booking_path, :id => "booked", :class => "some_class"
#=> <a id="booked" class="some_class" href="/bookings">Link 1</a>
More on how to insert ruby code in haml: Inserting ruby
And, just so there are no doubts about passing ids and classes to link_to, here is an example from the docs:
link_to "Articles", articles_path, :id => "news", :class => "article"
#=> Articles
To add an id selector in haml using link_to you have to specify two hashes.
e.g = link_to "Link 1", {booking_path, extra arg...}, {:id => 'booked'}
An important Ruby idiom is poetry mode: the ability to omit parentheses and curly braces when the parsing is unambiguous. Most commonly, Ruby programmers may omit parentheses around arguments to a method call, and omit curly braces when the last argument to a method call is a hash. Hence the following two method calls are equivalent, given a method link_to that takes one string argument and one hash argument:
Without curly braces, there’s no way to tell whether this call is trying to pass a hash with two keys or two hashes of one key each. Therefore poetry mode can only be used when there’s a single hash argument and it’s the last argument.
Patterson, David; Fox, Armando (2012-08-24). Engineering Long-Lasting Software: An Agile Approach Using SaaS and Cloud Computing, Beta Edition (Kindle Locations 1973-1975). Strawberry Canyon LLC. Kindle Edition.
I am in the process of trying to use the update_attribute command, but struggling to get it working (at all) and hoped someone could point me in the right direction?
I have previously posted a question about this issue, it was very useful in terms of giving a feel for the mechanics of what is going on, but unfortunately it didn't actually get it working.
I have a database of items (Items), which among other things contains ':item_name', ':click_count' and ':external_url'.
Currently I have a view (Showselecteditems) in which there is a list of all the items, when a user clicks on an item name, they are directed to the appropriate external url. This works using the code:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url %>
I would now like to add the ability to count the number of times a particular item name has been clicked on (i.e. in total for all users, not individual users) and therefore the number of times each external url has been visited in order to work out which is most popular.
Reading around, I believe i need to modify the code above to something of the form:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And need to define this function somewhere - it seems to only be found if located in 'application_helper'?
def clickcountplusone
clickeditem = Items.find(params[:identifier])
clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end
Needless to say, I cannot get this to work... My question is therefore, how can I set things up correctly so that when the link is clicked on the count is incremented? The other common problem people seem to report is that the number will be incremented each time the page is refreshed, which I would like to avod if possible.
Previously people have suggested adding to the 'show' section of the 'Items' controller, however, i don't know how this would work as the links are being clicked on the Showselecteditems view page, not the database itself where you get the show, edit, destroy commands. Any advice greatly appreciated.
This
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
will not point user to the some_controller#clickcountplusone, because you already specified an external link.
The easiest way to do this job is to modify your link_to like:
<%= link_to selecteditem.item_name.to_s, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And then to modify your actions source:
def clickcountplusone
clickeditem = Items.find(params[:identifier])
redirect_to clickeditem.external_url if clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end
In my view I want to display some right double angle quotes in my link.
Before Rails 3, this worked:
<%= link_to "» #{#category.name}", some_path %>
Now what should I do if I want to specify the » as html_safe but not the rest of the link's text?
In other words I do not want to do this:
<%= link_to "» #{#category.name}".html_safe, some_path %>
I do not want the #category.name treated as html_safe.
This produces the desired result:
<%= link_to "»".html_safe + " #{#category.name}", some_path %>
However, if I do this:
<%= link_to "#{#category.name}" + "»".html_safe, some_path %>
The output of the angle quotes is not treated as safe. I see » on my page and not ».
Why?
I tried extracting "»".html_safe to a helper method with the same results.
Is there a way to easily designate hard coded text/symbols as HMTL safe in Rails 3?
Thanks.
In this situation I often explicitly escape the unsafe part:
"» #{h #category.name}".html_safe
you need to make sure that the whole string is html_safe...
I'd recommend to try this:
"» #{h #cagegory.name}".html_safe