Form parameters not passing to controller

Thanks in advance for your insights....

I have searched for some help on this issue but so far nothing seems to work. I am new to rails but the code was already written.

Process is: User enters data on the form, hits submit, new search is created. This is a search app that allows you to create new 'searches' and save the results and the parameters for later review.

The problem seems to be that the form data is not getting passed to the controller after hitting the submit button. Instead I get "waiting for localhost" on the browser status and finally rails gives an error. If I comment out "@search.save" under 'def create' in the controller and instead render inline @search it shows '#' instead of the actual form data. Any thoughts??

**Here is the _form.html.erb code:**

<% form_for @search do |f| %>   <%= f.error_messages %>   <p>     <%= f.label :project %><br />     <%= collection_select(:search, :project_id, current_user.projects, :id, :name) %>   </p> <p>     Search Words A<br />     <%= f.text_field :setA %>     <%= collection_select(:search, :setAList, Autoterm.all, :id, :name) %>   </p>    <p>     Search Words B<br />     <%= f.text_field :setB %>     <%= collection_select(:search, :setBList, Autoterm.all, :id, :name) %>   </p>   <p>     Automatically run search every day?<br />     <%= f.check_box :auto %><br/>

  Include Google Search Results?<br/>   <%= f.check_box :google%><br/>

  Use Find All Words Filter?<br/>   <%= f.check_box :atf%><br/>

  Use Matching Word Filter?<br/>   <%= f.check_box :mwf%>   </p>   <p>     <%= f.label :notes %><br />     <%= f.text_area :notes %>   </p>   <p><%= f.submit "Create" %></p> <% end %>

**This is the new.html.erb code that calls the above partial:**

<% title "New Search" %>

<%= render :partial => 'form' %>

<p><%= link_to "Back to List", searches_path %></p>

**Here is part of the search.rb code:**

require 'yaml' require 'set' require 'google' require 'stemmer' require 'quick_log'

class String   include Stemmable end

class Search < ActiveRecord::Base   belongs_to :project   has_one :user, :through => :project

  attr_accessor :setA, :setB, :setAList, :setBList, :google   before_create :setup_data   before_save :setup_data

  def setup_data     @setax = setA.split' '     @setbx = setB.split' '      if setAList != 0       @setax << YAML.load(Autoterm.find(setAList).terms) .....more code.......

**And finally, here is part of the searches_controller.rb code:**

class SearchesController < ApplicationController   before_filter :login_required   include ActionView::Helpers::DateHelper   def index     redirect_to projects_path unless admin?     @search = Search.all   end

  def show     @search = Search.find_by_id(params[:id])   end

  def new     @search = Search.new   end

  def create     @search = Search.new(params[:search])      if @search.save       flash[:notice] = "Successfully created search."       redirect_to @search      else       render :action => 'new'      end    end   .....more code.....

Thanks in advance for your insights....

I have searched for some help on this issue but so far nothing seems to work. I am new to rails but the code was already written.

Process is: User enters data on the form, hits submit, new search is created. This is a search app that allows you to create new 'searches' and save the results and the parameters for later review.

The problem seems to be that the form data is not getting passed to the controller after hitting the submit button. Instead I get "waiting for localhost" on the browser status and finally rails gives an error. If I comment out "@search.save" under 'def create' in the controller and instead render inline @search it shows '#' instead of the actual form data. Any thoughts??

What makes you so sure that the form data isn't being passed properly? To me it sounds like your model code is doing something lengthy and eventually times out (what is the error that occurs?)

Fred

Frederick Cheung wrote in post #995643:

controller after hitting the submit button. Instead I get "waiting for localhost" on the browser status and finally rails gives an error. If I comment out "@search.save" under 'def create' in the controller and instead render inline @search it shows '#' instead of the actual form data. Any thoughts??

What makes you so sure that the form data isn't being passed properly? To me it sounds like your model code is doing something lengthy and eventually times out (what is the error that occurs?)

Fred

Thanks for responding.

Well, that could be the case. I remembered last night that I had commented out parts of this section:

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

Just to see if that was the cause of another error. Here is the error that happens when it is not commented out:

NoMethodError in SearchesController#create

undefined method `each' for true:TrueClass

/home/brad/Documents/blogazer/app/models/search.rb:201:in `set_from_sources' /home/brad/Documents/blogazer/app/models/search.rb:107:in `execute' /home/brad/Documents/blogazer/app/models/search.rb:73:in `setup_data' /home/brad/Documents/blogazer/app/controllers/searches_controller.rb:22:in `create'

Request

Parameters:

{"commit"=>"Create", "authenticity_token"=>"V4z2rM09NbCGGH7Kua/HGzhzybKXD1bjRQgKvdvwavc=", "search"=>{"setB"=>"linux", "project_id"=>"1", "notes"=>"test", "google"=>"0", "mwf"=>"0", "setAList"=>"2", "auto"=>"0", "atf"=>"0", "setA"=>"server", "setBList"=>"2"}}

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

Just to see if that was the cause of another error. Here is the error that happens when it is not commented out:

NoMethodError in SearchesController#create

