Rails 3 with devise undefined method `users_url` - ruby-on-rails-3

I'm making some small changes to devise in order to overwrite the layout used and some other things so I decided to create my custom routes with controllers. I only added this to the routes so far:
devise_for :users, :controllers => {
:sessions => "sessions/user",
:registrations => "registrations/user"
}
which is supposed to load the login page when accessed by /users/sign_in and it does it very well. When I try to ajax post to that url in order to validate the user input and login I get this error: NoMethodError (undefined method 'users_url' for #<Sessions::UserController:0x000000041bed98>):
The controller is basic and nothing was changed to overwrote devise logic:
class Sessions::UserController < Devise::SessionsController
layout "login" # The only addition. Rest is handled by devise
end
I had this working for a good while but today I decided to use devise for another model (admins) and I generated it's files using the devise command rails generate devise Admin which completely messed my working code of logging in the users.
Any ideas?

Related

Rails 3 + Devise 2 + JsonP / Callbacks

I'm working on a PhoneGap mobile app that communicates with a rails3 server using a REST api and json. Authentication is done using devise 2.0.4.
For my own controllers I can specify that rails should wrap the json with the callback to handle the Cross-Domain problem by the following:
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #books, :callback => params[:callback] }
end
notice the:
:callback => params[:callback]
I'm unable to do that with the devise controllers.
How can I get devise to respond to js requests - i.e. json with a callback?
Thanks a lot,
Ariel
It would be better to override devise controllers providing the :callback parameter to renderer or using before_filter somehow. In order to do that copy devise controller(s) from github/local-gem-directory to your app and then edit. But it would be much easier to define a JS views for each action:
If you ain't done it already start with generating devise views (which is not necessary but would help you to get the idea):
rails g device:views -s
It would generate lots of *.html.erb files inside of your app/views/devise folder. You have to create appropriate *.js.erb next to them with following contents:
<%=render :inline => params[:callback]+'('+resource.to_json+')'%>
PS: Actually you may just put that line into app/views/application.js.erb and skip other steps;)

Defining own Devise controllers prevents Devise using custom views

I used config.scoped_views = true in initializers/devise.rb to make Devise use my own views rather than its default views for my two roles (Admin and Subscriber). This worked great, and I went ahead and customized my views.
Then earlier today I added my own controllers that subclass Devise's controllers and told devise to use these controllers in routes.rb using:
devise_for :subscribers, :controllers => {
:registrations => "subscriber_registrations",
:sessions => "subscriber_sessions",
:passwords => "subscriber_sessions"
}
This works nicely, however it seems to have had the unwanted side-effect of making Devise revert back to using its default views for my Subscribers role.
In my logs when I load the login page for Subscribers I can see this:
Rendered /Users/myName/.rvm/gems/ruby-1.9.3-p0/gems/devise-2.1.0/app/views/devise/sessions/new.html.erb
Am I doing something wrong? Do I now need to manually add all the view rendering because I've defined my own controllers?

change routes in devise

Im working with ruby 1.9.2, rails 3.1.3, devise 1.5.3, I need to change the devise routes, I already changed the default routes, like login and logout, but I need to change the others routes, for example:
http://localhost:3000/password/new
I need that, when the user click the link did you forget your password? go to:
http://localhost:3000/recovery_password
in my routes I tried:
get "recovery_password", :to => "devise/passwords#new"
get 'recovery_password' => 'devise_passwords#new', :as => :new_user_password
but don't works, please help.
thanks in advance.
try this. It should works. ;)
# change :devise_model to :users, or :admins or any name of your devise model
devise_for :devise_model do
get 'recovery_password' => "devise/passwords#new"
end
and you can use this in view like this.
link_to 'Forgot you password?', recovery_password_url
PS. if you have customize devise controller. You should tell the router first, and change devise controller to your customize controller name.

RJS with Devise gem

What would be the proper way to set Devise gem (ruby on rails v3) in RJS style (example: http://railscasts.com/episodes/43-ajax-with-rjs)?
I usually create a create.js.erb file with alert(#error). When using devise I don't know how to properly get #error value. I would like to display all messages as javascript alerts.
I think, to achieve this you will need to write your own controllers (inherited from devise) and views. And then add respond_to :html, :js to this controllers. After this, controllers will be able to respond to javascript requests and you can manage your alerts.Read doc for how to create custom Devise views here: Devise

Rails app doesn't see my views

I've on a while on rails now and here's the problem I've been having on and on:
When I create a controller through:
"rails generate controller ControllerName ViewName"
I get everything working as I want but if for some reason I create the controller through:
"rails generate controller ControllerName"
and then just add ViewName.html.erb to the folder inside views that has the same name as my controller things would go wrong.
So the concrete case is me writing:
rails generate controller Subjects list show.
Which creates for me:
1.controllers>subjects_controller.rb
2.views>subjects>list.html.erb
3.views>subjects>show.html.erb
So this whole thing works fine.But as I already said if I need another view; let's say "new" I just add "new.html.erb" next to the other *.html.erb files and an action:
def new
end
to my subjects_controller.rb then it won't work.
The two previous views would keep working but any other "*html.erb" created outside the command line wouldn't.
Is there anywhere else where info about views is being stored?.
I'm a Windows 7 user (32 bit).Rails version=3.0.3. WebServer=WEBrick.
Text editor = E-TextEditor
This is most likely caused by your routes not being correctly configured. So it would be helpful to see the content of your routes.rb
In your case I think the best way to configure the routes is to use the resources mapping:
resources :subjects
This will by default create routing for the standard RESTful actions :index, :show, :edit, :update, :new, :create and :destroy.
For more detailed information about the routing, I would recommend Rails Routing from the Outside In