Hi Gurus,
I wanted to add siteadmin user(basically a user with a certain role)
while
creating the site itself. I hope that i have done all the necessary
stuff
by going through raynb’s railscasts (
http://railscasts.com/episodes/196-nested-model-form-part-1) But could
not
see user fields being displayed in the site creation form… Below are
the
details.
I Have two model users and sites. ( user model was created using devise
then i customized it).
Below are my models:
User model
class User < ActiveRecord::Base
*belongs_to :sites ########### I have added this
:token_authenticatable, :encryptable, :confirmable, :lockable,
:timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation,
:remember_me,
:role, :site_id
end
My site model:
class Site < ActiveRecord::Base
has_many :users, :dependent => :destroy * ########### I have added
this*
accepts_nested_attributes_for :users * ########### I have added this*
end
Here is the change i have done to the new method of
site_controller.rb
**
-----------------
- *def new
@site = Site.new
1.times { @site.users.build } *############ I have added this - respond_to do |format|
format.html # new.html.erb
format.json { render json: @site }
end
end
-------------------
**
*Below is the form to which i have made changes, I have added the bold
part
in the below form ( this form is called by views/sites/new.html.erb) *
**
-------------------------
<%= form_for(@site) do |f| %>
<% if @site.errors.any? %>
<div id="error_explanation">
<h2>Could not save record due to following errors. Please correct
them and continue
- <%= msg %>
<% @site.errors.full_messages.each do |msg| %>
<% end %>
<% end %>
<%= f.text_field :sitename %>
<%= f.text_field :slogan %>
Site Admin Details
<% f.fields_for :users do |account_fields| %> <%= account_fields.label :text_field, "email" %><%= account_fields.label :text_field, "password" %>
<%= account_fields.label :text_field, "password_confirmation" %>
<% end %>
**
-
<%= f.submit %>
Thanks In Advance,
–S