In my rails 4 erb view i have this link,
<%= link_to "Download #served_file.title", "/public/uploads/#served_file.file", :class => "btn" %>
However, it won't replace #served_object.title and #served_object.file with the text but just placing those lines inside a <%= %> works as expected.
You need to interpolate the values into the string.
<%= link_to "Download #{#served_file.title}", "/uploads/#{#served_file.file}", :class => "btn" %>
Related
I'm very new to Rails so I'll try and explain this the best I can. I have a simple Products model with one of the fields being the name of the Stripe plan name. This is so a new product can be added and the selection will append the plan name to the end of the path. It works great hard coded for testing.
Heres my products/show.html.erb line in questions. I am only including this for now as any experienced Rails developer should see the issue.
products/show.html.erb hard coded
...
<%= link_to 'Subscribe', new_subscription_path(plan: 'monthly'), class: 'btn btn-primary' %>
...
products/show.html.erb what I need
...
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= #product.stripe_name %>' ), class: 'btn btn-primary' %>
...
Having <%= #product.stripe_name %> in as a string throws an error.
The desired outcome would look like this in the browser
http://127.0.0.1:3000/subscription/new?plan=monthly
This:
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= #product.stripe_name %>' ), class: 'btn btn-primary' %>
Should be this:
<%= link_to 'Subscribe', new_subscription_path(plan: #product.stripe_name), class: 'btn btn-primary' %>
You don't put <%= ... %> inside another <%= ... %>.
I'm following this SE question to put a form in a bootstrap modal with rails.
The person who answered the question states: "Make sure that you have a unique id or class for every modal body.". So I am trying to put a unique id number in my link_to:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "<%= post.id %>-my-modal" %>
But this is causing an error. If I take out <%= post.id %> I do not have an error, but the modal behavior does not work.
How can I add the post.id with embedded ruby to the link to?
You have to write it like this:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Once you open a <% ... %> tag, you are writing ruby code. What this means is that you can't nest <% ... %> tags inside another <% ... %> tag because these tags aren't ruby syntax.
Inside the tag, to do string interpolation, use normal ruby methods:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Only mistake is putting another (ruby) tag <% %> inside the same tag.
In erb (embedded ruby) one <%= %> tag cannot have another tag inside.
This code result a syntax error.
<%= link_to "Edit", .... "data-target" => "<%= post.id %>-my-modal" %>
If you plan to output/render data inside the tag and quoted as string,
used #{put_data_hare}
Output like:
<%= link_to "Edit", .... "data-target" => "#{post.id}-my-modal" %>
In other case, you can do like:
post.id.to_s + "-my-modal"
I can't manage to make it work... even using raw or html_safe
view
<%= button_to(glyphicon('heart', 'I love it !'), some_path, class: "btn btn-success")%>
helper
def glyphicon(glyph, text = nil)
html = "<i class=\"glyphicon glyphicon-#{glyph}\"></i>"
html += " #{text}" if text
html.html_safe
end
result
A success btn with <i class= and " /> after
The following works (I've been doing it for ages), but the extra do syntax is annoying...
<%= button_to(some_path(#etude), class: "btn btn-success") do %>
<i class="glyphicon glyphicon-heart"></i> I love it !
<% end %>
EDIT
Found a more compact syntax :
<%= button_to(some_path(#etude), class: "btn btn-success"){
glyphicon('heart', 'I love it')} %>
I don't think its a problem with html safe, rather the name attribute for button_to will only accept plain text.
<%= button_to("<b>hello</b>".html_safe, 'http://www.google.com', :class => 'button', :method => 'get') %>
and
<%= button_to("<b>hello</b>", 'http://www.google.com', :class => 'button', :method => 'get') %>
both produce a button with <b>hello</b> written on it rather than just hello in bold.
The method using do is the only way to do it, it's the same thing you need to do when linking glyphicons via link_to.
But you could do this.
<%= button_to(some_path(#etude), class: "btn btn-success") do %>
glyphicon('heart','I love it')
<% end %>
I've been toying with using icons instead of standard buttons. I like the look of it and occasionally a icon is clearer than a labelled button.
While I found it easy to implement for link_to calls;
<%= link_to raw('<i class="icon-exclamation-sign"></i>'), '#' %>
I am struggling to achieve the same result when nested with form_for (or simple_form_for). Is there a way to use an icon for a submit or input when working with the form?
Here are some example forms from my code using the standard buttons:
<%= simple_form_for :present, url: present_path(list_item), method: 'put' do |f| %>
<%= f.hidden_field :purchased, value: "1" %>
<%= f.submit "owned" %>
<% end %>
<%= form_for :present, action: 'new', url: presents_path do |f| %>
<%= f.hidden_field :purchased, value: '0' %>
<%= f.submit "List", class: "btn btn-mini" %>
<% end %>
I've tried a few methods but none of yielded just the icon acting as a button - most render the button with either the raw HTML as its contents or the icon within the button.
The end result I'd like to be able to implement is just the icon acting as a click-able item (similar to the link_to result).
you probably want to look at image_submit_tag http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-image_submit_tag
I would like to produce this link:
<span class="magnifier icon"></span>Search
So as far as I understand it I have to convert
<%= link_to "Upgrade", :settings, :class => "button" %>
To a block, but when I do this:
<%= link_to "Upgrade", :settings, :class => "button" do %>
<span class="magnifier icon">Search</span>
<% end %>
It gives me the following error:
undefined method `stringify_keys' for :settings:Symbol
However, when I do: <%= link_to "Upgrade", :settings, :class => "button" %> it works perfectly.
How do I convert this to a block ?
When using a block, the first argument no longer contains the link content. The block is responsible for that. Therefore, it thinks that "Upgrade" is your link's destination and :settings is your options hash.
Remove "Upgrade" and put it in the block.