How to include class in link_to remote? - ruby-on-rails

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>&nbsp 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.

Related

How to construct a clickable link with a scope using Ransack and Rails

I have the requirement to have some scopes as clickable links in my application. This will allow the user to change the data they are seeing as required. Using Ransack and it's ransackable_scopes functionality I am very close. I do need to retain any filtering Ransack has done when the user clicks the scope.
I've got the scopes working but now I just need to construct the clickable link.
Here's my model:
class Product < ActiveRecord::Base
scope :upward_trending, -> { where( "status > ?", 100).where(above_revenue_average: true).order('end_date DESC') }
scope :downward_trending, -> { where( "status < ?", 100).order('end_date DESC') }
def self.ransackable_scopes(auth_object = nil)
[:upward_trending, :downward_trending]
end
end
Now in my view I've added these two scopes as hidden fields, like so:
<%= search_form_for #q, :html => {:class => 'filter-form'} do |f| %>
<div>
<%= f.hidden_field :upward_trending %>
<%= f.hidden_field :downward_trending %>
<%= f.label :name_cont, "Search", class: 'label' %>
<%= f.search_field :name_cont, class: 'form-control input-box', :placeholder => 'Search' %>
</div>
<div>
<%= f.submit "Filter", class: 'btn btn-primary' %>
<%= link_to "Clear Search", request.path, class:"btn btn-default" %>
</div>
<% end %>
From here I just need to create the links and it should work.. What is the best way to do this?
Thanks for your help!
I was planning to do dirty way. (But haven't yet)
create search_form_for every scope (you will have 2 form for your case)
set hidden field with own criteria (as you do in your code but each in own form)
make submit button looks like link (with css I think it is not very difficult. You can see Bootstrap button appeared as link)
Not very clean or elegant.
I achieved this by creating hidden fields for each scope and then creating a button with onclick javascript:
<%= f.hidden_field :upward_trending %>
<%= button_tag(:type => 'submit', :class => 'btn btn-primary scope-button upward_trending', :id => "upward_trending", :onclick => "document.getElementById('q_downward_trending').value = 0; document.getElementById('q_upward_trending').value = 1;") do %>
<i class="fa fa-chevron-up fa-2x"></i><br>Upward<br>Trending
<% end %>

undefined method for #<#<Class:0x007f05d8ec12d0>:0x007f05d8fa9be8> in Rails

Hello I am beginner to ruby on rails and I am making a form for taking student's info. I am facing the problem here:
My view code is:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#userinfo) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
My controller code is:
def new
#userinfo= Userinfo.new
end
My routing is:
get 'about' => 'userinfo#new'
I am getting the error: "undefined method `userinfos_path' for #<#:0x007f05d8fa9be8> "
I am beginner to this. How to solve this problem. Need help. Thank you.
You don't have a route for userinfos.
try adding in your config/routes.rb:
resources :userinfos
I hope this help.
The reason why it gives you exceptions is you're using form_for(#userinfo) simply in your view which intelligently generates form action for you.
Simply, you can specify the form attributes, like
form_for(#userinfo, url: '/about/create'), for more Rails way, you'll need to add method: :put
Here's the example:
ruby
form_for #userinfo, url: '/about/create', method: :put, html: { class: 'form-horizontal' }
Otherwise, #Archie Reyes 's answer works great.

html_safe not working with rails

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 %>

how to link form_tag to button rails 4

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();
});

adding a class to a link_to is breaking the link

I'm using link_to in RoR 3
When I use it like this, it works fine:
<%= link_to "Add to your favorites list",:controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}" %>
But I would like to pass in a class as well
however, this is not working for me. The class works, but it breaks the link. Any ideas?
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
try this
<%= link_to "Add to your favorites list", :controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
{ :class=>"ui-button-text button_text" } %>
Since the :class should be in :html_options (refering to API)
link_to(body, url, html_options = {})
The proper way of doing what you have is as follows:
link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"
As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:
link_to "Foo", favourite_companies_path(#company), :method => :post
What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:
link_to "Foo", :company_id => #company.id, :company_name => #company.name
As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.
I'm using a link_to do-end block so the above previous solutions didn't work for me.
If you want to embed other tags in your a tag, then you can use the link_to do-end block.
<%= link_to favourite_companies_path(:company_id => #company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
<i class="fa fa-star"></i>
<%= #company.company_name %>
<% end %>
In this case it's
<%= link_to path(url_params), html_options = {} do %>
<% end %>
Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like this
<%= link_to "Add to your favorites list",
{ controller: "favourite_companies", action:"create"},
company_id: #company.id,
company_name: #company.company_name,
class: "ui-button-text button_text" %>

Resources