形式中的第一个参数不能包含nil或为空ror

时间:2016-01-25 17:51:17

标签: ruby-on-rails

这是我的第一个应用程序。 我有主页:home.html.erb 我在那里组建。

  <%= form_for(@lead ,:html => {:class => 'check_form'}) do |f| %>

      <%= f.text_field :phone, placeholder: 'phone' %>
    <%= f.submit "Check car status", class: "btn btn-large btn-primary" %>
  <% end %>

背景故事:客户(我称他为Lead可以输入他的电话号码并检查他现在正在维修的汽车状态。)

现在,此视图home.html.erbstatic_pages_controller

提供
 class StaticPagesController < ApplicationController
  def home
  end

  def help
  end
  def about
  end
  def contact
  end
end

我还有LeadsController

class LeadsController < ApplicationController
  #before_action :signed_in_user, only: [:index, :edit, :update]
  #before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: [:index, :edit, :update, :destroy]


  def index
        @leads = Lead.paginate(page: params[:page])
  end

  def destroy
    Lead.find(params[:id]).destroy
    flash[:success] = "record deleted."
    redirect_to leads_url
  end

  def show
    @lead = Lead.find(params[:id])
  end
  def new
    @lead = Lead.new
  end
  def create
    @lead = Lead.new(lead_params)
    if @lead.save
      #sign_in @lead
      flash[:success] = "Request successfully created!"
      redirect_to @lead
    else
      render 'new'
    end
  end

  def edit
    @lead = Lead.find(params[:id])
  end

  def update
    @lead = Lead.find(params[:id])
    if @lead.update_attributes(status: params[:status])
      flash[:success] = "Information updated"
      redirect_to leads_url
    else
      render 'edit'
    end
  end



  private

    def lead_params
      params.require(:lead).permit(:name, :email, :phone, :car_type,
      :car_year, :car_manufacturer, :car_model, :photo1, :photo2, :coords )
    end

    # Before filters

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

    def correct_user
      @lead = Lead.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

当用户输入他的电话号码以找到具有相同电话号码的数据库中的潜在客户并向用户显示修复状态时,我想要做什么。 但它会产生错误: First argument in form cannot contain nil or be empty 所以我知道form_for(@lead.. @lead是空的。 但是我该怎么做才能摆脱这个错误。?

1 个答案:

答案 0 :(得分:4)

  

表单中的第一个参数不能包含nil或为空

由于home.html.erb提供了您的观点(static_pages#home),您应该在@lead操作中初始化static_pages#home

def home
  @lead = Lead.new
end