Does link_to automatically pass :id when controller is specified? - ruby-on-rails

I have a link_to code
<%= link_to "#{(pages_counter/2) + 1}", { controller: "videos", action: 'videos_navigate', offset: pages_counter }, remote: true %>
When clicking on the link it's passing the id of the video automatically meaning I didn't explicitly pass a video object or an id via the link_to code. Is this happening because I directly identified the controller and the action as seen in the code above? Thanks in advance
sample URL generated: /videos/videos_navigate/1?offset=2

If you are on a show page, where the ID is in the URL already, and your link_to doesn't specify an ID, it will pick up the ID from the URL . The same thing would happen if you did not specify the controller, it would instead grab the current controller you are in.
So if you went to record 2 and click the same link, your URL will be /videos/videos_navigate/2?offset=2

Why don't you link directly to the route instead. As in, run rake routes int he console, it should print out an path name for your videos_navigate path... then you can presumably link to it like this:
<%= link_to "#{(pages_counter/2) + 1}", videos_navigate_path, :remote => true %>
If it doesn't already have a path, then you can give it one by adding :as => 'videos_navigate' to your route declaration for the action inside your config.routes.rb file. Read here for more information on routing and paths.

Related

link_to Base URL Randomly Changed

I'm using link_to to create a custom link. For some reason, my url always is wrong and randomly adds blog to the URL. I can't figure out why. It only does this while within the partial that I am rendering (3 times on the page).
I can share any code necessary, but where the hell would it randomly grab the blog URL?
The browser renders:
Select Rate
My view (using slim) is:
- for rate in loantek_rates
- loantek_closing_costs = LoantekClosingCosts.new(rate)
= link_to 'Select Rate', {controller: 'quote'}
Routes
#routes.rb
match "/blog" => redirect("http://blog.mywebsite.com"), path: '/blog'
resources :quotes, path: 'quote'
Set paths in link_to tag which you can get from rake_routes
Ex.
link_to "Profile", profile_path(#profile)

Move one page to another in Ruby on rails?

In my rails project there is a controller and view named "Welcome/index" where index is an action and another one named "home/page" . As i set root"home#page" as my root page. Now i want to transfer from "page.html.erb" into "index.html.erb" . How can i do that. And the code i written is below.Do i have to enter some thing in my controller class. please suggest.
these are the links that i tried. (How to create an anchor and redirect to this specific anchor in Ruby on Rails)
<a rel="nofollow" href="index.html.erb">Transfer to index</a>
You are not supposed to link to .html.erb files, you should link to the methods (not exactly the name of the method, but the name of the route) of a controller.
I strongly encourage you to review the ruby on rails MVC principles. You can read about routing and linking aswell.
Responding to your question, check out the command "rake routes". It will list the defined routes of your app and helps you to use them.
Try to replace your code by this:
<%= link_to 'welcome', welcome_path %>
<%= link_to "Link", controller:"controllername" %>
is the code you should use
You need to make sure a named route is defined for welcome/index and then can use the Rails helper link_to to automatically build your link for you in the view.
In routes.rb:
match '/welcome' => 'welcome#index', :as => :welcome
In your page.html.erb view:
<%= link_to 'Go to Welcome Page', welcome_path %>
If you want go for index action of Welcome controller then you can use:
<%= link_to "Transfer to index", welcome_path %>
Check the rake routes for path.
Plese refer link

Trying to call for a specific method - routing error

I have user and product models. User's table has a "discount" field. I trying to call method "apply_discount" from users/index view, which multiplies all prices and given discount.
users/index.html.haml:
- #users.each do |user|
# user data
= form_tag(action:'apply_discount', method: :put) do
= number_field_tag :discount
= submit_tag
UsersController has a method named "apply_discount".
My problem is routing error when I submitting form:
localhost:3000/assets?action=apply_discount&controller=users&method=put
No route matches [POST] "/assets"
Please explain me why this form requests assets. And how to make right request.
Having this in your url probably means your form tag has some incorrect syntax so I don't think your problem is a routing error.
?action=apply_discount&controller=users&method=put
should look something like
<%= form_tag(:controller => "foo", :action => "bar", :method => "post" %>
<% end %>
you should have a route setup for the action you are wanting to get to in the controller
check out this guide
http://guides.rubyonrails.org/form_helpers.html
Actually, I've found that if there is not a corresponding good route in routes.rb, it makes the url be:
/assets?controller=xxx&ampaction=yyy
If it finds the route you get the correct
/xxx/yyy
(assuming xxx is the controller and yyy is the action you specified).
So if you are building a non-REST-ful url you better have it in the route table first.

Ruby on Rails: link-to using post method but parameters are in the URL

I'm using
link_to 'My link', path(:arg1 => session[:arg1], :arg2 => session[:arg2],:arg3 => anyobject.id), :method => :post
but the HTML link being generated includes (arg1,arg2,arg3) as URL query parameters.
How can remove them? Did I miss something in the documentation?
A link_to will always put the arguments into the query string as it is creating a get-style HTML link - even if you put :method => :post that just appends an extra ("special") argument _method.
What I think you really want is a button_to link - which will make it into a sort of form-post. It works the same, but it says button_to instead (for example, button_to 'My link', path(:params => :go_here). The downside is that it will look like a button. But you can give it a CSS class (eg "unbutton") and then change the styling on that CSS class to make it not look like a button.
Alternatively, if what you really want is actually to have no params passed to the controller at all... then just don't include them when making your link (for example, link_to "My link" path - there's no need for :post if you don't want to post any params).
Finally, if what you want is for the params to become a part of the URL (for example, stuff/[param_1]/more_stuff/[param_2], etc.) then you need to update your routes to include these parameters as options. Have a look at the routing section of the rdoc for how to do that.
You can use below code, which rails.js need data-method to switch to post mode in Ajax.
<%= link_to '<button id="print" type="submit" class="btn">Print</button>'.html_safe, { controller: :lots, id: #lot.containername, action: "print_#{print_template}", remote: true }, {'data-method' => :post} %>

Ruby on Rails passing of parameters between views from link_to tag instead of submit_tags (having both on page)

I'm creating in my index page of my ruby on rails program, a list of the most commonly searched for terms in my database and hence each time a user selects a specific category this is written to another database.
What i would like it to create a hyperlink and pass a certain amount of parameters to a form like is usually done with a select_tag but instead with just a hyperlink, i would like to pass a set of hidden fields that i have on the page as well as what the user has selected.
To give you a better idea, basically i have the following structure in my program:
User inputs a search on (index.html.erb), user clicks on submit tag
action, user is taken to search.html.erb page and is displayed a set of refined categories + some fields, submit button,
user is taken to closest.html.erb (which uses parameters from the previous form by invoking the params[:searchSelected] and a few other params. )
I would also like to add this functionality:
Mimick this same operation, but instead of going in the search.html.erb, i would click on an already refined search category on the index.html.erb page (from a link_to , transmit as parameters which link_to the user has chosen + the hidden fields.
i Currently have this code
#stats.each do
|scr|%>
<%= link_to scr.category, :action => 'closest', :id => scr.category%>
I'm not sure if this is relevant, but i currently have the following routes in my routes.rb file
map.resources :stores, :collection => { :search => :get }
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
would anyone please assist me please? this is my first ruby on rails project and i would really like to find a way around this please
I am not sure if this is what you were thinking, but you can add additional parameters to the link_to tag. They are then available in your controller. So:
<%= link_to scr.category, :action => 'closest', :id => scr.category, :other_param => "test" %>
Will be available in your controller.
def closest
params[:other_param] == "test" #this will be true
end
i managed to resolve this by taking the params[:id] and then according to the value either set my own values (instead of the hidden ones in the index.erb which i had set manually anyway) and otherwise, continue as usual had i placed a regular search
View:
<%= link_to obj.ptc_devicename ,"/wiuconfig/hd?idval=#{obj.id.to_s}&val=#{#devicetype}",:value => obj.ptc_devicename,:id =>obj.id %><br/>
Controller:
#Heading= params[:val]
#id=params[:id]
value will be id is 2 and val is #devicetype

Resources