1

I have a "search" page that have some controls and below is the search page code:

<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
<table>
      <tr>
         <td align="center" style="vertical-align:top;">
            <h2 style="color:Black; font-size: x-large;">Specs</h2>               
         <table>
            <tr>
               <td align="center">
                  <input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
               </td>
            </tr>             
         </table>
         <table>
            <tr>
               <td>
                  <div class="button">
                    <input type="submit" name="search" value="Search" class="buttonSearch">
                  </div>
               </td>
            </tr>
         </table>
         </td>
         <td align="center" style="vertical-align:top;">
            <h2 style="color:Black; font-size: x-large;">
                    Result
            </h2>
            <% @user_zip.each do |uzr_zip| %>
            <h1><%= uzr_zip.First_Name %></h1>
            <% end %>
            <table id="searchResult" width="100%" runat="server">
                  <tr>
                     <td bgcolor="#CCDBE0">
                        Image:
                     </td>
                     <td bgcolor="#CCDBE0">
                        <%= f.label(:zip, "Mentor") %>
                     </td>
                  </tr>
            </table>
         </td>
      </tr>
</table>
<% end %>

And when I am trying to get the textbox value into the controllers page like below

def search
    @students=Students.all
    @blah = params[:search][:tf_Zip]
    end     
    render 'search'
end

Then it gave me an error below, at this line @blah = params[:search][:tf_Zip]

undefined method `[]' for nil:NilClass

Kindle help me. Thanks

3 Answers 3

2

I think your params[:search] is nil? so for this you can use

   @blah = params[:tf_Zip]

or change your input field like this

<%= f.text_field :tf_Zip %>

or you can use like this

<input type="text" name="search[tf_Zip]" Style="text-align: left;" 
     BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
Sign up to request clarification or add additional context in comments.

Comments

1

Look at your log: you will see what is coming through in params, then you'll see why params[:search][:tf_Zip] doesn't work.

The error is telling you, effectively, that params[:search] is nil, and that you can't call [tf_Zip] on nil.

Your problem is this line:

<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">

It will populate params[:tf_Zip] because it's name is "tf_Zip". If you want it to populate params[:search][:tf_Zip] then you should set the name attribute to search[tf_Zip].

What would be nicer though is to use the rails form field helpers. I don't know why you have so much raw html inside a form_for.

<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">

can be replaced with

<%= f.text_field :tf_Zip %>

which will populate params[:search][:tf_Zip]

For the other attributes (Style etc) you should set these with css. Rails will probably put a class on the field automatically which you can use to do this. The "style" (note lowercase) attribute can be used instead but it's clumsy as it doesn't allow you to restyle the field (and more generally, your site) with css.

Comments

0

Have you checked to see if params[:search] is nil? If it is, then trying to pull [:tf_Zip] will cause that error.

Usually you'd submit the form from one controller action and handle the result in another action.

And your statement end looks misplaced.

2 Comments

params[:search] is nill
You can do traces, for instance, puts params to see the elements available to you. As another answer indicates, you should see params[:tf_Zip] does have a value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.