0

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

Extracted source (around line #27):

27: <%= f.select( :client, Project.all.map {|p| [p.client]}.uniq!, :prompt => "Select a previous Client") %>

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! method in my form view. Any other ideas?

Thanks

Jazz
  • 1,090
  • 6
  • 23
  • 55
  • Have you tried outputting the params variables to see which one is nil? Put this on a line above the first `params[...].empty?` call: `logger.debug "--- params[:new_client]: #{params[:new_client]}, params[:new_exception_pm]: #{params[:new_exception_pm]}, params[:new_project_owner]: #{params[:new_project_owner]}, params[:new_role]: #{params[:new_role]}, params[:new_industry]: #{params[:new_industry]}, params[:new_business_div]: #{params[:new_business_div]}"`. Also, you should consider using `.blank?`. See [http://stackoverflow.com/a/888877/664833](http://stackoverflow.com/a/888877/664833) – user664833 Jul 18 '12 at 16:25

3 Answers3

1

You should use blank? method instead of empty?

  • I tried your suggestion and the error still reads the same, even though I changed all `empty?` to `blank?` – Jazz Jul 19 '12 at 08:12
  • I now see it was to do with the `.uniq!`. I changed it to `uniq`, which seems to work, and I left your suggestion in. Thanks – Jazz Jul 19 '12 at 08:46
0

You don't have any projects to map into your drop down.

Add a project and you should be ok.

Edward
  • 3,429
  • 2
  • 27
  • 43
0

I changed from using uniq! to uniq.

That seemed to solve the problem.

Jazz
  • 1,090
  • 6
  • 23
  • 55