Can't get link_to to work on nested resource - ruby-on-rails

There seem to be a few questions like this but I have read all of them but they don't help.
I have this in views/towns/index.html.haml:
= link_to "towns attractions", towns_attractions(town.id)
I have done an inspect on town.id and the id is correct.
My routes seem okay because I can go to
http://127.0.0.1:3000/towns
and I can go to
http://127.0.0.1:3000/towns/1/attractions
when I take my link_to out (because it breaks).
However, I get this error in index.html.haml:
undefined method `towns_attractions'
I have tried all combinations of pluralization with no luck!
My routes:
resources :towns do
resources :attractions
end
rake routes:
town_attractions GET /towns/:town_id/attractions(.:format) attractions#index
POST /towns/:town_id/attractions(.:format) attractions#create
new_town_attraction GET /towns/:town_id/attractions/new(.:format) attractions#new
towns GET /towns(.:format) towns#index
POST /towns(.:format) towns#create
new_town GET /towns/new(.:format) towns#new
edit_town GET /towns/:id/edit(.:format) towns#edit
town GET /towns/:id(.:format) towns#show
PUT /towns/:id(.:format) towns#update
DELETE /towns/:id(.:format) towns#destroy
All I want is a link that goes to http://127.0.0.1:3000/towns/1/attractions but where 1 is replaced by town.id.

towns_attractions should be town_attractions_path. That's it.

Related

Routing error in R

I'm new to Ruby and Ruby on rails and have hit a problem with routing. I have 3 controllers, application controller, a bike controller and a ride controller. My routing table is as follows:
Rails.application.routes.draw do
get 'cycle_tracker/index'
resources :rides
resources :bikes
root 'cycle_tracker#index'
When I run rails routes it brings the following:
Prefix Verb URI Pattern Controller#Action
cycle_tracker_index GET /cycle_tracker/index(.:format) cycle_tracker#index
rides GET /rides(.:format) rides#index
POST /rides(.:format) rides#create
new_ride GET /rides/new(.:format) rides#new
edit_ride GET /rides/:id/edit(.:format) rides#edit
ride GET /rides/:id(.:format) rides#show
PATCH /rides/:id(.:format) rides#update
PUT /rides/:id(.:format) rides#update
DELETE /rides/:id(.:format) rides#destroy
bikes GET /bikes(.:format) bikes#index
POST /bikes(.:format) bikes#create
new_bike GET /bikes/new(.:format) bikes#new
edit_bike GET /bikes/:id/edit(.:format) bikes#edit
bike GET /bikes/:id(.:format) bikes#show
PATCH /bikes/:id(.:format) bikes#update
PUT /bikes/:id(.:format) bikes#update
DELETE /bikes/:id(.:format) bikes#destroy
root GET / cycle_tracker#index
In my main view I have the following (I'm simply trying to create a link from my main page to rides/new.
<%= link_to 'rides', :controller => new_ride_path %>
If I try and access http://127.0.0.1:3000/rides/new then it works as expected. However, if I simply try and access http://127.0.0.1:3000 then I get the following:
showing D:/Dev/CycleTracker/app/views/cycle_tracker/index.html.erb where line #2 raised:
No route matches {:action=>"index", :controller=>"rides/new"}
If I try to use new_ride_url instead of path I get the following:
Showing D:/Dev/CycleTracker/app/views/cycle_tracker/index.html.erb where line #2 raised:
No route matches {:action=>"index", :controller=>"http://127.0.0.1:3000/rides/new"}
I imagine this is probably a fairly straightforward issue, but any help appreciated.
<%= link_to 'rides', new_ride_path %>
Try that - you don't need the :controller => part
if you want to use the controller-action style you need to do this:
<%= link_to "Rides" , controller: "rides", action: "new" %>

rails link_to using get instead of post

I'm making a website for a class and I'm trying to implement a friend request function with a model called 'Users' and a join model called 'Relationships'. I have a button on the user#show page that should add a friend by using the create method in the Relationships controller. Here is the code for the button:
<%= link_to "Add as Friend", relationships_path(:friend_id => #user), method: :post %>
When I press the link, however, it tries to access the index method instead. After looking in the console, it looks like the link is sending a GET request, which routes to the index method, instead of a POST request, which routes to the create method. Can someone explain why this error is occurring and how I can fix it?
Edit: As requested, here is what I have in my routes:
Rails.application.routes.draw do
resources :interests
get 'interests/create'
get 'interests/destroy'
get 'home/index'
get 'sessions/create'
get 'sessions/destroy'
resources :users
resources :relationships
resources :subscriptions
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'home#index'
get "/auth/:provider/callback" => "sessions#create"
get "/signout" => "sessions#destroy", :as => :signout
Using a link_to helper indicates to Rails that you'd like to produce an a tag in your HTML. No element of the HTML specification regarding a tags allows for producing POST requests. Because Rails understands the utility of allowing for POST and DELETE requests to be issued using links, however, it provides those options in the link_to helper. It's implementation, though, must use JavaScript under the hood in order to appropriately function.
Check that jquery-ujs is installed, and that your asset pipeline is working correctly in order to use the helper in this way.
You may also evaluate whether using a form_for and a button is better, since that will automatically POST.
I'm pretty sure you are matching the wrong route. Run rake routes and see the route that links to the Relationships#create.
Using 'url' instead of 'path' with the route helper solved the problem for me. So instead of 'relationships_path' use 'relationships_url'.

Polymorphic Routes in Rails 4.1.4

I have a polymorphic association between Posts and Postables, right now with Projects being my only Postables. In my routes, I have:
resources :projects do
...
member do
resources :posts
end
end
I know how to retrieve the right ids from the parameters, and all of my controller specs pass just fine, but when I try to write links in my views, they don't work. Running rake routes, I see a little weirdness:
...
post SHOW /projects/:id/posts/:id(.:format) posts#edit
...
And so on for the rest. If I'm not mistaken, the path should be 'new_project_post', and the first parameter should be :project_id.
Now, in my show view for Projects, I have the index of posts for that particular project. But the links don't work. Lets assume I have a project with an ID of 2, and a post for that project with an ID of 1.
If I try link_to 'Edit', edit_post_path(#project, post), the link comes out as .../projects/1/posts/1/edit, so both :ids get the post's id. If I swap post and #project, both :ids will be the project's id.
If I try passing them as an array, link_to 'Edit', post_path([post, #project]), the resulting link will be .../projects/1%2F2/posts/1%2F2/edit. %2F is a URL-encoded slash character, so I'm not sure what the hell Rails is trying to do here.
If I try using polymorphic_path([#project, post]) for my links, all it does is spit out paths that don't exist: undefined method 'project_post_path'
I've tried several other combinations of parameters without success. So if anyone could point me in the right direction, I'd be extremely grateful.
The appropriate syntax for nested resources in Rails is:
resources :projects do
resources :posts
end
In member block you could only declare additional actions to work with project instances.
You are using nested resource inside member and it is incorrect. Read more about this here: http://thelazylog.com/posts/polymorphic-routes-in-rails

Simple routing error

Total noob question.
I am building a simple photoblog in Rails which consists of Posts. Each post has its own id "/posts/1". I built Posts using rails scaffolding.
The problem is that I am unable to go to a url such as "/posts/index" or "/posts/anything" because it's trying to match anything after "/posts/" it to an id... So I get back an error like:
Couldn't find Post with ID=index
I'm sure this could be fixed with routes, but I'm not really sure how and I feel that there is some big-picture problem I am missing here.
You can clone my app from here: https://github.com/tbhockey/PhotoBlog
Thank you.
Running rake routes is usually pretty helpful. In this case, the route to your index should just be /posts (not /posts/index). If you run rake routes you'll probably see something like
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
That's trying to tell you that the URL /posts will be directed to the index action of your posts_controller. Also, this may be referred to in your code as posts_path or posts_url.
Try to do this for your routes.rb :
scope :path => '/posts', :controller => :posts do
get 'show/(:id)' => :show, :as => 'show_post'
end
For a /posts/show/1, it will call the show action of the posts controller.
It's always a nice idea to learn about how routing works in Rails. It's very important and will help you understand many things about Rails. Especially looking at named routes is vital for good Rails programming.

Nested Route link_to helper working in one place but not another

I have a nested routes problem that I can not figure out. I have an app that has nested routes like so:
resources :events do
resources :sessions
end
I am trying to use the following link_to in my code:
<%= link_to "New Session", new_event_session_path %>
When I run rake routes, it will show the proper URL with a GET method existing:
new_event_session GET /events/:event_id/sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
When I try to use the link_to in one place it works, when I try to use it in another place it does not, it instead will give me this error:
No route matches {:controller=>"sessions", :action=>"new"}
The only difference between the two files is the location of the files in the app (one is under views/events the other is under views/sessions and the url being called:
/events/1 --vs-- /events/1/sessions
I am still a noob with rails, so this is probably a stupid question, but I have hit a bit of a wall. Any help is appreciated.
You just need to pass event object to path helper:
new_event_session_path(#event)

Resources