I tried to add a simple HTML class to my link_to tag:
<%= link_to "Individual Images", action: 'feature_highlights', site_title: site.title, class: "btn" %>
..but it gets appended to the URL instead:
How do I add a class to my link_to tag?
As per the API docs:
Be careful when using the older argument style, as an extra literal
hash is needed
So, the solution was
<%= link_to "Individual Images", { action: 'feature_highlights', site_title: site.title }, 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 trying to create a helper method that can have optional arguments for link_to method. My intention is to create a helper method for different cases:
# Typical case #1 (can have any number of extra arguments)
<%= toolbar_item('google.com', 'Google', 'globe', 'btn-primary', target: '_blank') %>
# Typical case #2 (extra arguments are optional)
<%= toolbar_item(root_path, 'Start', 'flag', 'btn-primary') %>
Follows the code:
def toolbar_item(url,text,icon,custom_class, optional_extra_settings = {})
link_to raw("<i class='fa fa-#{icon}'></i> #{text}"), url, class: custom_class, optional_extra_settings
end
It's no good. link_to method does not recognize the extra_settings and raises and error.
Any ideas?
Thanks!
The link_to method accepts only 3 arguments. The last argument needs to be a hash. Therefore you have to merge your class setting with the optional extra settings hash.
Change your example to:
def toolbar_item(url, text, icon, custom_class, optional_extra_settings = {})
html_options = { class: custom_class }.merge(optional_extra_settings)
link_to raw("<i class='fa fa-#{h icon}'></i> #{h text}"), url, html_options
end
Furthermore, you will notice that I used h to escape your icon and text. Just to be safe, because you disabled the auto-escaping that is usually done by Rails by using raw.
Don't reinvent the wheel. The CSS class is an option you can pass to the link_to helper via the options hash. Let's move it to the options hash and remove one needless argument.
# notice class: 'btn-primary' vs 'btn-primary'
<%= toolbar_item(..., class: 'btn-primary', target: '_blank') %>
Now, link_to also accepts a block. Use it to simplify your code pass the icon ('globe' or 'flag', etc...) as a block.
def toolbar_item(url, text, options = {}, &block)
if block_given?
link_to url, options do
yield
end
else
link_to text, url, options
end
end
Now, each time you use the helper with an icon, you can specify the icon you want:
<%= toolbar_item 'google.com', class: 'btn-primary' do %>
<%= content_tag :i, class: 'globe' %> Google
<% end %>
Which begs the question. Do you really need a helper after all? All we did was create a wrapper. You can just do:
<%= link_to 'Google', 'google.com', class: 'btn-primary' %>
<%= link_to 'Google', class: 'btn-primary' do %>
<%= content_tag :i, class: 'globe' %> Google
<% end %>
I'm having a situation here with my bootstrap buttons. I cannot get the styling to apply even though the css for the theme itself is working.
Here's my code
<%= button_to 'Sign Up', class: 'btn btn-green' %>
I tried another method, but this one gives me errors since the view isn't generated yet. But, I wanted to do was add html files that I already created for another website to the project. To use them as a view.
<%= button_to("View Web Page", {action: "Web_Page_To_Be_Used"}, class: "btn btn-primary") %>
Please, show me the best method.
UPDATE:
The code below has the best format for the link. The one I used was incomplete. For the placeholder text, you have to create a new route first. And then include it into the button's link.
= link_to 'Register', new_user_registration_path, class: 'btn btn-green btn-md' %>
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
html_options is the third argument of button_to so your second example is closer, you're probably getting an error because action expects a route.
<%= button_to("View Web Page", {action: "show"}, class: "btn btn-primary") %>
I feel like I'm abusing form_for (and simple_form_for) to update a single hidden attribute of a record. Here is an example:
<%= simple_form_for :present, url: present_path(list_item), method: 'put' do |f| %>
<%= f.hidden_field :ordered, value: "1" %>
<%= f.button :submit, "ordered", class: "btn btn-mini" %>
<% end %>
Essentially this presents a single button that a user can press to mark a present as 'ordered'. On the back end, it updates the attribute :ordered to the value 1 for the current list_item.
I feel like this is a cheat because its not a proper form per se. I'd much rather have a single link_to which would when clicked updated the attribute.
I imagine the link_to would need to be method: aware to update the attribute.
Is it possible to replace my simple_form_for with a single link_to?
it is, use
link_to 'Ordered', present_path(list_item, ordered: 1), method: :put
Try using:
link_to 'Ordered', present_path(list_item, present: {ordered: 1}), method: :put
When I try to use button_to for link smthing - rails add to that button tag and my design is broke. How i can fix that? For example:
<%= f.submit %>
<%= button_to 'Back', resumes_path %>
I know that i can use somthing like link_to, but in this situation i need to use button_to. Thanks.
Please create any class name or div id and write button css.
<%= link_to 'Back', resumes_path , :class => "button_css" %>
and write style,
.button_css
{
// your button style...
}
Edit : Try this
<% content_tag :button :type => :submit do %>Button<% end %>
This will create a button, you can then add onclick event to redirect the page.