Ruby on rails 表单中的第一个参数不能包含nil或为空ror
这是我的第一个ror应用程序。 我有主页:Ruby on rails 表单中的第一个参数不能包含nil或为空ror,ruby-on-rails,Ruby On Rails,这是我的第一个ror应用程序。 我有主页: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-
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 %>
我还有LeadsController
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
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
class LeadsController
当用户输入他的电话号码以在数据库中查找具有相同电话号码的潜在客户并向用户显示维修状态时,我想做的事情。
但它会产生一个错误:
表单中的第一个参数不能包含nil或为空
所以我得到了(@lead..这里@lead是空的)。
但是我该怎么做才能消除这个错误呢
表单中的第一个参数不能包含nil或为空
由于您的视图(home.html.erb
)由static#u pages#home
提供,您应该在static#u pages#home
操作中初始化@lead
def home
@lead = Lead.new
end
表单中的第一个参数不能包含nil或为空
由于您的视图(home.html.erb
)由static#u pages#home
提供,您应该在static#u pages#home
操作中初始化@lead
def home
@lead = Lead.new
end
@lead
在哪里初始化?@Makoto我不确定它在哪里初始化。我为我的leadController添加了代码。我想现在我没有初始化它。@lead
在哪里初始化?@Makoto我不确定它在哪里初始化。我为我的leadController添加了代码。我想现在我没有初始化它。第二个问题:我知道怎么初始化它o通过电话查找我的潜在客户,如“lead.find(params[:phone])”,但我可以将代码放在哪里?我应该在评论中问这个问题还是添加另一个问题?@user2950593您应该将其放在潜在客户中#创建操作:)@user2950593是的…)如果我已经有这样的代码@lead=lead.new(lead_参数),如果@lead.save#在@lead flash中签名[:success]=“成功创建了请求!”重定向到@lead else render‘new’end
。。那个么,当用户单击register时,当他只是想通过他的电话号码找到他已经注册的汽车时,我将如何理解?将检查:phone field
中的nill是个好主意吗?@user2950593无法理解您所说的内容。试着发布另一个问题并解释清楚。第二个问题:我知道如何通过电话找到我的潜在客户,比如“lead.find(params[:phone])”,但我可以把我的代码放在哪里?我应该在评论中问这个问题还是添加另一个问题?@user2950593你应该把它放在潜在客户中创建操作:)@user2950593是的…)如果我已经有这样的代码@lead=lead.new(lead_参数),如果@lead.save#在@lead flash中签名[:success]=“成功创建了请求!”重定向到@lead else render‘new’end
。。那个么,当用户单击register时,当他只是想通过他的电话号码找到他已经注册的汽车时,我将如何理解?将检查:phone field
中的nill是个好主意吗?@user2950593无法理解您所说的内容。试着提出另一个问题并解释清楚。