I have to Devise models, User and Vendor. I generated the views for Vendor and customized the signup form to suit my needs. I needed to tweak the registrations controller for Vendor, like so:
controllers/vendors/registrations_controller.rb
class Vendors::RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication
def create
super
end
protected
def sign_up(resource_name, resource)
true
end
def new
super
end
end
My signup view for Vendor looks like so:
views/vendors/registrations/new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:multiparet => :true} ) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :store_name %>
<%= f.text_field :store_name %></div>
<div><%= f.label :contact_name %>
<%= f.text_field :contact_name%></div>
<div><%= f.label :contact_phone %>
<%= f.text_field :contact_phone %></div>
<div><%= f.label :address %>
<%= f.text_field :address %></div>
<div><%= f.label :image %>
<%= f.file_field :image %></div>
<div><%= f.label :email %>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "vendors/shared/links" %>
When I attempt to render the page, I get this error:
undefined method `errors' for nil:NilClass
How can I render devise error messages in this situation? Thanks in advance!
I would write this as a comment, but it will be easier to read as an answer:
Perhaps you could try replacing devise_error_messages! with a standard Rails error display:
<% if resource.errors.any? %>
<div class="error">
<strong><%= pluralize(resource.errors.count, "Error") %></strong>
<ul>
<% resource.errors.each do |attr,msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Related
I have column inviter in my User model. I want get params from the link and save it in cookies. For example, from this link:
domain.com/?ref=5
I want save to cookies number ?ref=**5**
And when a user goes to registration, the user will not need to write in the number of the referral in the form, because it is already in cookies.
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name,'Your name'%>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email,'Email' %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :invite, 'Number who invited you' %>
<%= f.text_field :invite, class: 'form-control' %>
<%= f.label :password, 'password' %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation ,'password confirmation' %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<br>
<br>
<div class="textc">
<%= f.submit yield(:button_text), class: "formbut" %>
</div>
<% end %>
The invite number will use ?ref=**5** and register the new user.
Add something like this to your application_controller.rb:
before_action :store_ref
private
def store_ref
cookies[:ref] = params[:ref] if params[:ref].present?
end
And something like this to your user create action:
def create
#user = User.new(user_params)
#user.ref = cookies[:ref]
if #user.save
# ...
A better approach would be to use a hidden input and populate it with the value from params[:ref].
class UsersController < ApplicationController
def new
#user = User.new do |u|
if params[:ref].present?
u.inviter = params[:ref]
end
end
end
# ...
end
<%= form_for(#user) do |f| %>
# ...
<% if f.object.inviter.present? %>
<%= f.hidden_field :inviter %>
<% end %>
<% end %>
It requires less workarounds.
I've added the resource helper methods to my settings controller so that I can update the current_user from within the controller. I've taken the form_for from registrations/edit and copied it into my settings controller view. I'm able to successfully update the user, but the existing values aren't showing up in the form as they would if I open users/registrations/edit. It's the same code displaying each form. What am I missing?
settings_controller.rb
helper_method :resource_name, :resource, :devise_mapping, :resource_class
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
def resource_class
User
end
devise/registrations/edit.html.erb and general.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.
form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect
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 want to create a new user from my form in my new.html.erb file:
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password%>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br/>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Sign Up"%>
</div>
<% end %>
In my users_controller.rb I have this
class UsersController < ApplicationController
def new
#title = "Sign Up"
#user = Users.new
end
And I have this in my routes.rb
resources :users
I keep getting an error: http://d.pr/wJm8 when I go to users/new and I can't figure out what's wrong...help please?
I think there are some plurality inconsistencies. Your model should be named User not Users.