user.rb
class User < ActiveRecord::Base
# attr_accessible :title, :body
#validate :username ,:first_name ,:last_name ,:email ,:password ,:phone ,:location ,:require => true
# validates :username,:require => true
validates :username, :presence => true
has_many :ads
#validates :phone , :presence => true
attr_accessor :password,:password_confirmation
validates_confirmation_of :password
attr_protected :hashed_password ,:salt
users_controller
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = 'User successfully created.'
redirect_to :action => 'index'
else
render :action => 'index'
end
end
def new
if session[:user_id]
flash[:notice] = "You have already registered"
redirect_to(:controller => 'main',:action => 'index')
end
@user = User.new
end
alias :register :new
registration_form
<%= form_for @user do |f| %>
<%= f.error_messages %>
<table>
<tr>
<th>
<%= f.label :first_name %>
</th>
<td>
<%= f.text_field :first_name ,:placeholder => 'Please enter your real name.' %><br/>
</td>
</tr>
<tr>
<th>
<%= f.label :last_name %>
</th>
<td>
<%= f.text_field :last_name ,:placeholder => 'Please enter your real name.' %><br/>
</td>
</tr>
<tr>
<th>
<%= f.label :username %>
</th>
<td>
<%= f.text_field :username ,:placeholder => 'Enter your username here'%>
</td>
</tr>
<tr>
<th>
<%= f.label :email%>
</th>
<td>
<%= f.text_field :email ,:placeholder => 'sample@sample.com' %><br/>
</td>
</tr>
<% if !session[:user_id] %>
<tr>
<th>
<%= f.label :password %>
</th>
<td>
<%= f.password_field :password ,:placeholder => 'EnterPassword' %><br/>
</td>
</tr>
<tr>
<th>
<%= f.label :password_confirmation,'Confirm Password' %>
</th>
<td>
<%= f.password_field :password_confirmation ,:placeholder => 'Confirm password' %><br/>
</td>
</tr>
<% end %>
<tr>
<th>
<%= f.label :phone %>
</th>
<td>
<%= f.text_field :phone ,:placeholder => '09351270000' %><br/>
</td>
</tr>
<tr>
<th>
<%= f.label :location %>
</th>
<td>
<%= f.text_field :location ,:placeholder => 'Your address' %><br/>
</td>
</tr>
<tr>
<td></td> <td> <%= f.submit !session[:user_id] ? 'Register' : 'Save changes',:class => 'button',:style => 'height:50px' %></td>
</tr>
</table>
<% end %>
doubt
when i am logging in and updating user information using the same form its working fine , but when i am creating new user , i am redirected to users/index , while i am supposed to be registered
This basically means that the @user.save fails. There can be many reasons for this, it is hard to tell exactly what this is, since you don’t give any error messages. The most probable case, I think, you are running into now is that you are trying to set a field (column of the user model) which is not accessible by the
attr_accessiblefield.Since you have commented that line out you are telling rails that there exists no fields in the user model which can be mass assigned. This is what happens when you call
User.create(params[:user])So to fix you problem now, try uncommenting your
attr_accessibleand add all the fields you need to set a user. In your case this would be:I suggest you find some information on what attr_accessible and these others do. It is handy to know how these work.