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 <%= ... %>.
Related
I have this code and I want to include glyphicon, but the code below is not working. If I remove the class subcomment_link, it will work, but I really need to include it for jquery. What is the correct syntax for link_to with remote with class specified?
<%= link_to new_subcomment_path(:response_id => response.id), class: "subcomment_link", id: "reply_new_subcomment#{response.id}", remote: true do %>
<i class="glyphicon glyphicon-pencil"></i>  Edit your profile
<% end %>
Try changing it to:
<%= link_to new_subcomment_path(:response_id => response.id), {remote: true}, {class: "subcomment_link", id: "reply_new_subcomment#{response.id}"} do %>
Usually the tag options come before the html options.
I am pretty new on Rails and Ruby and I cannot find the answer to add a span inside this link_to
<p class="withButtons" >
<%= link_to 'RĂ©pondre', lawyer_answers_path(#current_lawyer ? 'mon-compte' : ''), :class => 'button answerRevealer reply' %>
</p>
You can pass a block to link_to
<%= link_to lawyer_answers_path(#current_lawyer ? 'mon-compte' : ''), :class => 'button answerRevealer reply' do %>
<span>RĂ©pondre</span>
<% end %>
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 have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});
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" %>