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

ユーザーコントローラー

  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

登録用紙

            <%= 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 にリダイレクトされます

4

1 に答える 1

1

これは基本的に @user.save が失敗することを意味します。これには多くの理由が考えられます。エラー メッセージが表示されないため、これが何であるかを正確に判断するのは困難です。最も可能性の高いケースは、フィールドからアクセスできないフィールド (ユーザー モデルの列) を設定しようとしているということですattr_accessible

その行をコメントアウトしたので、ユーザーモデルに一括割り当てできるフィールドが存在しないことをレールに伝えています。電話するとこうなるUser.create(params[:user])

今すぐ問題を解決するには、コメントを外してattr_accessible、ユーザーを設定するために必要なすべてのフィールドを追加してみてください。あなたの場合、これは次のようになります。

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

attr_accessible とこれらの他の機能に関する情報を見つけることをお勧めします。これらがどのように機能するかを知っておくと便利です。

于 2012-07-29T18:09:45.420 に答える