I have nested routes that I'm having difficulty formatting the way I want to.
My routes.rb has the following
resource :loan_application do
scope ":loan_application_id" do
resources :wizard, only: [:show, :update]
end
end
When I click the link to create a new resource:
<%= link_to business_loan_application_path(#user), method: :post %>
I get sent to a URL that looks like the following
http://localhost:3000/businesses/69/163/loan_application/wizard/eligibility
For some reason the loan_id (163) comes before /loan_application. I would like it to come after /loan_application.
When I rake routes I can see the same problem:
business_loan_application_wizard GET /businesses/:business_id/:loan_application_id/loan_application/wizard/:id(.:format) wizard#show
In my application I made the routes without your scope:
resources :loan_application do
resources :wizard, only: [:show, :update]
end
That leads me into exact the pathes I need
Related
I get a weird behavior when trying to do a super easy task:
adding
resources :something do
...
end
to the routes.rb generates all routes except #new.
when trying to browse 'localhost:3001/somethings/new' it handled by #show action.
I'm using rails 5.1.4 version. Any ideas?
======== UPDATE ========
I added s and still have the same problem: Here is the full code example and the result:
namespace :api do
namespace :webapp do
resources :user do
resources :documents
end
end
end
I was able to make it work when added:
resources :documents , only: [:show, :index, :update, :edit, :new, :create]
I have a controller, let's just call it FruitsController, that grabs all of the fruit and sends it to the index view. In the view, I want to show links to the individual pages for those fruits. I'm using the format:
<% #fruits.each do |fruit| %>
<%= link_to fruit.name, fruit_path(fruit) %>
<% end %>
And this works great when I have the route resources :fruits, but I don't want routes for deleting, saving, and updating, so I don't want to use resources. But when I just do individual routes for showing all and individual fruits, I get the error fruit_path function is not defined, and when I use the function fruits_path it works but it just appends a period to the path like /fruits.1. How can I use the fruit_path function without using resources? Thanks.
There are a number of ways you could do this; in your config/routes.rb, any of the following should work:
resources :fruits, only: :show
resources :fruits, except: [:index, :edit, :destroy, :update] # etc
get 'fruits/:id', to: 'fruits#show', as: :fruit
scope controller: :fruits do
get 'fruits/:id' => :show, as: :fruit
end
You're not limited to just resources, you can customize and create your own routes, for example:
get '/:username/photos', to: 'users#show', as: 'collage'
to: means controller/action, in this case users is the controller and show is the action.
as: creates a path for you 'collage_path'
you can find good info regarding routing =>http://guides.rubyonrails.org/routing.html
I have simple controller and routes file.
In my route and controller i have created a module. I wrote a simple method which is redirecting me show. I am not sure why.
Controller
module Seller
class CampaignsController < Seller::BaseController
before_action :confirm_logged_in
def viewAllCampaigns
#campaigns = Campaign.all
end
def show
end
end
end
Routes file
scope module: 'seller' do
#namespace :power do
resources :dashboard, only: [:index]
resources :sessions, only: [:create, :destroy]
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
get 'viewAllCampaigns' => 'campaigns#viewAllCampaigns'
end
Output
Started GET "/campaigns/viewAllCampaigns" for 127.0.0.1 at 2015-10-12 17:39:43 +0500
Processing by Seller::CampaignsController#show as HTML
Parameters: {"id"=>"viewAllCampaigns"}
Rendered seller/campaigns/show.html.erb (0.1ms)
I am hitting http://localhost:3000/campaigns/viewAllCampaigns in browser.
Ideally your routes should be defined like this.
resources :campaigns, only: [:index, :create, :show, :update, :destroy] do
get 'viewAllCampaigns', on: :collection
end
The first comment on the routes.rb file is The priority is based upon order of creation: first created -> highest priority. This is the reason your route is redirecting to show. Rails is treating this url as campain/:id.
Routes are tested in order, from top to bottom. The show route you've added for the campaigns resource will look for urls matching this pattern:
/campaigns/:id
/campaigns/viewAllCampaigns matches this, so it will do the show action., with params[:id] = "viewAllCampaigns"
Move the special case route up above the resources#campaigns route to fix this, then it will catch the url first.
get 'viewAllCampaigns' => 'campaigns#viewAllCampaigns'
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
It takes the following get request as a show action because show expects campaigns/:id, and it assumes 'viewAllCampaigns' is an id in this instance:
/campaigns/viewAllCampaigns
Your link_to should just be pointing to the following:
'/viewAllCampaigns'
Your route structure isn't really RESTful, but that's a separate topic.
I understand resource and path routes in rails 3 but I do not know is there any way to have both routes ? I try this routes but it not work for me, this is the routes:
resources :roles, only: [:index, :create, :show, :update]
get '/roles/:id' => 'roles#available_users'
How can we routes to use both routes ?
thankyou very much
Routes
What you're asking for cannot be done, as you'll be using the same "route" for different controller actions:
#config/routes.rb
resources :roles, only: [:index, :create, :show, :update] #-> domain.com/roles/:id - roles#show
If you then create another route for domain.com/roles/:id, Rails will just take the first which it finds in the routes file
--
The way to fix your issue is likely to be down to the following:
#config/routes.rb
resources :roles, except: [:edit, :destroy] do
get :available_users # -> domain.com/roles/:id/available_users
end
This will take you to the roles#available_users action, providing you with the ability to load the view you wish (to display the users for a particular role)
For a more defined explanation, I'd recommend checking out the nested_resources part of the Rails routing system
If I understand you correctly you want something like this:
resources :roles, only: [:index, :create, :update] do
get '/roles/:id' => 'roles#available_users'
end
Correct?
Just add an "do" behind the closing ] and an end after the custom routes.
Edit: Apparently I got wrong. ;) What you could do is:
resources :roles, only: [:index, :create, :show, :update] do
get '/roles/:id/available' => 'roles#available_users'
end
<%= link_to "Whatever", current_user %>
is linking to /user.id
My routes are setup like this
resource :user, except: [:index, :destroy]
So it should be linking to /user, right?
When I visit /user it says "Couldn't find User without an ID".
My user show action looks like this
def show
#user = User.find(params[:id])
end
The reason you are getting /user.id is because you have defined your routes as
resource :users, except: [:index, :destroy]
Note the singular resource which will create all the routes without any :id. As you don't have an accepting param within the route, the current_user that you are passing is matched to the format i.e., like .html, .js, etc. which in your case becomes .id
I would recommend using resources (note the plural)
resources :users, except: [:index, :destroy]
This will resolve the error Couldn't find User without an ID as you would be passing an params id within your route.
NOTE:
As per Rails convention, controller name should be plural. For UsersController, resources should be resources :users
I have had this happen many times. My fix is to use the path helpers.
<% link_to "Whatever", user_path current_user %>
This will drop the .id and make it /user/id