Updated
I want to add more details to this. I have a user model and this user model
has_many :trial_subscriptions
attr_accessible :trial_subscriptions_attributes
accepts_nested_attributes_for :trial_subscriptions, :allow_destroy => true
I am building a custom form so when a user enters a trial_email, the form will create a new user with an associated trial_subscription
The trial_subscription.rb model inherits from manual_subscription model and manual_subscription inherits from subscription model.
The subscription model
belongs_to :user
I need to build the associations from the has_many side. Now I am having trouble figuring out where to post the form (getting the #user to be editted) when devise is involve. I need to know where the create method is when the user gets created and hence my question below.
This will be a separate page from normal sign up in a /sales url so I can not add nested fields to the signup form.
The rails application that I am working on has devise installed on the user model.
In the routes
I have
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users
the sign up form is in views/devise/registrations/new.html.erb
I have a users_controller.rb with a new and create method
in the users_controller.rb there is this before_filter
before_filter :authenticate_admin!
We have active admin installed. I am still quite new to the code. Since this filter exist I am sure the user is not getting created in this controller.
when I go to
/users/sign_up - there is the sign up page
if I do
/users/new - I am redirected to /users/sign_up
so I am guessing that the user is getting created in the registrations controller but the new method is
this
def new
session[:qbid] = params[:qbid]
session[:trial] = params[:trial]
session[:sublength] = params[:sublength]
if session[:trial] = true
#trial_flow = true
end
super
end
there is no User.new object getting passed to the view to get edited? I am guessing that devise doesn't need it.
I am getting confused on where the user is getting created because my task is to create a custom form which creates a new user.
I don't know where to post the form to? the registrations_controller?
The user has a has_many subscriptions association and I need to post a form that both create the user and its subscriptions.
You need to customize devise's registrations controller. See it's doc
Though the doc shows only how to customize the session controller (login), the steps are similar, you just need name your customized controller differently.
If you are wondering, you can see here how devises' controllers look.
When you are customizing those, you are going to use inheritance. That is another topic you should read about.
Related
Here's the scenario to illustrate my question. I have 2 models:
# models/post.rb
belongs_to :user
validates_presence_of :comment
And we have a devise model called Users
# models/user.rb
has_many :posts
What I would like to achieve:
Person comes to the website, is able to create a Post, after creating the Post, they are prompted to create an account. After creating the account, the Post that they just created would be associated to the User they just created.
Usually i'd make use of routes to hold the params[:id] which can be accessed in the controller method. For example the URL may look something like this:
www.foo.com/foo/new/1
And then I can do this:
# foo_controller.rb
def new
#foo = Foo.new
#parent = Parent.find(params[:id])
end
And in the view I can simply access #parent and use a hidden field to fill the parent ID.
But when routing through so many different pages (such as creating a Devise User), how do I hold onto the parent/child ID such that I can still create that association?
Using an hidden field or the route to store the id, with no authorization in the process, would not be secure. What if I just use the browser inspector and change the value of the id ? Your cool post would be mine.
What you could do is, for instance, add a field called guest_id to the Post, in which the value is unique (like SecureRandom.uuid), and also store that value in the session.
Thus, after the user is created, you could do something like that
if (post = Post.find_by(guest_id: session[:guest_id])).present?
post.update(user_id: current_user.id)
end
Requirement: I need to create an application where user can logged in and can change the background color of that page, can change the font of the page and it should persist with session. so I am trying to create form which accept those attributes from user and can save in database. I am using devise for authentication.
How can I create form for user who is successfully logged into application(authentication is done by devise gem, so user table is already existing) and upon submission of form those attributes should get updated in user table. Any suggestion will be appreciated.
Q1 .What should be the name of controller and view for this form ?
Q2. How the routes can be define.
Q3. Whether controller should have update action to update user table with the extra attributes present in the form.
Thanks. Please comment below if I missed some information needed to provide. You can suggest me if you think this can be achieve in easier way also.
Whatever you want. Sounds li?ke you are just updated user attributes, so i would just use the User#update action
resources :users, only: [:update, :edit] #and any other user routes
? see 1
Also you will want to make sure that people can only edit their own account. In a before action you will want to add.
redirect_to root_path unless current_user == user_you_are_editing
What I would do:
Create a Model called UserPreferences that belongs_to :user, give this Model the attributes 'background_color', etc...
Add statement to user has_one :user_preferences
Create a form for the prefs like any Rails Model, that can only be accessed by current_user.
Use current_user.user_preferences to refer to these values, you can enhance this by placing alias methods in User, for example:
class User < ActiveRecord::Base
def background_color
self.user_preferences.background_color
end
end
I am using Devise gem to create models of Clinics and Doctors who can register and sign up. In order to have separate views for each of them, I activated the following line in config/initializers/devise.rb
config.scoped_views = true
After generating separate views, I was able to modify the sign_up form for Doctors in app/views/doctors/registration/new.html.erb, and the modified form was being displayed
After this I needed to allow a signed in Doctor to create another Doctor, so I overwrote the default Devise RegistrationController as follows
class RegistrationController < Devise::RegistrationsController
skip_before_filter :require_no_authentication
end
In routes.rb, I instructed it to use this controller
devise_for :doctors, :controllers => {:registrations => "registration"}
Everything is working OK, a signed in doctor can access the registration form for creating a new doctor, except that the Form is the Devise default one for registrations, not the one I had modified in app/views/doctors/registration/new.html.erb
I cannot find a way to instruct my Inherited Controller to use the modified form instead of the default one.
Thanks in advance
I have a page that takes a user through a short sign up tutorial when they create their account in order to create their first resource. In my app, :hotel belongs to :user, and :user has_one hotel. For the tutorial page, in my controller, I have:
#hotel = current_user.build_hotel
Which works, except that it a user somehow finds him back on the tutorial page that command disassociates their previously created hotel. In other words, the second time the user accesses the page with:
#hotel = current_user.build_hotel
The user_id field in the hotel they created the first time becomes nil. Obviously that is a serious problem. I can do a before_filter on that page, but I'm not very happy about having a way for the user to screw everything up simply by visiting a page. How should I properly use the build command for a has_one relationship?
You can test for the existence of a hotel before building it:
#hotel = current_user.hotel || current_user.build_hotel
Hello I have the following models.
The User Model:
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
has_one :user_informations
has_secure_password
end
The UserInformation Model:
class UserInformation < ActiveRecord::Base
belongs_to :user
attr_accessible :address, :address2, :business, :descripcion, :identification_number, :mobile_cell, :name, :phone_number
end
Now I need to create the view and the controller to create and update the user information, and I have many questions:
1) how can I generate the controller:
rails g controller UserInformations
o
rails g controller UserInformation
2) how my new, create and update action know the user ID.
3) how can I set the routes for this user information
Thanks. Maybe these are a basic question, but I'm new in rails and I don't know how to do all of this.
Thanks again for your help.
1) You have to use pluralize for controller, so rails g controller UserInformations will work.
2 + 3) You can set up Restful routes:
resources :users do
member do
get 'user_information'
end
end
With above routes you will have path users/:id/user_information, so you can know your user ID through params[:id], ex, in your create or update action you can use:
user = User.find(params[:id])
to find which user is shown informaton.
You'll need a user_id column in your user_information table and model.
1) rails g UserController
2) you can include the user_id as param, so for the new action it will be
def new
#user = User.find(param[:user_id])
#user_information = #user.user_information.new
end
the create and update actions would get the user id from the form params but you'll need to think about who is going to be using these actions and if you want to allow all users to update the information of other users. If not, you should have the user id as a hidden param and use a gem like cancan (https://github.com/ryanb/cancan) to restrict access
alternatively you can set them up as nested resources (http://railscasts.com/episodes/139-nested-resources)
3) for a simple resources you can add this to your routes.rb file
resource :user_information
or for nested you can do
resource :users do
member do
resource :user_information
end
end
First of all
in User Model, there should be
has_one :user_information
as there association name should be singular with has_one.
you can create controller by giving the command
rails g controller UserInformation
and it depends on you what name you want to give to you controller.
In new action you will have to find user by its id. You can store user id in session
after login. Or if you are saving user first time then you will have to pass user id.
In new action do
user = User.find(session[:id])
user.user_information.create(params[:user_information])
I think you need to study all this first. Go through some examples. Then try. It is too quick to ask here.