Ruby on Rails link_to directing to Index instead of Show - ruby-on-rails

I am having trouble with some Ruby on Rails code. I have a data structure of nested resources (List has_many Tasks and a Task belongs_to a List). On my List.show page, I have the following line of code:
<td><%= link_to 'Show Task', list_tasks_path(#list, task) %></td>
The idea is that this page will link to a "show" page where I can view the details of an individual Task. I want the url to basically be
/lists/:list_id/tasks/:task_id
The problem is that the above code is directing me to
/lists/:list_id/tasks.task_id
on the index page of Task. How can I tell Rails to send me to the show page instead?
I have tried adding :action => show and :controller => tasks, as well as using show_list_tasks_path(#list, task). I also know that the structure itself is there because my show page works fine if I manually enter /lists/:list_id/tasks/:task_id.
Any help is appreciated. Thank you!
-Sum

The problem here is that you're using list_tasks_path when you should be using list_task_path.
The list_tasks_path method will generate a URL to the index action, as you know already, and will make the second argument be the format for this request.
For more information please read the Routing Guide.

A simpler way to specify this url would be
<%= link_to 'Show Task', [#list, task] %>
You can also do list_task_path(#list, task) (notice task instead of tasks).
Run rake routes to see exact names rails generates for your routes.

Related

Why ruby on rails link_to redirect to itself?

Lets say I have a controler test.
In test I define 3 actions:
def zah
end
def zeh
end
def zih
end
I have the views:
zah.html.erb
zeh.html.erb
zih.html.erb
and under routes.rb I have:
get 'test/zah'
get 'test/zeh'
get 'test/zih'
If I write under zah.html.erb, using code automaticaly created from rubymine IDE, this:
<%= link_to test_zeh_path%>
I will get my page source code with this:
http://localhost:3000/test/zeh
which makes the redirection from zah be itself.
running rake routes returns this:
Prefix Verb URI Pattern Controller#Action
test_zah GET /test/zah(.:format) test#zah
test_zeh GET /test/zeh(.:format) test#zeh
test_zih GET /test/zih(.:format) test#zih
Can anyone explain to me why is the link going to itself (from zah to zah) instead of another page(from zah to zeh)?
Edit:
I have found out that adding a name to a link makes the generated code works right:
<%= link_to 'zeh', test_zeh_path%>
I have seen the first usage (link_to test_zeh_path) here at 22:45.
Ruby on rails api does says that if nil name is passed then "the value of the link itself will become the name.".
As for a mistake of myself I was wondering why Dave Jones was able to create a link without a name, but he wasnt and that can be seen on his source code.
Because you have written the url in the display part.
You can simply do
<%= link_to 'Goto Zeh', test_zeh_path %>
and you will be good to go.

Pass data safely from view to controller in rails

I want to pass data from a view (link) to a controller so it can look up the related information. Services for a company, in this case.
I see examples where people have added to params like this:
<div>
<%= link_to 'Services', :controller => 'company', :action => 'services', :company_id => #company.id %>
</div>
...but that results in a transparent (unsafe) URL like this:
http://localhost:5000/company/services?company_id=17
Is there a way to get around this without stuffing data into the Session? What's the best practice on links inside an app that requires authentication?
THere is no such major harm in passing data like this in View.
Still if you insist on having, then check prettyurls:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Prior to we must have valid checks in controller & model files.
1. Valid Checks and redirection in Controller is helpful.
2. Depending on need adding validations in model can be a good support.
<%= link_to "Sign in", new_session_path(:id => Base64.encode64("1")) %>
and in your controller
def new
id=Base64.decode64(params[:id].to_s)
end
this is another form for create a link with data
check your routes with command un console rake routes
for more information read this documention
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Link_to efficiency without fetching record

In my Rails app there are experiment instances and each experiment has attachments, which are represented as binary blobs in the database and can be quite big. This question is about efficiency in coding a link_to show the attachment.
Originally I had this:
<%= link_to #experiment.attachment.file_name, #experiment.attachment %>
However I was told that the Rails app would be more efficient in rendering the page with
<%= link_to #experiment.attachment.file_name, {:controller => :attachments, :action => :show, :id => #experiment.attachment_id}, {:method => :get} if ! #experiment.attachment_id.nil? %>
The justification is that the first version fetches the attachment from the database, and the second one does not, making it is better, albeit longer and uglier. Is this true?
Both versions accomplish the same thing in directing the user to the show page for an attachment and I was under the impression the first is the default way to do a link_to a record show page.
Is there a way to shorthand the second piece of code to make it less terrible with code in the view?
Try using a rails route helper, use rake routes to view all your routes and then you can get something like this (don't forget to apped _path to route):
experiment_attachment_path(#experiment.attachment)
I would express this using the route path rather than using the ActiveRecord instance to load so the view can use its to_param method (which is what it is doing under the hood).
<%= link_to #experiment.attachment.file_name, attachment_path(#experiment.attachment_id) %>

link_to one page but controller from another in Rails

I've seen some similar questions on stack but I don't think this is a duplicate as each answer I've gottent has been specific to one underlying problem.
Ill keep it simple. I'd like to know how to click a link, have it take the user to one page but execute a def from another. This is in a rails application of a web crawler and Its something I thought would be quite simple but turned nasty on me.
At the moment I have:
<td><%= link_to 'Crawl!', crawl_path :controller => :crawl, :action => :crawl %>
This takes the user to the crawler's index page after its done running.
What I would like to do is redirect them to a different page. Namely jobs_path which shows a list of the crawlers sites and status (this is working fine, I just want to included it for relevance). If I try it like this:
<td><%= link_to 'Crawl!', jobs_path, :controller => :crawl, :action => :crawl %>
The user is directed to the jobs page but the crawler script never ran and thus the jobs list was never update. For the record, each job is just a url and the depth that it lies at.
Is this just a syntactical error or am I miles off the mark?
Any help appreciated.
What have you set your redirect_to in crawl#crawl as? Perhaps set it to redirect to jobs_path upon completion?

Calling a controller action with link_to

After playing around with links in Rails for a view hours i've managed to actually get a link to invoke a method in my controller. But i still don't understand why all my other attempts failed. Im hoping you could help me out with that.
I have the scaffold "Cars". When in the show view for a car, id like to click a link that invokes the method "drive" in my Car controller.
This WORKS: <%= link_to "Drive", drive_car_path(#car) %>
It seems this only works if i have this is my routes.rb:
resources :cars do
member do
get 'drive'
end
end
Why does <%= link_to "Drive", car_path, :method => :drive %> not work?
Do I need to put a GET in the routes.rb file for every method I create in my controller?
I can't seem to find any sites explain how to use links together with routes. They only seem to come separate. Do you guys have any easily understandable tutorials on this?
Try link_to "Drive", :controller => "car", :action => "drive"
Also, method is for choosing the HTTP method (GET, POST, ...). It's not method as in routine.
Be sure to check out Rails Routing from the Outside In and The Lowdown on Routes in Rails 3, they're both awesome resources.

Resources