Currently I have a typical devise installation but I've added user-id's to the routes that have a current_user available. But I'm getting the error stated below when trying to login to the service.
Error
NameError in Devise::SessionsController#create
undefined local variable or method `offers_path' for #<Devise::SessionsController:0x007f0e80ec7a08>
def after_sign_in_path_for(resource)
offers_path
end
routes.rb
devise_for :users
resources :users do
resources :offers do
member do
put :tourcomplete
end
end
end
Rake Routes
tourcomplete_user_offer PUT /users/:user_id/offers/:id/tourcomplete(.:format) offers#tourcomplete
user_offers GET /users/:user_id/offers(.:format) offers#index
POST /users/:user_id/offers(.:format) offers#create
new_user_offer GET /users/:user_id/offers/new(.:format) offers#new
edit_user_offer GET /users/:user_id/offers/:id/edit(.:format) offers#edit
user_offer GET /users/:user_id/offers/:id(.:format) offers#show
PATCH /users/:user_id/offers/:id(.:format) offers#update
PUT /users/:user_id/offers/:id(.:format) offers#update
DELETE /users/:user_id/offers/:id(.:format) offers#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
Not too sure if this is the best way to put user-id's within the logged in routes. Would love any help on this issue.
Seeing your routes, you don't have any offers_path but you are trying to use that, that's why you are getting this error.
You should try using user_offers_path instead and the pass the user as the argument, something like this:
user_offers_path(#user)
Related
I have the following routes.rb
resource :user, :only => [ :edit, :update ] do
collection do
get :tax_info
get :payment_info
put :payment_info
and would like to reference the get payment_info via an rspec matcher like this:
expect(response).to redirect_to(user_payment_info)
What would be the proper way to reference that path? I have tried user_payment_info and edit_user_payment_info?
Edit #1
Show output of rake routes on this controller
You're missing the _path portion that rails generates for all routes. Your rspec matcher should look like this:
expect(response).to redirect_to(user_payment_info_path)
The information returned when running rake routes will look similar to this:
GET /users/:id(.:format) user#show
tax_info_user GET /user/tax_info(.:format) users#tax_info
payment_info_user GET /user/payment_info(.:format) users#payment_info
PUT /user/payment_info(.:format) users#payment_info
edit_user GET /user/edit(.:format) users#edit
user PATCH /user(.:format) users#update
PUT /user(.:format) users#update
You can see on the left hand side there is a column that denotes the prefix for the url and path helpers that rails is going to generate. You can see this defined in the Rails 3.2 routing documentation.
I'm new to Ruby and Rails and working my way through the Rails Tutorial. Early on, I realized that I accidentally created my model as Users rather than User, so I've been going with that ever since.
It hasn't been an issue until I tried to implement the sign up page, and now whether going to pages or running the test visit signup_path for the sign up page, I keep getting the following error:
undefined method `users_index_path' for #<#<Class:0x007f9e3e1d91b8>:0x007f9e3e1d1ff8>
I can't figure out what's going wrong here. When I run rake routes I get the following:
~/Coding/rails_projects/sample_app (sign-up*) ☔ rake routes
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
signup GET /signup(.:format) users#new
From that output, it looks to me like there should be a method users_index_path.
If it helps, my Users controller is the following:
class UsersController < ApplicationController
def new
#user = Users.new
end
def show
#user = Users.find(params[:id])
end
end
Using Rails 4.0.5.
Output from a page giving me the error:
Please keep in mind users_index_path is being called by Rails and not by me.
with
users GET /users(.:format) users#index
POST /users(.:format) users#create
you'll have a users_path method that will generate the /users path. That path will lead to either the index action (with a GET request) or the create action (with a POST request).
Under the hood, form_for #user will call the users_path method and set things up to fire a POST on submit (when #user is a new record). The URL helper method is calculated dynamically from the class name. This is ok if you're using Rails' defaults, but if you've defined custom routes you'll need to specify the URL explicitly.
With this out of the way, let's look at your problem.
A model with a plural name, e.g. class Users, will confuse rails.
When you pass #user to form_for, Rails will look at the class name, notice that it's a plural word, and try its best to deal with the ambiguity.
Normally it would be user_path for singular routes (show, update, delete) and users_path for the plural ones (index, create). Here, however, users_path must be used for the singular routes, and Rails will fallback to use users_index_path for index and create.
This would be all right.... but you have defined the routes using the default statement, probably with something like
resources :users
Which is correct, but not compatible with your model name.
Either rename your routes (bad) or rename your model (good).
I have the following error when trying to log out from an authentication gem I have just install.
http://0.0.0.0:3000/users/sign_out
Routing Error
uninitialized constant UsersController
I dont have a users_controller.rb file.
I do have a user.rb Model.
This is the path/url i am trying to reach:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
Any idea?
Your problem is that devise_for :users is overshadowed by resources :users
Rails complains about UsersController, because it thinks that you are trying to reach users#show (if you use GET request) and users#destroy (if you use DELETE)
You should either create UsersController or remove resources :users from your routes. And if you decide to create UserController, move resources :users under devise_for :users. Devise routes would take precedence that way.
Are you sure that you need resources :users?
I am trying to make a POST request to /api/kpi?data=some+stuff:
curl -i http://127.0.0.1:9010/api/create_kpi -F data="some stuff"
but I'm getting a 404.
My routes are:
# config/routes.rb
namespace :api do
resource :kpi, :except => [:edit, :destroy]
end
Which should hit my controller
# app/controllers/api/kpi_controller.rb
class Api::KpiController < ApplicationController
def create
temp = Kpi.new(params[:data])
end
end
So I am guessing the paths are not correct. Right? I am having a hard time understanding whether my route is incorrect, or the controller, or the call.
When you get a 404, check your routes. It usually means there is no route to the controller to reach. Routes are what makes the link between URLs and controllers. If your controller was getting hit, it'd either work or give you a runtime error.
Inspect your routes by running rake routes. It's a very helpful tool. It should give you something like this:
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
You can see that it gives you the mapping of what [method, URL] request will hit which [controller, action]. For example, here, POST /users will trigger action create of UsersController.
Given a controller/resource name, Rails will, by convention, go looking for the plural of that name. For example, given resources :user, Rails will go looking for UsersController in file app/controllers/users_controller.rb. (Path/file names have to match the name!)
#yfedblum talks about the use of singular and plural in Rails into more detail.
I have this setup for Devise
devise_for :users, :controllers => {
:confirmations => "confirmations",
:registrations => "registrations"} do
put "confirm_user", :to => "confirmations#confirm_user"
end
and when I run
rake route
I have get strange routes for registrations_controller specialy the edit_path
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
The problem is edit url for example for first user.
I expected
/users/1/edit
but I have get
/users/edit.1
I expect this route can not work but it does. Now I am not sure if I have made some mistake or if the devise generate the routes this way.
And if it generate routes that way where goes the format of request?
I can not believe that the URL might look like this.
/users/edit.1.js
Thanks for any advise?
The issue is not related to the edit url, instead it depends on the page that is linking to the edit one. You have probably a link of this form
link_to "Settings", edit_user_registration_path(#user)
that point to the edit url, which generates the unexpected url
/users/edit.id
You simply have to replace the link omitting the #user, as
link_to "Settings", edit_user_registration_path
That . is always there when showing a format. It's nothing from Devise, and there's nothing wrong with it. You're all good!
I am passing the id to the edit route but it doesn't expect an id.
The edit_user_registration_path is only for current_user so the user.id is unnecessary.
This question might be also helpful.
Devise: Allow admins to edit other users - Rails