RJS with Devise gem - ruby-on-rails-3

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

Related

Rails 3 with devise undefined method `users_url`

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?

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;)

Devise: how can I access the helper methods in all my controllers

I'm trying to implement a devise sign in form in my nav bar header(twitter bootstrap), but it just tells me that resource isn't a defined method.
Do I need to somehow inherit the devise helper methods to achieve this?
When it comes to creating a custom sign in page, it doesn't have anything to do with your controller, it has to do with accessing things in your view, through the helper of helper methods. You want to add them to your application_helper.rb file. There is an overview of the method here:
https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app
In a nutshell, you basically want to add this to yours application_helper.rb
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
I have used this a bunch of times in all my projects to make custom sign in forms in the navbar when I use twitter bootstrap. It works great and doesn't require you to change any other code anywhere else.

Rails form helpers with devise

I am using rails 3.2.3 with devise to sign up and sign in users.
The problem is that devise (I might be wrong here) somehow overrider the interpretation of form helpers where an error in the form occures (for example, the registrations/new form). It puts the labels and the inputs, wich are wrong in some kind of div blocks and ruines the view.
How can I customize that behavior?
UPDATE 1
The views/devise/registrations/new.html.erb looks like this: https://gist.github.com/2585197
And here is the output if errors occur: https://gist.github.com/2585208
You will need to generate views for the model. For example
rails generate devise:views users
This gives you control over the views you get from devise. Check out the documentation here
https://github.com/plataformatec/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