undefined method `each' for true:TrueClass

/home/brad/Documents/blogazer/app/models/search.rb:201:in `set_from_sources' /home/brad/Documents/blogazer/app/models/search.rb:107:in `execute'

I think you need to look at the line above in search.rb. It suggests you are calling each for something that is not a collection but has the value true.

Colin

Colin Law wrote in post #995808:

Just to see if that was the cause of another error. Here is the error that happens when it is not commented out:

NoMethodError in SearchesController#create

undefined method `each' for true:TrueClass

/home/brad/Documents/blogazer/app/models/search.rb:201:in `set_from_sources' /home/brad/Documents/blogazer/app/models/search.rb:107:in `execute'

I think you need to look at the line above in search.rb. It suggests you are calling each for something that is not a collection but has the value true.

Colin

Hmmm... Here is the code from around line 201 in search.rb:

def set_from_sources(sources)     final = Set.new      sources.each do |source|       p = Page.find_by_id source       h = YAML.load(p.data)       final.merge h.keys     end     final   end

Here is line 107 in search.rb: gamma[:D] = set_from_sources(gamma[:C]) - (txv[:a] + txv[:b])

If sources is empty from the form data not getting passed to it or sources getting assigned invalid data could that be causing the error? Please ignore my rails stupidity. Thanks

David Mr. wrote in post #995865:

Colin Law wrote in post #995808:

Just to see if that was the cause of another error. Here is the error that happens when it is not commented out:

NoMethodError in SearchesController#create

undefined method `each' for true:TrueClass

/home/brad/Documents/blogazer/app/models/search.rb:201:in `set_from_sources' /home/brad/Documents/blogazer/app/models/search.rb:107:in `execute'

I think you need to look at the line above in search.rb. It suggests you are calling each for something that is not a collection but has the value true.

Colin

Hmmm... Here is the code from around line 201 in search.rb:

def set_from_sources(sources)     final = Set.new      sources.each do |source|       p = Page.find_by_id source       h = YAML.load(p.data)       final.merge h.keys     end     final   end

Here is line 107 in search.rb: gamma[:D] = set_from_sources(gamma[:C]) - (txv[:a] + txv[:b])

If sources is empty from the form data not getting passed to it or sources getting assigned invalid data could that be causing the error? Please ignore my rails stupidity. Thanks

Just found out if I leave the form fields empty and hit submit, the error changes to:

undefined method `-' for true:TrueClass

Does this help narrow things down? Thanks

Colin Law wrote in post #995808:

Just to see if that was the cause of another error. Here is the error that happens when it is not commented out:

NoMethodError in SearchesController#create

undefined method `each' for true:TrueClass

/home/brad/Documents/blogazer/app/models/search.rb:201:in `set_from_sources' /home/brad/Documents/blogazer/app/models/search.rb:107:in `execute'

I think you need to look at the line above in search.rb. It suggests you are calling each for something that is not a collection but has the value true.

Colin

Hmmm... Here is the code from around line 201 in search.rb:

def set_from_sources(sources) final = Set.new sources.each do |source|

The error suggests that sources is not a collection

 p = Page\.find\_by\_id source
 h = YAML\.load\(p\.data\)
 final\.merge h\.keys

end final end

Here is line 107 in search.rb: gamma[:D] = set_from_sources(gamma[:C]) - (txv[:a] + txv[:b])

So gamma[:C] is not a collection. Have a look at the Rails Guide on debugging and find how to use ruby-debug to break into your code so you can examine gamma.

Colin

Colin Law wrote in post #995918:

/home/brad/Documents/blogazer/app/models/search.rb:201:in

Here is the code from around line 201 in search.rb:

def set_from_sources(sources) final = Set.new   sources.each do |source|

The error suggests that sources is not a collection

  p = Page.find_by_id source   h = YAML.load(p.data)   final.merge h.keys end final end

Here is line 107 in search.rb: gamma[:D] = set_from_sources(gamma[:C]) - (txv[:a] + txv[:b])

So gamma[:C] is not a collection. Have a look at the Rails Guide on debugging and find how to use ruby-debug to break into your code so you can examine gamma.

Colin

OK. I used debugger and found that 'setax and 'setbx' are nil instead of being assigned the strings in 'setA.split(" ")' and setB.split(" ").

Using debugger I verified that setA has the proper string value entered at the form.

Even if I change the code to setax = setA, setax is always nil.

Does the fact that setA and setB are in the Attribute-Accessor list make a difference in the way I should assign other variables their value?

Here is the code:

class Search < ActiveRecord::Base   belongs_to :project   has_one :user, :through => :project

  attr_accessor :setA, :setB, :setAList, :setBList, :google   before_create :setup_data   before_save :setup_data   def setup_data    debugger     setax =     setbx =     setax = setA.split(" ")     setbx = setB.split(" ")

Thanks!

Colin Law wrote in post #995918:

/home/brad/Documents/blogazer/app/models/search.rb:201:in

Here is the code from around line 201 in search.rb:

def set_from_sources(sources) final = Set.new sources.each do |source|

The error suggests that sources is not a collection

p = Page.find_by_id source h = YAML.load(p.data) final.merge h.keys end final end

