rails创建一个新用户

时间:2012-07-29 17:42:24

标签: ruby-on-rails

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 %>

疑问

当我登录并使用相同的表单更新用户信息时工作正常,但是当我创建新用户时,我被重定向到users / index,而我应该注册

1 个答案:

答案 0 :(得分:1)

这基本上意味着@ user.save失败了。这可能有很多原因,很难准确说明这是什么,因为您没有提供任何错误消息。我认为,您现在遇到的最可能的情况是您正在尝试设置attr_accessible字段无法访问的字段(用户模型的列)。

由于您已注释掉该行,您告诉我们在用户模型中不存在可以批量分配的字段。当您致电User.create(params[:user])

时会发生这种情况

因此,要立即解决问题,请尝试取消注释attr_accessible并添加设置用户所需的所有字段。在你的情况下,这将是:

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

我建议你找一些有关attr_accessible和其他人的信息。知道这些是如何工作的,这很方便。

相关问题