Topic: Error with drop down menu.

Hey guys. Yet another post.

Struggling with a problem i have with a drop down menu. Been looking for hours but i cant see what the issue is. Would someone mind taking a look for me please?! Thanks ever so much.

The view...

 
<p>
<b>Topic</b><br/>
<select name="newsitem[topic_id]">
<% @topics.each do |topic| %>
<option value="<%= topic.id %>">
<%= topic.topic_name %>
</option>
<% end %>
</select>
</p>

The controller...

class NewsitemsController < ApplicationController
  layout "standard"
 
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @topic = @params['topic']
    @newsitems = Newsitem.find_all
  end

  def show
    @newsitem = Newsitem.find(params[:id])
  end

  def new
    @newsitem = Newsitem.new
    @topics = Topic.find_all
  end

  def create
    @newsitem = Newsitem.new(params[:newsitem])
    @newsitem.date = Date.today
    if @newsitem.save
      flash[:notice] = 'The News item was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @newsitem = Newsitem.find(params[:id])
    @topics = Topic.find_all
  end

  def update
    @newsitem = Newsitem.find(params[:id])
    if @newsitem.update_attributes(params[:newsitem])
      flash[:notice] = 'News Item updated.'
      redirect_to :action => 'list', :id => @newsitem
    else
      render :action => 'edit'
    end
  end

  def delete
    Newsitem.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end

Re: Error with drop down menu.

What error are you getting? And for what action?

Re: Error with drop down menu.

I receive the following error...


NoMethodError in Newsitems#update

Showing app/views/newsitems/edit.rhtml where line #23 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each

Extracted source (around line #23):

20:   
21: <p><b>Topic:</b><br>
22:  <select name="newsitem[topic_id]">
23:    <% @topics.each do |topic| %>
24:        <option value="<%= topic.id %>"
25:          <%= 'selected' if topic.id == @newsitem.topic_id %>>
26:          <%= topic.topic_name %>

RAILS_ROOT: /Users/peterroome/Sites/Everycare/Website/everycare/public/../config/..
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/views/newsitems/edit.rhtml:23:in `_run_rhtml_newsitems_edit'
#{RAILS_ROOT}/app/controllers/newsitems_controller.rb:49:in `update'

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in `compile_and_render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in `render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:742:in `render_without_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:691:in `render_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:643:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:240:in `render_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:600:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:597:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
/Users/peterroome/Sites/Everycare/Website/everycare/public/dispatch.fcgi:24

#{RAILS_ROOT}/app/views/newsitems/edit.rhtml:23:in `_run_rhtml_newsitems_edit'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in `compile_and_render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in `render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:742:in `render_without_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:691:in `render_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:643:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:240:in `render_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
#{RAILS_ROOT}/app/controllers/newsitems_controller.rb:49:in `update'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:600:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:597:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
/Users/peterroome/Sites/Everycare/Website/everycare/public/dispatch.fcgi:24

Request

Parameters: {"id"=>"1", "newsitem"=>{"topic_id"=>"2", "headline"=>"The new everycare website", "overview"=>"The new website is finally here thanks to Ceative Consulting.", "date"=>"2006-12-07", "id"=>"1", "content"=>"A new database driven webiste that has all the features of an expensive aplication. We would like to thnk them very muchf ro all they have done.\r\n "}}

Show session dump

---
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}

Re: Error with drop down menu.

Sorry..


The error occurs when i try and edit an item or when i try and add a new one. On clicking the submit button or update button i get the error.

Regards.

Re: Error with drop down menu.

When you get a validation error on create and update, you render new.rhtml and edit.rhtml. These views expect a @topics variable. Your error is

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each

which means @topics is nil.

Add @topics = Topic.find(:all) on your else clause before you call render.

Re: Error with drop down menu.

Sorry for the long delay.

Cheers for that. The drop down seems to work now without any errors. However i am now unable to save or update any items to the database. It is a problem that is only occuring with this controller.

I have compared the code with other similar controllers but am unable to see anything different. Very odd.

It would appear that the .save or .update part of the if loops is being skipped and going straight to the else part.

Here is my controller again for your viewing:

