简体   繁体   English

Ruby on Rails:表单错误

[英]Ruby on Rails: Form Error

I have a form which takes users information and stores it in the database. 我有一个接受用户信息并将其存储在数据库中的表格。 For some of the fields the user can put new information in a field, or select previous info from the database. 对于某些字段,用户可以在字段中放入新信息,或从数据库中选择以前的信息。 It was working earlier, however I recently cleaned the database and now I get this error when I try to access the page. 它之前工作正常,但是最近我清理了数据库,现在尝试访问该页面时出现此错误。

undefined method `empty?' 未定义的方法“空”? for nil:NilClass 对于nil:NilClass

Extracted source (around line #27): 提取的源(第27行附近):

27: <%= f.select( :client, Project.all.map {|p| [p.client]}.uniq!, :prompt => "Select a previous Client") %> 27:<%= f.select(:client,Project.all.map {| p | [p.client]}。uniq !,:prompt =>“选择以前的客户端”)%>

Form view example: 表单视图示例:

<div class="field">
<%= label_tag :new_client, "Client" %><br/>
<%= text_field_tag :new_client %>
Or
<%= f.select( :client, Project.all.map {|p| [p.client]}.uniq!, :prompt => "Select a previous Client") %>
</div>

Project Controller: 项目负责人:

def create
@project = Project.new(params[:project])


@project.client = params[:new_client] unless params[:new_client].empty?
@project.exception_pm = params[:new_exception_pm] unless params[:new_exception_pm].empty?
@project.project_owner = params[:new_project_owner] unless params[:new_project_owner].empty?
@project.role = params[:new_role] unless params[:new_role].empty?
@project.industry = params[:new_industry] unless params[:new_industry].empty?
@project.business_div = params[:new_business_div] unless params[:new_business_div].empty?

respond_to do |format|
  if @project.save
    format.html { redirect_to @project, notice: 'Project was successfully created.' }
    format.json { render json: @project, status: :created, location: @project }
  else
    format.html { render action: "new" }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end
end

UPDATE: 更新:

I think the error is coming from the .uniq! 我认为错误来自.uniq! method in my form view. 我的表单视图中的方法。 Any other ideas? 还有其他想法吗?

Thanks 谢谢

You should use blank? 您应该使用blank? method instead of empty? 方法而不是empty?

You don't have any projects to map into your drop down. 您没有任何要映射到下拉菜单中的项目。

Add a project and you should be ok. 添加一个项目,您应该可以。

I changed from using uniq! 我从使用uniq!改变了uniq! to uniq . uniq

That seemed to solve the problem. 那似乎解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM