Amir Nabaei
Amir Nabaei

Reputation: 1642

Params always nil in controller

I am trying to just get an input and send it to controller. But the params in controller is always nil

View (index.html.erb)

   <%= form_tag(:controller => "zip_code_lookup", :action => 'index') do  %>
     <%= text_field_tag :zip_code, params[:zip_code] %>
     <%= submit_tag("go") %>
 <% end %>

Controller

 class ZipCodeLookupController < ApplicationController
    def index
         render :text => params[:zip_code].inspect
      end
    end

Upvotes: 1

Views: 235

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

You should use params[:zip_code] in your controller, but NOT in your view.

In your index.html.erb view, replace:

<%= text_field_tag :zip_code, params[:zip_code] %>

with:

<%= text_field_tag :zip_code %>

Then, grab the zip_code value using params[:zip_code] in your controller action.

So, your view (index.html.erb)) becomes:

   <%= form_tag(:controller => "zip_code_lookup", :action => 'index') do  %>
     <%= text_field_tag :zip_code %>
     <%= submit_tag("go") %>
  <% end %>

See this article for some more information on how form_tag works.

Upvotes: 1

Related Questions