I using devise to manage my authentication, and want to add a new page where users can upload a picture , so when they sign in, they will be redirected to their profile where they can upload a picture, how do I go about that? Should I create a new controller for this matter or override devise's controllers and how will my route for this matter look like? help is much appreciated.
Thanks
I am assuming that you have a User model and image as its attachment field
You need add this to your Application Controller:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :configure_permitted_parameters, if: :devise_controller?
def after_sign_in_path_for(resource)
upload_path
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user) << :image
end
end
You need to create an upload page and its corresponding route, where the user uploads the image.
Related
I'm trying to use Devise gem in my project. That worked, but I still have a problem:
I setup devise in my application and generated the views, but I added an extra field in the database (username). So, the thing is that I need this username, but it's not being saved in the database when I create a new user :/ The problem seems to be in the controller, but I don't know how to access it.
Do devise hide its controllers? Can I access this controller and simply add the field I want in the params, for example? Do I really need to code a new controller that will override the default one? What should I do? and how?
See doc: https://github.com/plataformatec/devise
You have to tell the devise to save extra parameters in the DB.
Your application_controller.rb file look like:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
I'm trying to display a simple welcome message to the user when they log in such as, "Welcome, Anthony!".
I created first_name and last_name attributes on the sign up and migrated them to the devise Users table and permitted the new attributes through the ApplicationController with this code,
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :first_name, :last_name])
end
end
This works perfectly fine, however, I want to display the first_name in my navbar in the application.html.erb file, which acts as a welcome message to the user once theyve signed in.
How do I display info from the Devise Users table?
Thanks,
Ant.
(If I need to add anything else then just let me know!)
Once you logged in you should be able to access a current_user object, from which you can get first_name and display in the welcome message in the application layout.
I have used the 'Devise' gem provided by Rails to for user SignUp/Log In. By default it provides 'email', 'password' and 'password confirmation' fields. I wish to add more fields like 'Nickname', 'City', 'Company' etc and then also create a user profile page (Dashboard) which simply displays this information.
Can you please tell me in detail(at least mention the steps) as to how I can achieve this?
As Iceman stated, you can find how to add parameters at https://github.com/plataformatec/devise. You will have to generate the views and edit the forms to include your parameters. Then the "lazy way" is too add the following code in your Application controller.
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
To create a profile page, you can just add an action to one of your existing controllers or one of the devise controllers.
I'm building a rails app, and so far I've set up a pretty basic user registration/login system, mainly by following this railcast I found thanks to stack overflow. I've left everything the same as the railcast, only used strong parameters instead of attr_accessible and added some additional fields (username, bio, img url) to the table.
Now I want my app to redirect users onto my login page if they're not logged in, no matter what page they try to access, and if they are, then redirect to the normal path. My loging page is currently my root_path. Do I need to do this in all the controllers separately or can I just write this into my appController? How would I go about writing the controller? I was thinking something like this:
if session[:user_id] == nil
redirect_to login_path
else
redirect_to current_controller_path
end
Now how do I check if user is logged in, and how do I redirect to current controller path (for instance articles_index_path?
I am new to ruby on rails, and still trying to wrap my head around models, views and controllers, so please assume I know nothing when writing up explanations :) Thanks for the help
Oh I'm using Rails 4 with ruby 2.2.1
You need to add a before_filter in your ApplicationController to check user's authentication.
class ApplicationController < ActionController::Base
...
before_filter :authenticate_user!
...
private
def authenticate_user!
redirect_to login_path unless session[:user_id]
end
end
Now it will make sure that user should be logged in for accessing any action of any controller, including signup, signin and other actions which should be accessible to non-logged in users too.
You need to make sure that you skip above before_filter where you don't want user to be logged in such as signup, signin, about us, contact us etc actions like this.
For Example:
class SessionsController < ApplicationController
skip_before_filter :authenticate_user!, :except => :destroy
...
def new
...
end
def create
...
end
...
end
You can read more about skip_before_filter on APIDock
I have used Devise for authentication.
Went ahead and updated my Users database to include a bio column. Added this and ran the migration so I can see it is there.
Now I want Users to be able to add a bio once they are logged in. Did some research and I see attr_accessible is no more in rails 4 and I should use strong parameters. Also checked out the Devise documentation but couldn't quite find what I am after.
I see they are added to the controller but as I have used Devise I don't have access to the UsersController
How can I add the ability for Users to update the bio field so it saves in the database?
You should add a before_filter in your ApplicationController to do that. Devise docs contains a section explaining this. I took the code below from there:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end
In the example, the attribute :username is allowed to be parsed in the sign_up page.