Nil object while the required parameter are in the request

I’m having an application as follows.
I have all the parameters for as object appended in the url.
And there is an action in the controller like this

def newlet
render :layout=> false
@product = Product.new

if request.get?
@product.title= params[:title]
@product.description = params[:description]
@product.primary_link = params[:primary_link]
@product.image_url1 = params[:image_url1]
@product.primary_price = params[:primary_price]

end
end

And the url looks like
http://example.com/controllername/newlet?title=aaaa&description=bbbb&primary_link=cccc&image_url=dddd&primary_price=1220

And the corresponding view



Title

Primary link

Description

Image url1

Primary price

But the error message

NoMethodError in Admin#newlet

Showing app/views/admin/_autoform.rhtml where line #8 raised:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.title

Extracted source (around line #8):

5:
6:
7:

Title

8:


9:
10:

Primary link

11:

Trace of template inclusion: /app/views/admin/newlet.rhtml

RAILS_ROOT: C:/INSTAN~1/rails_apps/test/config/…
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/views/admin/_autoform.rhtml:8:in
_run_rhtml_admin__autoform' #{RAILS_ROOT}/app/views/admin/newlet.rhtml:5:in_run_rhtml_admin_newlet’
#{RAILS_ROOT}/app/controllers/admin_controller.rb:37:in `newlet’

C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:incompile_and_render_template’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:in
render_template' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:inrender_file’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:266:in
render' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/partials.rb:59:inrender_partial’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:29:in
benchmark' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:307:in realtime' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:29:inbenchmark’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/partials.rb:58:in
render_partial' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:278:inrender’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:incompile_and_render_template’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:in
render_template' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:inrender_file’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:726:in
render_file' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:695:inrender_with_no_layout’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/layout.rb:253:in
render_without_benchmark' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:inrender’
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:inrender’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:inperform_action_without_filters’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in
perform_action_without_benchmark' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in
perform_action' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in
process_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:inprocess_without_session_management_support’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in
process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:indispatch’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/rails.rb:73:in
process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:551:inprocess_client’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in
each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:inprocess_client’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:ininitialize’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in
initialize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:innew’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:956:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in
each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:127:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/command.rb:199:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235
C:/INSTAN~1/ruby/bin/mongrel_rails:18:in `load’
C:/INSTAN~1/ruby/bin/mongrel_rails:18

#{RAILS_ROOT}/app/views/admin/_autoform.rhtml:8:in
_run_rhtml_admin__autoform' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:in
compile_and_render_template' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:inrender_template’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:in
render_file' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:266:inrender’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/partials.rb:59:in
render_partial' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:29:inbenchmark’
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:307:inrealtime’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:29:in
benchmark' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/partials.rb:58:inrender_partial’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:278:in
render' #{RAILS_ROOT}/app/views/admin/newlet.rhtml:5:in_run_rhtml_admin_newlet’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:316:incompile_and_render_template’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:in
render_template' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:inrender_file’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:726:in
render_file' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:695:inrender_with_no_layout’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/layout.rb:253:in
render_without_benchmark' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:inrender’
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:53:inrender’
#{RAILS_ROOT}/app/controllers/admin_controller.rb:37:in newlet' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:insend’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in
perform_action_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:inperform_action_without_benchmark’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:inperform_action’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in
send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:inprocess_without_filters’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in
process_without_session_management_support' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:inprocess’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in
dispatch' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/rails.rb:73:inprocess’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:551:in
process_client' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:ineach’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in
process_client' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
initialize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:innew’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:ininitialize’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in
new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:956:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:ineach’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:127:inrun’
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/command.rb:199:in
run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235 C:/INSTAN~1/ruby/bin/mongrel_rails:18:inload’
C:/INSTAN~1/ruby/bin/mongrel_rails:18

Request

Parameters: {“title”=>“Yatego_Merino_Soft_Stringbody.jpg (JPEG
\345\234\226\347\211\207\357\274\214400x527 \345\203\217\347\264\240)”,
“primary_link”=>“http://www9.yatego.com/images/450121e567d499.5/Yatego_Merino_Soft_Stringbody.jpg”,
“primary_price”=>“2000”, “description”=>“hlghghkgf”,
“image_url1”=>“http://www9.yatego.com/images/450121e567d499.5/Yatego_Merino_Soft_Stringbody.jpg”}

I can see the request contains all the parameter needed.
And I’ve also validated each parameter.
No idea what could go wrong with this.

Stephan W. wrote:

Bontina C. wrote:

I’m having an application as follows.
I have all the parameters for as object appended in the url.
And there is an action in the controller like this

def newlet

Move this line to the end of method newlet

render :layout=> false
  @product = Product.new

if request.get?
@product.title= params[:title]
@product.description = params[:description]
@product.primary_link = params[:primary_link]
@product.image_url1 = params[:image_url1]
@product.primary_price = params[:primary_price]

end

Here

 render :layout=> false

end

Stephan

It works!!! THANKS A LOT
But I have two create button at the bottom.
When I remove
They are all gone.
I suppose there should be one left.

Bontina C. wrote:

I’m having an application as follows.
I have all the parameters for as object appended in the url.
And there is an action in the controller like this

def newlet

Move this line to the end of method newlet

render :layout=> false
  @product = Product.new

if request.get?
@product.title= params[:title]
@product.description = params[:description]
@product.primary_link = params[:primary_link]
@product.image_url1 = params[:image_url1]
@product.primary_price = params[:primary_price]

end

Here

 render :layout=> false

end

Stephan

But I have two create button at the bottom.
When I remove

Just to make sure, the create buttons both say “Create” ?

Try changing the one “Create” that you see to “Create2” and see what
happens.

I don’t recall the Windows equivalent of grep, this might help too:

grep Create app// app///*

Also it seems you should be using method=“POST”, not “GET” for your
form.
See also

http://www.theserverside.com/tt/articles/article.tss?l=RedirectAfterPost

about redirecting in response to POSTs.

Stephan

Stephan W. wrote:

But I have two create button at the bottom.
When I remove

Just to make sure, the create buttons both say “Create” ?

Try changing the one “Create” that you see to “Create2” and see what
happens.

I don’t recall the Windows equivalent of grep, this might help too:

grep Create app// app///*

Also it seems you should be using method=“POST”, not “GET” for your
form.
See also

http://www.theserverside.com/tt/articles/article.tss?l=RedirectAfterPost

about redirecting in response to POSTs.

Stephan

Thanks for the reference. I’m doing this in two stages. I use get to
fill data in the form column. And post when user makes sure this is what
the want to summit.
I figured out the difference of the two create button.
Only one of them is really functioning.

this one

But I found the data in the @product are all flushed out through this
action.

def create

@product = Product

@product.user_id = session[:user_id]
@product.created_at = Time.now

if @product.save
flash[:notice] = ‘Product was successfully created.’
redirect_to :action => ‘list’

else
  render :action => 'new'

end

end

Anything I can do to keep the data?

Many thanks

That’s a mis-paste sorry.
I fixed the problem.
Thank you so much for all the help.

But I found the data in the @product are all flushed out through this
action.

def create

You seem to mean

 @product = Product.new(params[:product])

instead of just Product.

@product = Product

@product.user_id = session[:user_id]
@product.created_at = Time.now

if @product.save
flash[:notice] = ‘Product was successfully created.’
redirect_to :action => ‘list’

else
  render :action => 'new'

end

end

Anything I can do to keep the data?

Many thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs