无法注册用户 - Rails只刷新我的用户新视图

时间:2015-07-09 11:54:42

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我正在尝试创建新用户,当我点击提交时,只刷新页面。提交按钮之前有效,但现在还没有。

我现在一直试图解决几个小时。有什么想法吗?

这是我的用户控制器。

$SOLR_INSTALL/server/solr/mysolr/conf

这是我的用户新视图:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id]) 
    @item = @user.items.paginate(page: params[:page])
  end

  def show_user_items
    @user = User.find(current_user)
    @item = @user.items.paginate(page: params[:page])
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted"
    redirect_to users_url
  end

  private

    def user_params
      params.require(:user).permit(:username, :email, :password, :password_confirmation, :avatar, :description)
    end

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end

end

以下是我点击提交时服务器说的内容

<center><h1>Create Seller Account</h1></center>

<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
          <%= simple_form_for @user do |f| %>
            <%= f.input :username %>
            <%= f.input :email %>
            <%= f.input :password %>
            <%= f.input :password_confirmation %>
            <%= f.button :submit, "Create My Seller Account", class: "btn btn-primary" %>
          <% end %>
        </div>
      </div>    
    </div>
  </div>
</div>

这是一个破坏的测试。

Started POST "/users" for ::1 at 2015-07-09 21:45:10 +1000
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"A8le1k27tk8fhpQ/T31V3iGFHzuXY1kspjnJBlMaWr9CxJ15S2C1QKCH7JgQ+wLlp/dz6O8CG9zX3nS60boLAQ==", "user"=>{"username"=>"Admin1234", "email"=>"admin@example.com.au", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create My Seller Account"}
   (0.2ms)  begin transaction
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE "users"."username" = 'Admin1234' LIMIT 1
  User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'admin@example.com.au' LIMIT 1
   (0.1ms)  rollback transaction
  Rendered users/new.html.erb within layouts/application (12.7ms)
  Rendered layouts/_header.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 323ms (Views: 222.5ms | ActiveRecord: 0.6ms)

以下是像22这样的测试是assert_difference&#39; User.count&#39;,1 do:

  4) Failure:
UsersSignupTest#test_valid_signup_information_with_account_activation [/Users/joseph/Documents/Safsy/Website/Safsy/Safsy/test/integration/users_signup_test.rb:22]:
"User.count" didn't change by 1.
Expected: 35
  Actual: 34

用户模型:

  test "valid signup information with account activation" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, user: { username:  "Exampleuser16",
                               email: "user16@example.com",
                               password:              "password16",
                               password_confirmation: "password16" }
    end
    assert_equal 1, ActionMailer::Base.deliveries.size
    user = assigns(:user)
    assert_not user.activated?
    # Try to log in before activation.
    log_in_as(user)
    assert_not is_logged_in?
    # Invalid activation token
    get edit_account_activation_path("invalid token")
    assert_not is_logged_in?
    # Valid token, wrong email
    get edit_account_activation_path(user.activation_token, email: 'wrong')
    assert_not is_logged_in?
    # Valid activation token
    get edit_account_activation_path(user.activation_token, email: user.email)
    assert user.reload.activated?
    follow_redirect!
    assert_template 'users/show'
    assert is_logged_in?
  end

1 个答案:

答案 0 :(得分:1)

当我查看您的模型验证时,您有

validates :description, presence: true, length: { maximum: 500 }

但在你的参数中,你没有描述。为了确定此错误的来源,您可以更改控制器内的代码。

从:

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

为:

  def create
    @user = User.new(user_params)
    if @user.save! # this will throw error
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

但我猜您缺少对用户的描述。

尝试添加

<%= f.input :description %>

以您的形式