Sessions中的ArgumentError #new

时间:2016-07-13 22:10:31

标签: ruby-on-rails forms

我试图将我的登录表单和注册表单放在会话的新操作视图中。我得到的错误说:“形式上的第一个参数不能包含nil或为空”'在视图的第9行。

路线:

Rails.application.routes.draw do
 resources :users
 get '/users/new' => 'users#new'
 post '/users/new' => 'users#create'

 get '/login' => 'sessions#new'
 post '/login' => 'sessions#create'
 get '/logout' => 'sessions#destroy'

 root 'users#index'

控制器:

class SessionsController < ApplicationController
def new 

end

def create
    @user = User.find_by_email(params[:session][:email])
    if @user && @user.authenticate(params[:session][:password])
        session[:user_id] = @user.id 
        redirect_to '/'
    else
        render '/login'
    end
end

def destroy 
    session[:user_id] = nil
    redirect_to '/login'
end



class UsersController < ApplicationController
before_action :require_user, only: [:index]

def index
end

def new
    @new__user = User.new
end

def create
    @new_user = User.new(user_params)
    if @new_user.save
        session[:user_id] = @new_user.id 
        redirect_to '/'
    else
        render '/login'
    end     
end

def edit
    @user = User.find(current_user)
end

def update
    @user = User.find(current_user)
    if @user.update(user_params)
        redirect_to '/home'
    else
        render '/edit'
    end
end

private
def user_params
    params.require(:user).permit(:name, :email, :password)
end

查看:

<div class="forms">

    <div class="login">

        <h3>Login</h3>

        <%= form_for(:session, url: login_path) do |x| %>
            <%= x.email_field :email, :placeholder => " Email", :style => "height:50px; width:350px; border: solid #ff4d4d; background-color:#eff5f5; margin-top:20px; padding-left:5px;" %>
            <%= x.password_field :password, :placeholder => " Password", :style => "height:50px; width:350px; border: solid #ff4d4d; background-color:#eff5f5; margin-top: 35px; padding-left:5px;" %>
            <%= x.submit "Login" %>
        <% end %>

    </div>

    <div class="divide"></div>

    <div class="signup">

        <h3>Signup</h3>

           <%= form_for(@new_user) do |x| %>
            <%= x.text_field :name, :placeholder => " Name", :style => "height:50px; width:350px; margin-top:20px; border: solid #ff4d4d; background-color:#eff5f5; padding-left:5px;" %>
            <%= x.email_field :email, :placeholder => " Email", :style => "height:50px; width:350px; margin-top:35px; border: solid #ff4d4d; background-color:#eff5f5; padding-left:5px;" %>
            <%= x.password_field :password, :placeholder => " Password", :style => "height:50px; width:350px; margin-top:35px; border: solid #ff4d4d; background-color:#eff5f5; padding-left:5px;" %>
            <%= x.submit "Submit" %>
        <% end %>

    </div>

</div>

1 个答案:

答案 0 :(得分:0)

user controller中查看此内容似乎有两个下划线。

   def new
        @new__user = User.new
    end

将其更改为@new_user