2012-07-29 79 views
0

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 => '[email protected]' %><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 %> 

疑問

,當我登錄並使用更新用戶信息相同的形式其工作正常,b當我創建新用戶時,我重定向到用戶/索引,而我應該註冊軌道創建一個新用戶

回答

1

這基本上意味着@ user.save失敗。可能有很多原因,因爲你沒有給出任何錯誤信息,所以很難確切地說明這是什麼。我認爲,現在最容易遇到的情況是,您正嘗試設置attr_accessible字段無法訪問的字段(用戶模型的列)。

既然你已經評論說,你告訴軌道,用戶模型中不存在可以批量分配的字段。當您撥打電話User.create(params[:user])

因此,要解決您現在的問題,請嘗試取消註釋attr_accessible並添加設置用戶所需的所有字段。在你的情況下,這將是:

attr_accessible :first_name, :last_name, :username, :email, :password, :phone, :location 

我建議你找到一些關於什麼attr_accessible和這些其他人做的信息。知道這些如何工作很方便。

+0

我設置attr_protected爲:hashed_pa​​ssword和salt,是否使所有其他訪問? – Abhimanyu 2012-07-30 05:28:28