views:

390

answers:

1
undefined method `to_sym' for nil:NilClass

I have this error only in my edit page of my nifty_scaffold.

This is _form.html.erb

<% form_for @progress do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :date %><br />
    <%= f.date_select :date %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :weight %><br />
    <%= f.text_field :weight %>
  </p>
  <p>
    <%= f.label :fatpercentage %><br />
    <%= f.text_field :fatpercentage %>
  </p>
  <p>
    <%= f.label :height %><br />
    <%= f.text_field :height %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

This is edit.html.erb

 <% title "Edit Progress" %>
 <%= render :partial => 'form' %>

And this is my controller:

class ProgressesController < ApplicationController
  def new
    @progress = Progress.new
  end

  def create
    @progress = Progress.new(params[:progress])
    if @progress.save
      flash[:notice] = "Successfully created progress."
      redirect_to progresses_url
    else
      render :action => 'new'
    end
  end

  def edit
    @progress = Progress.find(params[:id])
  end

  def update
    @progress = Progress.find(params[:id])
    if @progress.update_attributes(params[:progress])
      flash[:notice] = "Successfully updated progress."
      redirect_to progresses_url
    else
      render :action => 'edit'
    end
  end

  def index
    @progresses = Progress.all
  end
end

What could be wrong? I can't seem to find my error :-S. It seems that it: - fetches the data correctly - can't insert the db-values into the "edit view" fields.

I'm using :float, :string and :date as data types in the scaffold.

Just for the completed post, this is my error: NoMethodError in Progresses#edit

Showing app/views/progresses/edit.html.erb where line #3 raised:

undefined method `to_sym' for nil:NilClass
Extracted source (around line #3):

1: <% title "Edit Progress" %>
2: 
3: <% form_for @progress do |f| %>
4:   <%= f.error_messages %>
5:   <p>
6:     <%= f.label :date %><br />

At line 6 the log of the code ends...

Edit: It seems to be an error in my routes.rb. This is currently commented:

 map.edit_progress "edit_progress", :controller => "progresses", :action => "edit"

when i uncomment it, it gives an error also in my index view.

For some reason, this is called: 'http://127.0.0.1:3001/progresses/1/edit', shouldn't it be: 'http://127.0.0.1:3001/progresses/edit/1' ? Even though it seem's that the "edit view" is called... Perhaps this is the reason why the values aren't filled in, in my view...

What could be my solution?

+1  A: 

I will suggest two step debugging here:

  1. Remove all your code from the edit view and a add some plain text in it, then access your page in the browser and see if you get any error or new error or no error

  2. If you get any new error then it might help you in solving the issue or in your controller edit action raise the @progress to see whether it is being set

    def edit
      @progress = Progress.find(params[:id])
      raise @progress.inspect
    end
    

These two steps might help you in resolving the issue.

nas
I don't receive an error when i replace the "edit view" by plain text.and with raise @progress.inspect i receive all the values (namely: #<Progress id: 1, date: "2010-02-17", description: "Eerste test", weight: 78.0, fatpercentage: 24.0, height: 178, created_at: "2010-02-17 03:38:56", updated_at: "2010-02-17 03:38:56">)What's next?
NicoJuicy
Do you have a resource set for progress in your `config/routes.rb` file? If not then, add `map.resources :progresses`. If that doesnt work too then try replacing `<% form_for @progress do |f| %>` with `<% form_for :progress, :url => { :action => "update" } do |f| %>` in your edit view
nas
map.resources :progresses is already defined.My edit view renders the partial form 'form'.If i replace your code with the current, it still doesn't work (syntaxError).
NicoJuicy
Sorry I missed the object use this `<% form_for :progress, @progress, :url => { :action => "update" } do |f| %>`
nas
Awesome. This worked! Thank you very much! You're a life-saver!Can you explain me why this should be changed and the default option is not enough? (Isn't required as i already accepted your solution ;))Thanks!
NicoJuicy
Actually both should work, I suggested this based on my guess that there is some inconsistency in your `config/routes.rb` file. May be somewhere you are overriding the default route defined by `map.resources :progresses`. Otherwise `<% form_for @progress do |f| %>` should work fine if the routes file is OK. Try checking routes at some point when you have time.
nas