部分形式的 ArgumentError,形式中的第一个参数不能包含 nil 或为空

ArgumentError for partial form, First argument in form cannot contain nil or be empty

我的注册表工作得很好,但是一旦我将注册从 _signup.html.erb 移到部分 _signup.html.erb,它就会给我错误 "First argument in form cannot contain nil or be empty"

我想让用户注册从主页通过一个模式,而不是被重定向到 new.html.erb

static_pages/home.html.erb

<% else %>
  <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    Sign up with email
  </button>
  <div class="modal fade">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">Sign up</h4>
        </div>
        <div class="modal-body">
          <%= render partial: 'users/signup', locals: {user: @user} %>
        </div>
      </div>
    </div>
  </div>
<% end %>

并在部分 _signup.html.erb

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user) do |f| %>
    <form id="SignupForm">
      <%= render 'shared/error_messages' %>
      <fieldset>
        <legend>Fill in your info</legend>

        <%= f.label :username %>
        <%= f.text_field :username, class: 'form-control' %>

        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control' %>

        <%= f.label :email %>
        <%= f.email_field :email, class: 'form-control' %>

        <%= f.label :password %>
        <%= f.password_field :password, class: 'form-control' %>

        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation, class: 'form-control' %>
        <fieldset>
        </fieldset>
        <legend>Languages</legend>
        <label for="Gender">Select gender</label>
        <%= f.select :sex, options_for_select([['Select Gender', ''],['Male','1'],['Female','2']], "#{@user.sex}") %>

        <label for="Country">Fill in your location</label>
        <%= f.country_select :country_code, { priority_countries: ["GB", "US"] }, class: 'required-field', required: true %>

        <%= f.submit "Create my account", class: "btn btn-primary" %>
        <% end %>
      </fieldset>
    </form> 
  </div>
</div>

部分users_controller.rb

class UsersController < ApplicationController
    respond_to :html, :js
   before_action :logged_in_user, only: [:index, :show, :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 show
    @user = User.find(params[:id])
    @page_name = "user_page"
  end

  def new
    @user = User.new
  end

  def signup
    @user = User.new
  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

routes.rb

Rails.application.routes.draw do
  root   to:          'static_pages#home'
  get    'help'    => 'static_pages#help'
  get    'about'   => 'static_pages#about'
  get    'contact' => 'static_pages#contact'
  get    'signup'  => 'users#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'
  get "users/signup" => 'users#signup' 
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
end

您无权访问部分实例变量 @user。您必须将其作为局部变量传递:

static_pages/home.html.erb

<div class="modal-body">
  <%= render partial: 'users/signup', locals: {user: @user} %>
</div>

并且在您的部分表单中,您必须使用那个 user 局部变量:

_signup.html.erb

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(user) do |f| %>
    <form id="SignupForm">
      <%= render 'shared/error_messages' %>
      <fieldset>
        <legend>Fill in your info</legend>

        <%= f.label :username %>
        <%= f.text_field :username, class: 'form-control' %>

        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control' %>

        <%= f.label :email %>
        <%= f.email_field :email, class: 'form-control' %>

        <%= f.label :password %>
        <%= f.password_field :password, class: 'form-control' %>

        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation, class: 'form-control' %>
        <fieldset>
        </fieldset>
        <legend>Languages</legend>
        <label for="Gender">Select gender</label>
        <%= f.select :sex, options_for_select([['Select Gender', ''],['Male','1'],['Female','2']], "#{user.sex}") %>

        <label for="Country">Fill in your location</label>
        <%= f.country_select :country_code, { priority_countries: ["GB", "US"] }, class: 'required-field', required: true %>

        <%= f.submit "Create my account", class: "btn btn-primary" %>
        <% end %>
      </fieldset>
    </form>
  </div>
</div>