undefined method `users' for nil:NilClass - ruby-on-rails-3

I am trying to create an application having domains and users, there are 3 types of users, super admin,domain admins and domain users.All the users(3 types) are in the users table and domains in domain table. At present there are domains and super admin. A super admin can login and create domains and domain users. The domains users should be related to a particular domain(i have a "domain_id" column in my 'users' table).Now when i am trying to create a user under a domain(after selecting a domain), i am getting this error "undefined method `users' for nil:NilClass".
My user controller.
def new
#domain = Domain.find(params[:id])
#user = User.new
#title = "Sign up"
end
def create
#user = #domain.users.build(params[:user])
if #user.save
flash[:success] = "Welcome to My Space!"
redirect_to #user
else
#title = "Sign up"
render 'new'
end
end
My user model is having.
belongs_to :domain
validates :domain_id, :presence => true
and domain model is having.
has_many :users
My new.html.erb form
<%= form_for #user do |f| %>
<%= f.label :first_name %>
<%= f.text_field(:first_name, :size => 20) %>
<%= f.label :last_name %>
<%= f.text_field(:last_name, :size => 20) %>
<%= f.label :email %>
<%= f.text_field(:email, :size => 20) %>
<%= f.label :password %>
<%= f.password_field(:password, :size => 20) %>
<%= f.label :password_confirmation, "Verify Password" %>
<%= f.password_field(:password_confirmation, :size => 20) %>
<%= f.submit "Create" %>
<% end %>

Assuming #domain = Domain.find(params[:id]) in the new method does what it should do:
def new
#domain = Domain.find(params[:id])
#user = #domain.users.build
#title = "Sign up"
end
def create
#user = User.new(params[:user])
if #user.save
flash[:success] = "Welcome to My Space!"
redirect_to #user
else
#title = "Sign up"
render 'new'
end
end
And your view should look like this:
<%= form_for #user do |f| %>
<%= f.hidden_field :domain_id %>
<%= f.label :first_name %>
<%= f.text_field(:first_name, :size => 20) %>
<%= f.label :last_name %>
<%= f.text_field(:last_name, :size => 20) %>
<%= f.label :email %>
<%= f.text_field(:email, :size => 20) %>
<%= f.label :password %>
<%= f.password_field(:password, :size => 20) %>
<%= f.label :password_confirmation, "Verify Password" %>
<%= f.password_field(:password_confirmation, :size => 20) %>
<%= f.submit "Create" %>
<% end %>
Because of the hidden domain_id field in your form, the user will be assigned to the correct domain.

Related

a new page, nested form using devise's registration path

