link_to is not working with a variable url - ruby-on-rails

I have a view that uses link_to to pass parameters to a controller. The url is a variable. Something is not working. I'd appreciate any clues. Thanks!
<% url1 = dialogs_path(#dialogId) %>
<%= url1 %>
<%= link_to "Go!", url1(:uid1 => #uid1, :uid2 => #uid2), :id => "my_link" %>
url1 displays correctly. However, executing link_to crashes.

You should use dialog_path(#dialogId) instead.
You're trying to view a particular object, it's singular and what rails expects.
Take a look here :
http://guides.rubyonrails.org/routing.html#paths-and-urls
The way you use url confuses me, try something like this :
<%= link_to "Go!", dialog_path(#dialogId, :uid1 => #uid1, :uid2 => #uid2), :id => "my_link" %>

The url1 should be defined as:
<% url1 = dialogs_path(#dialogId)+'?uid1=' + #uid1 + '&uid2=' + #uid2% , :id => "my_link" %>
And the link_to should be:
<%= link_to "Go!", url1, :id => "my_link" %>

Related

How can I make link_to in this case?

I want the link which goes to
http://example.com/shop/:shop_name
so it should be something like this.
<%= link_to "Shop", req.host + "/shop/"+ #shop.shop_name , :class => 'btn' %>
I don't want to use something_path or something_url here.
I just want to create url link from current host, and variable.
How can I?
UPDATE:
<%= link_to "Shop", request.host + /shop/ +#shop.shop_name , :class => 'btn' %>
This takes me to
http://www.example.com/shop/www.example.com/shop/walmart
try with,
<%= link_to "Shop", "/shop/"+ #shop.shop_name , :class => 'btn' %>
Why don't you want to use url helpers? Doing it by hand is error-prone.
Simply put this to routes.rb
get "shop/:name", to: "shops#show", as: "shop_name"
Then you can use this in your templates:
<%= link_to "Shop", shop_name_path(#shop.shop_name), :class => 'btn' %>
In the show action of shops controller just fetch the name param:
shop_name = params[:name]

Rails : Linking an anchor from a different controller/view

<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails

Rails 3 - button_to update same page with controller#method

In my project I have the following form_tag to select a Site
<%= form_tag({:controller => "hvacs", :action => "index"}, :method => "get") do %>
<div class="field">
<%= select :p, :site_sel, #user_sites.map{|s| [s.name, s.id]} %>
</div>
<div class="actions">
<%= submit_tag("Select site") %>
</div>
<% end %>
This form_tag updates the index page through calling its method in the controller again.
I have the following button_to
<td><%= button_to 'Select', {:controller => "hvacs", :action => "select"}, :method => "get" %></td>
I would like to achieve a similar update with this as above rather than redirect to a new page with "select_path" etc, but the above does not seem to work.
How can I achieve this? Cheers!
OK, this looked so much like my AJAX problem, I tried to make it one!
I think all you need is a simple render statement in your select action
render :index
or
render :action => 'index'
But see http://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect_to for more.
The following solution worked. Apologies if I was not so clear on what I was looking for.
<%= button_to 'Select', review_hvacs_path(:h => hvac, :a => params[:a], :s => params[:s]) %>
I was trying to pass parameters with the button, while staying on the review page.

How can I make search result to be in a shortened url?

Rails guides show the following example as a generic search form.
<%= form_tag(search_path, :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
Things work fine but, if I want to find something that has "251", the resulting url from the above looks like
app.com/searches?utf8=✓&keywords=251
How should I modify the code such that the resulting url will look something like
app.com/searches/251
How about redirecting from the search action if the param[:keywords] exists like this:
redirect_to( :action => "searches", :id => params[:keywords] ) and return
Depending on how your routes are setup.

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