class NewsitemsController < ApplicationController
  layout "standard"
 
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :delete, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @topic = @params['topic']
    @newsitems = Newsitem.find_all
  end

  def show
    @newsitem = Newsitem.find(params[:id])
  end

  def new
    @newsitem = Newsitem.new
    @topic = Topic.find_all
  end

  def create
    @topic = Topic.find_all
    @newsitem = Newsitem.new(params[:newsitem])
    @newsitem.date = Date.today
    if @newsitem.save
      flash[:notice] = 'The News item was successfully created.'
      redirect_to :action => 'list'
    else
      flash[:notice] = 'The News item was NOT created.'
      render :action => 'new'
    end
  end

  def edit
    @newsitem = Newsitem.find(params[:id])
    @topic = Topic.find_all
  end

  def update
    @topic = Topic.find(:all)
    @newsitem = Newsitem.find(params[:id])
    if @newsitem.update_attributes(params[:newsitem])
      flash[:notice] = 'News Item updated.'
      redirect_to (:action => 'show', :id => @newsitem)
    else
      flash[:notice] = 'The News item was NOT updated.'
      render :action => 'edit'
    end
  end

  def delete
    Newsitem.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end


I have been staring at this for a while now so would appreciate any input from anyone.

Thank you.

Re: Error with drop down menu.

If the model won't save because of errors, save will return false so you'll go to the else clause.

Add <%= error_messages_for "newsitem" %> on your views so you can see why the model isn't saving or updating.

Re: Error with drop down menu.

Ok so the errors i am getting are as follows...

There were problems with the following fields:

    * Content can't be blank
    * Title can't be blank
    * Overview can't be blank

Now this relates to the validation that i have in place on the model...

validates_presence_of :Title, :Overview, :Content


However, i am typing text into each of the fields and still getting this error?! Any suggestions...

Re: Error with drop down menu.

Can I look at your views? Do you get the errors for new and edit?

Re: Error with drop down menu.

New View:

 <form action="/newsitems/create" method="post">
<p>

<div id="title">
   <h1> Create A News Item...</h1>
<font size=-2><b style="color: green; text-align: right"><%= flash[:notice] %></b></font>
</div>
<br>
<b>Headline</b><br/>
<input id="newsitem_headline" name="newsitem[headline]" size="30" type="text" value=""/>
</p>

<p>
<b>Topic</b><br/>
<select name="newsitem[topic_id]">
<% @topic.each do |topic| %>
<option value="<%= topic.id %>">
<%= topic.topic_name %>
</option>
<% end %>
</select>
</p>

<p>
<b>Overview</b><br/>
<input id="newsitem_overview" name="newsitem[overview]"
    size="30" type="text" value=""/>
</p>

<p>
<b>Content</b><br/>
<textarea cols="40" id="newsitem_content" name="newsitem[content]"
    rows="15" wrap="virtual">
</textarea>

</p>
<br>

<input type="submit" value="Create"/>
<%= error_messages_for "newsitem" %>
</form>


Edit View:
<div id="title">
   <h1> Edit News Item...</h1>
    <font size=-2><b style="color: green; text-align: right"><%= flash[:notice] %></b></font>
</div>
<br>
<form action="../update/<%= @newsitem.id %>" method="POST"">
  <input id="newsitem_id" name="newsitem[id]" size="30"
         type="hidden" value="<%= @newsitem.id %>" />
 

<p><b>Headline</b><br>
  <input id="newsitem_headline" name="newsitem[headline]" size="41"
         type="text" value="<%= @newsitem.headline %>" />
  </p>


<p><b>Date</b><br>
  <input id="newsitem_date" name="newsitem[date]" size="41"
         type="text" value="<%= @newsitem.date %>" />
  </p>
 
<p><b>Topic:</b><br>
<select name="newsitem[topic_id]">
   <% @topic.each do |topic| %>
       <option value="<%= topic.id %>"
         <%= 'selected' if topic.id == @newsitem.topic_id %>>
         <%= topic.topic_name %>
       </option>
   <% end %>
  </select></p>

  <p><b>Overview</b><br>
  <textarea class="textarea" cols="40" id="newsitem_overview" name="newsitem[overview]" rows="2" wrap="virtual"><%= @newsitem.overview %></textarea></p>


  <p><b>Overview</b><br>
  <textarea class="textarea" cols="40" id="newsitem_content" name="newsitem[content]" rows="20" wrap="virtual"><%= @newsitem.content %></textarea></p>
<br>

<input type="submit" value="Update" />

