I have a home-landing-page that is my root url. I want users to begin the sign up process here where they input only their email, click signup, and continue the registration on a separate page.
This is the part of the form I want on the home-root-landing-page
<div>
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
This is what the full (separate) signup page looks like. I would like the email address the users entered on the home-root-landing-page to auto populate on this page (after they hit signup and are redirected to this page.)
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br />
<%= f.text_field :last_name %></div>
<div><%= f.label :profile_name %><br />
<%= f.text_field :profile_name %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
I'm using devise and I'm having trouble on how I would simply approach this. Thanks for taking a look.
In your view that you are rendering in homepage, set the form action to the signup page /user/signup.
In the signup controller, initialize the #user with the params that you've received from the homepage.
#UsersController
def signup
#user = User.new(params[:user]
end
The above action will render signup view. Now use #user in the form_for. Like:
form_for(#user)
This will auto populate the first name and email.
However, it is apparent that you're using Devise. I'm not sure it will work for its default controllers as it is initilizing the resource from empty hash (i see the following code in source)
def new
resource = build_resource({})
respond_with resource
end
So, you may have to override the registration controller.
Related
I'm just in the beginning phase of building my app - so nothing too serious here. It works fine in my local environment, but seems to be crashing on heroku.
The code is like so:
I have a User model in devise.
I have 2 sub classes under User, Authors and Readers.
The form for adding a reader looks like so:
<%= form_for(#reader, :url => user_registration_path) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<div><%= f.label :zipcode %><br />
<%= f.text_field :zipcode %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
The controller has:
def index
#reader = Reader.new
end
And the model is simply:
Class Reader < User
end
While I haven't tried to create a new user yet using the form, I'm not getting any error loading the page in my local environment. Yet on heroku, I'm getting undefined method 'model_name' for NilClass:Class.
I've raked the DB, and restarted the server with no luck.
Any ideas?
I looks like #reader is not set.
The form is not in index.html.erb.
it must be the partial _form.html.erb used by the new and edit actions. So you have to set #reader in those actions in the readers controller
I have a devise users table with a fully functioning sign in/up form.
What I was wondering was how to have that users form appear on a different table.
For example
users/sign_in works perfectly
but
I want to have that form appear on movies/index
Ive tried adding the form code to the movies/index but i get this error
undefined local variable or method `resource' for #<#<Class:0x00000102cbf0b8>:0x00000103bb6d78>
This is the sign in form
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
<% end %>
<%= render "devise/shared/links" %>
Thanks!
You can generate the views (I understand you already did that) and you can override the controllers, or, in your case, you can watch the controllers of Devise, take the code that you need, and in your view call a partial (from the Devise views).
I did something like that a few months ago, but what I did (that I don't fully suggest but I haven't find a better way) was to take the code from the Devise views, and copy the code in another view with some modifications:
<%= form_for(User.new, :as => "user", :url => session_path("user"), :remote => true) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<div><%= f.submit "Sign in" , :class=>"blue_submit_degradiant", :id =>"sign_in_user"%></div>
<% end %>
It works, but the right way is to have a #user instead of User.new
I'm trying to allow users to sign up for the site on my home/landing page.
I've duplicated the devise registration form into my landing page view-
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br />
<%= f.text_field :last_name %></div>
<div><%= f.label :profile_name %><br />
<%= f.text_field :profile_name %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
And I've also added the helper methods in my application_controller.rb so that I properly define the resource-
class ApplicationController < ActionController::Base
protect_from_forgery
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
end
However I'm still getting the error undefined local variable or method resource'for #<#:0x007fa816b4b750>`
What am I missing here to properly render a REGISTRATION form on my home landing page?
You need to declare those controller methods as helper methods to access them from the views:
helper_method :resource, :resource_name, :devise_mapping
Just add that line inside your ApplicationController after the methods are defined.
You'll notice that the link you provided actually mentions to put this code in the application helper, not the controller... You can avoid this helper_method declaration if you do it that way.
How to add value from devise registration form to another table that is not devise table
this is sample code
<div class="signin">
<h2>Register</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :full_name %><br />
<%= f.text_field :full_name %></div>
<div><%= f.submit "Register" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
</div>
full name is field of profile table and i set device on user table,
and if user enter name i want name full name store in profile table
i dont have any idea how can i do this?
you can use one of the many hooks that are provided by active record models:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#creating-an-object
ie. you could retrieve the profile object of the user, update the record and save it back in an before_save hook.
I got the answer
Put a def in devise model def name same as field name then devise allow you to pass the that field to model, in model/controller you can insert value through params in any table.
When I go to the edit_user_registration_path provided by Devise I have fields such as email and username pre-filled.
I want to create custom user profile page with the similar form for edit user information containing additional fields of dependent model. And I want fields be pre-filled.
How should I do it properly?
The default form provided by Devise:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :username %><br />
<%= f.text_field :username %></div>
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
Use nested attributes
In your devise user model, if you have other dependent models, you can add a relationship between those models, like has_many and belongs_to. The do this:
Devise user model:
accepts_nested_attributes_for :name_of_other_model
Then in your form you can use fields_for. fields_for docs
Here's also a great railscast of this here