I'm trying to build a nested form in a custom page using devise.
The error that comes out is: undefined method `build_profile' for nil:NilClass
<%= form_for("user", :url => user_registration_path) do |f| %>
<%= f.email_field :email, :autofocus => true, :placeholder => 'E-mail Address' %>
<%= f.password_field :password, :placeholder => 'Password' %>
<%= f.password_field :password_confirmation, :placeholder => 'Password Confirmation' %>
<% #user.build_profile %>
<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.text_field :name, :placeholder => 'Name' %>
<%= profile_form.text_field :address, :placeholder => 'Address' %>
<%= profile_form.phone_field :phone, :placeholder => 'Phone (example: 0193284647)' %>
<% end %>
<p><%= f.submit "Sign up", :class=>'btn btn-primary' %> </p>
<% end %>
Edit (Extra information):
should i add #user = #user.build_profile by creating a users_controller.rb instead?
Would it cause problems with devise?
yes you have to do
#user = #user.build_profile
in registrations_controller.rb . And,mention the customize controller in routes.rb in devise method.
You should so something like
class Registrations < Devise::RegistrationsController
def new
#user = User.new
#user = #user.build_profile
super
end
end

Getting attributes in nested model rails 3.2

I have two models user and profile.
I want to save the username and password in user and other user profile details in
profile.
Now,
The user model has:
has_one :profile
accepts_nested_attributes_for :profile
attr_accessible :email, :password,:profile_attributes
The profile model has
belongs_to :user
attr_accessible :firstname, :lastname
The user controller has
def new
#user = User.new
#profileattr = #user.build_profile
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "user created successfully!"
else
render "new"
end
end
The view new.html.erb have fields for both user and profile.
view has:
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<%= f.fields_for #profileattr do |pf|%>
<p>
<%= pf.label :firstname %><br />
<%= pf.text_field :firstname %>
</p>
<p>
<%= pf.label :lastname %><br />
<%= pf.text_field :lastname %>
</p>
<% end %>
<p class="button"><%= f.submit %></p>
<% end %>
However,when I run this web application it shows error:
Can't mass-assign protected attributes: profile
on debug I found the user has attributes as:
:email:abc#gmail.com
:password:secret
:profile
---:firstname:abc
---:lastname:xyz
so,instead of expected params[:profile_attributes] the view returning params[:profile]
leading to mass-assignment error.so what is going wrong?
Did you try
<%= f.fields_for :profile do |pf|%>
?

Couldn't find <model> without an ID, with nested attributes

I have what should be a super simple form which I'm trying to use to update multiple records at once. I'm on Rails 3. I've been through all of the railscasts, etc and am pulling my hair out at this point. I'm using Devise, and have a contacts controller. Users have_many :contacts, and accepts_nested_attributes_for :contacts. The form looks like:
<%= form_for #user, :url => '/updateusercontacts' do |i| %>
<%= i.fields_for :contacts do |f| %>
<%= f.label :first_name %>
<%= f.text_field :firstname %>
<%= f.label :last_name %>
<%= f.text_field :lastname %>
<%= f.label :phone_number %>
<%= f.text_field :phonenumber %>
<%= f.label :user_id %>
<%= f.text_field :id %>
<p>
<% end %>
The form displays properly, but then on submit I get "Can't find Contact without an ID". The controller looks like:
def updatecontacts
#contacts = Contact.find(params[:id])
#contacts.each do |contact|
contact.update_attributes(params[:id])
end
render '/home'
end
The parameters seem correct, and the id's seem to be present and correct, but I can't get the save to work! I'm sure I'm missing something obvious here.
Try to remove the fields_for and when you invoke your find, use this [:user][:contacts]
your code should look like this.
View:
<%= form_for #user, :url => '/updateusercontacts' do |i| %>
<%= i.label :first_name %>
<%= i.text_field :firstname %>
<%= i.label :last_name %>
<%= i.text_field :lastname %>
<%= i.label :phone_number %>
<%= i.text_field :phonenumber %>
<%= i.label :user_id %>
<%= i.text_field :id %>
<p>
<% end %>
Controller
def updatecontacts
#contacts = Contact.find(params[:user][:contacts])
#contacts.each do |contact|
contact.update_attributes(params[:user][:contacts])
end
render '/home'
end
Hope this works.

Rails 3: how to pull change password out to be its own separate form from devise

I'm using devise for my user registrations and I'd like to have a separate edit path for user's to change/update their passwords. I've pulled the devise registrations views out and created a separate registrations controller like described in Railscast 236
I've tried creating a new action called change_password in the registrations controller but when I try to set the route with match '/change_password', to => 'registrations#change_password' I get an AbstractController::Actions Not Found
registrations controller
class RegistrationsController < Devise::RegistrationsController
def create
super
session[:omniauth] = nil unless #user.new_record?
end
def destroy
resource.destroy
set_flash_message :notice, :destroyed
sign_out_and_redirect(self.resource)
end
def change_password
render_with_scope :edit
end
private
def build_resource(*args)
super
if session[:omniauth]
#user.apply_omniauth(session[:omniauth])
#user.valid?
end
end
routes.rb
match 'auth/:provider/callback' => 'authentications#create'
resources :authentications
devise_for :users, :controllers => {:registrations => 'registrations'}
resources :posts do
member do
get :likers
end
collection do
get :search
end
end
resources :relationships, :only => [:create, :destroy]
resources :appreciations, :only => [:create, :destroy]
root :to => "pages#home"
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/blog', :to => 'pages#blog'
resources :users do
member do
get :following, :followers, :likes
end
resources :collections
end
end
views/registrations/change_password.html.erb
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true,
:html => { :method => :put }, :html => {:multipart => true}) do |f| %>
<%= devise_error_messages! %>
<p><strong>To change password, otherwise leave blank.</strong></p>
<p><%= f.label :current_password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :current_password %></p>
<p><%= f.label :password, "New password" %> <br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
</div><br />
<p><%= f.submit "Update" %></p>
<% end %>
Try to pass match .. in the block of devise_for helper:
devise_for :users, :controllers => {:registrations => 'registrations'} do
match '/change_password', to => 'registrations#change_password'
end
I found this to be the easiest way to split the user edit form into edit & account
Originally I had
:name,
:email,
:avatar,
:location,
:website,
:bio,
:password (all in one form)
ROUTES (this provides route:
account_user GET /users/:id/account(.:format) users#account)
resources :users do
member do
get :following, :followers, :account
end
end
USERS_CONTROLLER.RB
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers, :account]
before_filter :correct_user, only: [:edit, :update, :account]
def edit
#user = User.find(params[:id])
end
def account
#title = "Account"
#user = User.find(params[:id])
end
def update
if #user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in #user
redirect_to #user
elsif #title = "Account"
render 'account'
else
render 'edit'
end
end
(split form views)
EDIT.HTML.ERB
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :avatar %><br />
<%= f.file_field :avatar %>
<%= f.label :location %>
<%= f.text_field :location %>
<%= f.label :website %>
<%= f.text_field :website %>
<%= f.label :bio %>
<%= f.text_area :bio, placeholder: "About yourself in 160 characters or less..." %>
<%= f.submit "Update Profile", class: "btn btn-medium btn-primary" %>
<% end %>
ACCOUNT.HTML.ERB
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Update Account", class: "btn btn-medium btn-primary" %>
<% end %>
SIDE NAV BAR INSIDE OF EDIT AND ACCOUNT VIEWS, SO THE USER HAS ACCESS TO EDIT AND ACCOUNT FORM
<ol class="nav nav-tabs nav-stacked">
<% #user ||= current_user %>
<li>
<a href="<%= edit_user_path(#user) %>">
Profile
<i class="icon-chevron-right"></i>
</a>
</li>
<li>
<a href="<%= account_user_path(#user) %>">
Account
<i class="icon-chevron-right"></i>
</a>
</li>
</ol>

How to create Rails3 forms for embedded Mongoid models?

How to create form for new User model with embedded Phone model?
I've found solution for creating form to add Phone for existing User but how to do that at the same time i create new User?
You have to create a nested form
<%= form_for #user, :url => users_path do |f| %>
<%= f.label :name, "Name:" %> <br />
<%= f.text_field :name %>
<%= f.fields_for :phone do |p| %>
<%= p.label :number, "Phone Number" %> <br />
<%= p.text_field :number %>
<% end %>
<% end %>