Here is line 107 in search.rb: gamma[:D] = set_from_sources(gamma[:C]) - (txv[:a] + txv[:b])

So gamma[:C] is not a collection. Have a look at the Rails Guide on debugging and find how to use ruby-debug to break into your code so you can examine gamma.

Colin

OK. I used debugger and found that 'setax and 'setbx' are nil instead of being assigned the strings in 'setA.split(" ")' and setB.split(" ").

Using debugger I verified that setA has the proper string value entered at the form.

Even if I change the code to setax = setA, setax is always nil.

Does the fact that setA and setB are in the Attribute-Accessor list make a difference in the way I should assign other variables their value?

Here is the code:

class Search < ActiveRecord::Base belongs_to :project has_one :user, :through => :project

attr_accessor :setA, :setB, :setAList, :setBList, :google

First I would stick to the Rails conventions when naming things, so these should be set_a and so on.

before_create :setup_data before_save :setup_data def setup_data debugger setax = setbx =

You don't need the above as split will return an array

setax = setA.split(" ") setbx = setB.split(" ")

Are you saying that this method gets called ok but at this point, checked in the debugger, that setA has a sensible value but setax does not?

It might help also if you explained whether any of the variables are db columns and which are just instance variables.

Colin

Colin Law wrote in post #995978:

So gamma[:C] is not a collection. Have a look at the Rails Guide on

attr_accessor :setA, :setB, :setAList, :setBList, :google

First I would stick to the Rails conventions when naming things, so these should be set_a and so on.

before_create :setup_data before_save :setup_data def setup_data debugger setax = setbx =

You don't need the above as split will return an array

setax = setA.split(" ") setbx = setB.split(" ")

Are you saying that this method gets called ok but at this point, checked in the debugger, that setA has a sensible value but setax does not?

It might help also if you explained whether any of the variables are db columns and which are just instance variables.

Colin

As a quick reply...

Yes setA has a sensible -as expected value, but setax is always nil. I can go to the debugger window and do setax = setA.split(" ") and then setax has the proper value. How is that happening?

No, the variables are not the column names. The setA and setB data are put into a table named 'searches' and column named 'terms'. The setA and setB data are entered from a form using form_for.

Thanks

So if you put a break point immediately after setax = setA.split(" ") then setax is nil, but if you then (while still broken) do setax=setA.split(" ") then setax gets a value. If so I find that most remarkable.

You have not said how setax and setbx are defined. Are they db cols for the model in which setup_data is defined or what?

Colin

Colin Law wrote in post #996041:

So if you put a break point immediately after setax = setA.split(" ") then setax is nil, but if you then (while still broken) do setax=setA.split(" ") then setax gets a value. If so I find that most remarkable.

You have not said how setax and setbx are defined. Are they db cols for the model in which setup_data is defined or what?

Colin

Correct. I put a break point immediately after setax = setA.split(" ") and it is nil. p setax nil

Then do setax = setA.split(" ") and setax gets a value. p setax ["test", "linux"]

setax and setbx are only variables used to for searching columns in the Search table. They are not used anywhere else. Part of the code below:

if setBList != 0       setbx << YAML.load(Autoterm.find(setBList).terms)       setbx.flatten!     end     setag =     setax.each do |w|       setag << Transform.transform(w.stem.strip)     end     setbg =     setbx.each do |w|       setbg << Transform.transform(w.stem.strip)     end

I hope this info is what you were asking about. Thanks for your help!

Colin Law wrote in post #996041:

So if you put a break point immediately after setax = setA.split(" ") then setax is nil, but if you then (while still broken) do setax=setA.split(" ") then setax gets a value. If so I find that most remarkable.

You have not said how setax and setbx are defined. Are they db cols for the model in which setup_data is defined or what?

Colin

Correct. I put a break point immediately after setax = setA.split(" ") and it is nil. p setax nil

Then do setax = setA.split(" ") and setax gets a value. p setax ["test", "linux"]

Very odd, even ludicrous. Could you copy and paste the relevant bit of the terminal output here? Also paste the setup_data method please. In your first version of the code it was @setax, now it is setax. Which is it?

setax and setbx are only variables used to for searching columns in the Search table. They are not used anywhere else. Part of the code below:

So are they just local variables within that method? Can you do a global search for setax and make sure they are not defined elsewhere also. The only way I can see you getting the effect you are seeing in the debugger is if there is more than one var of that name and the debugger is showing the wrong one. You could rename it in that method to see what happens.

Colin

Colin Law wrote in post #996087:

So are they just local variables within that method? Can you do a global search for setax and make sure they are not defined elsewhere also. The only way I can see you getting the effect you are seeing in the debugger is if there is more than one var of that name and the debugger is showing the wrong one. You could rename it in that method to see what happens.

Colin

I'll try to post the terminal output soon. It is setax, I had tried @setax to see if it made a difference, it didn't. Yes, they are just local variables used only in search.rb. I renamed them too, no dice.

Thanks

OK. Problem solved. Rails ignorance. Your right Colin, ludicrous. Thank you for your help Colin and everyone!!