<%= error_messages_for "newsitem" %>

</form>


I have taken the validation out for the time being and a new item was added and saved to the database.

I ideally would like to the validation that i mentioned in my previous post.

Thanks

Re: Error with drop down menu.

Did the new records have title, content and overview?

Try using title (lowercase t) instead of Title on your validations.
validates_presence_of :title, :overview, :content

Re: Error with drop down menu.

Ok that works brilliantly, thank you very much. Something so simple but still very annoying. That bit of code for showing the errors was helpful.

Need to add a flash when validation hasnt been satisfied.

I have one other issue now...sorry.

Having just created those new items i wanted to delete them because i populated the fields with rubbish basically. However my delete function in my controller does not appear to be doing anything. I simply get redirected  to the list of items as the function says but without the item being deleted.

 def delete
    Newsitem.find(params[:id]).destroy
    flash[:notice] = 'The NewsItem was successfully deleted.'
    redirect_to :action => 'new'
  end

The form:
<div id="title">
   <h1> News...</h1>
</div>


   
    <br>
<font size=-1><%= @newsitem.date%> || <b> <%= @newsitem.topic.topic_name %> </b></font>

<h2><%= @newsitem.headline.upcase %></h2>
   
    <font size=-2>
        <%= link_to 'Edit', :action => 'edit', :id => @newsitem.id %> |
        <%= link_to "Delete",
                :action => "delete",
                :id => newsitem.id,
                :confirm => "Are you sure you would like to delete #{@newsitem.headline}?" %>  </font>

<br>   
<br>
    <p><%= @newsitem.content %></p>
<br>
<font size=-1><b><%= link_to 'Back', :action => 'list' %></b></font>


I have tried that piece of code to flag up any errors but it does not seem to pull anything up!

Re: Error with drop down menu.

use @newsitem.id instead of newsitem.id. You forgot the @.

Re: Error with drop down menu.

Hmmmmm ok so that didnt work...

Revised Show View:

<div id="title">
   <h1> News...</h1>
</div>


   
    <br>
<font size=-1><%= @newsitem.date%> || <b> <%= @newsitem.topic.topic_name %> </b></font>

<h2><%= @newsitem.headline.upcase %></h2>
   
    <font size=-2>
        <%= link_to 'Edit', :action => 'edit', :id => @newsitem.id %> |
        <%= link_to "Delete",
                :action => "delete",
                :id => @newsitem.id,
                :confirm => "Are you sure you would like to delete #{@newsitem.headline}?" %>  </font>

<br>   
<br>
    <p><%= @newsitem.content %></p>
<br>
<font size=-1><b><%= link_to 'Back', :action => 'list' %></b></font>

<%= error_messages_for "newsitem" %>

Re: Error with drop down menu.

Add this to your delete action on the controller.

render :text => Newsitem.find(params[:id]).inspect and return

Make that the first line. You should see the news item you want to delete.

Re: Error with drop down menu.

Ok i added that to the controller but the same thing happened. When i clicked delete the redirect to list action cycled through as usual but the item is still there.

def delete
    render :text => Newsitem.find(params[:id]).inspect and return
    Newsitem.find(params[:id]).destroy
    flash[:notice] = 'The NewsItem was successfully deleted.'
    redirect_to :action => 'new'
  end

Re: Error with drop down menu.

I think you're calling the wrong delete action. What controller does the show view belong to? and the delete action? Are they the same controller?

Re: Error with drop down menu.

I have fixed it!

Brilliant.

For some reason this part of my controller had been changed to say...

verify :method => :post, :only => [ :delete, :create, :update ],
         :redirect_to => { :action => :list }

as opposed to...

verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

I have changed this now and everything seems to work. Thank you very much for your time. You have been an absolute star. Have a good evening.

Re: Error with drop down menu.

This is correct:

verify :method => :post, :only => [ :delete, :create, :update ],
          :redirect_to => { :action => :list }

delete, create, and update are your actions. Don't change it to destroy unless you have a destroy action.

You can remove delete there if you don't want to use POST in deleting. But I suggest you change your view to use POST in deleting instead of GET. Using GET is not advisable since web crawlers can delete your records even if there's a popup confirm box.

Re: Error with drop down menu.

Ok. I didnt know that thanks for the advice. Is it just a simple case of changing GET to POST then or will i need to ammend other parts of the application?