Remove :html reponse to Devise Passwords Controller - ruby-on-rails

I would like to prevent users from accessing the html Devise page users/passwords/new, as I have made all my devise pages respond to :js.
It's all working for my Registrations and Sessions, but I can't seem to make it work with the Passwords.
I have set the routes to overwrite the controller:
devise_for :users, controller: { passwords: 'users/passwords' }
I have reset the navigational formats in that controller:
class Users::PasswordsController < Devise::PasswordsController
clear_respond_to
respond_to :js
****** rest of devise code
end
And I have set up devise.rb
config.navigational_formats = ['*/*', :html, :js] (even if I remove html here it still does not work)
I've even deleted new.html.erb from the views/devise/passwords folder.
But I still have access to the url users/passwords/new
Of course if I use a link with remote: true it does render my new.js.erb just like it should.
Also the fact that it only reponds to :js prevents Devise from working if I enter a mail in the field I get a ActionController::UnknownFormat error.
Anyone has ever done this before?

Well I think I've found a solution (?)
I just add this block in my confirmations_controller
def new
super
respond_to do |format|
format.js
end
end
So that users can still access the modal if the form is remote: true, but can't access the html version, because it raises an ActionController::UnknownFormat
Could this cause any further problems?

Related

How do you respond using a one-line piece of javascript in a Ruby on Rails controller action?

I haven't been able to find any useful resources online on how to do this. Basically what I'm trying to do is run a simple jQuery $('#test-div').show(); when my def show_div controller action is complete.
I've tried the following and it doesn't work. It actually renders HTML which is confusing to me. when I explicitly state that the method respond with js.
users_controller.rb
def show_div
#user = User.first
respond_to do |format|
format.js {}
end
# also tried
# render :js => "$('#test-div').show();"
end
show_div.js.erb
$('#test-div').show();
render :text should do what you are asking for -- just return raw text (which in your case happens to be JavaScript code) without doing anything to it.

Ruby on rails User registration using rest API call and Devise

Can anyone guide me how to register a user from mobile device (rest API) in ruby on rails. I'm using Devise with Rails 3.0.
it is giving me this following error
NameError in Devise::CustomRegistrationsController#create
I've override the functionality of devise registration controller with the following.
def create
respond_to do |format|
format.html {
super
}
format.json {
build_resource
if resource.save
render :status => 200, :json => resource
else
render :json => resource.errors, :status => :unprocessable_entity
end
}
end
end
this solved the problem and I've added
skip_before_filter :verify_authenticity_token, :only => :create
to avoid authenticity check.
Wouldn't it be easier to make views for mobile than make an app on android/iOS? If you need API, then go with POST requests at /users/sign_up (and similar), for example,
browse localhost:3000/users/sign_up and change form's action parameter to action="/users.json", then click submit and you will receive the API's response, for me (on vanilla setup):
{"email":["has already been taken"],"password":["doesn't match confirmation","is too short (minimum is 6 characters)"]}
This way you can debug API (which follows standard conventions) with your browser. Notice that only :format parameter changes on rails routes (you can choose .json or .xml for APIs response)
POST info sent by my browser:
"utf8=✓&authenticity_token=n5vXMnlzrXefnKQEV4SmVM8cFdHDCUxMYWEBMHp9fDw%3D&user[email]=asd%40fasd.org&user[password]=321&user[password_confirmation]=1233&commit=Sign+up"

Missing template with Devise custom registration controller

When using recaptcha for Devise I have to make a new custom registrations controller and my issue is I get a missing template error when their is an error for the email, password or password confirmation because its hitting a route that doesn't even exist.
Template is missing
Missing template registrations/new
The recaptcha works on its own error and renders back to the same page but not for the others.
class RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha
super
else
flash.delete :recaptcha_error
build_resource
clean_up_passwords(resource)
flash[:alert] = "There was an error with the recaptcha code below."
render :template => '/devise/registrations/new'
end
end
end
devise_for :users, :controllers => { :registrations => "registrations" }
It should be hitting the same page the recaptcha does on errors ('/devise/registrations/new')How do I correct this issue?
Thanks.
Try moving the templates from /views/devise/registrations to just /views/registrations. (And changing the reference in your code from /devise/registrations/new to just /registrations/new.)
Add following line to your config/application.rb file
config.paths['app/views'] << 'app/views/devise'

Remove Devise Flash Notices for Sign out

As the name says, I am using devise for user auth in a rails 3 app
Upon user log out, there is a flash notice, "User Successfully signed out" that I don't want to appear. However, I can't figure out how to remove the notice.
Is there a way to get around just making it blank? I would like to completely remove the notice so that, ideally, there's not even an html div for notice
If you explicitly put in a blank string for this in your locale file, then Devise "won't bother" to render the message at all (e.g. there won't even be an empty HTML div).
#en.yml
devise:
sessions:
signed_in: 'Signed in successfully.'
signed_out: ''
My routes.rb
devise_for :users, :controllers => {
sessions: 'user/sessions'
}
My controller "account/sessions_controller.rb"
class User::SessionsController < Devise::SessionsController
def destroy
super
flash.delete(:notice)
end
end

How to set a home page for logged in users and non logged in users?

How would I set in my rails app so that if a first time user comes to my site www.example.com they see a page that they can sign in but if an already logged in goes to www.example.com it now displays their own posts but still at the same www.example.com url.
Would I do something like render template based if they are logged in or is there some other way to do this?
You can set the users#home to be the root URL:
UsersController:
def home
if logged_in?
#blogs = current_user.blogs
render :action => 'logged_in'
else
render :action => 'non_logged_in'
end
end
Have 2 files in the app/views/users folder:
logged_in.html.erb & non_logged_in.html.erb
A great article was writen by Steve Richert. He is using advanced constraint when defining the route, see here
It depends on how you are making your log in logic
Usually you should have two actions, one for home/login form and another for user logged in home. You can make a before_filter on your application controller, so you can test if the user is logged in or not and then redirect him to home (logged out) if not.
If you are not using your own code or another solution I would like to recommend you this gem called devise, it implements a lot of login logic itself and is easy to change too.
EDIT: I think this solutions is better than the others that were presented and I didn't put the code (although it is quite the same code of the before_filter link), so here it is:
class ApplicationController < ActionController::Base
before_filter :require_login
private
def require_login
unless logged_in?
flash[:error] = "You must be logged in to access this section"
render :controller => 'home', :action => 'not_logged_in'
else
# whatever code you need to load from user
render :controller => 'home', :action => 'logged_in'
end
end
end
This solutions works perfectly because it tests if the user is logged in in every controller/action he tries to access.

Resources