Rails 4 Params not passed to the controller from the form -
i using form "get" method. created text field (in testing.html.erb) user enter name. want name passed controller , based on name, want retrieve data database through query. problem here not getting instance variable in controller action (nothing printed on screen when display @testing in "testing.html.erb"). below routes.rb file.
rails.application.routes.draw resources :users # priority based upon order of creation: first created -> highest priority. # see how routes lay out "rake routes". # can have root of site routed "root" # root 'welcome#index' # example of regular route: 'index' => 'users#index' 'testing' => 'users#testing' # example of named route can invoked purchase_url(id: product.id) # 'products/:id/purchase' => 'catalog#purchase', as: :purchase .....
this testing.html.erb file
<h1> testing page </h1> <%= form_for users_path, :url => {:action => "testing", :name => :name}, :html => {:method => :get} |f| %> <%= f.label :first_name %><br /> <%= f.text_field :name %><br /> <%= f.submit "view schedule" %> <% end %> <%= @testing %> <%#= @testing.email %> <%#= @testing.content %>
please note commented @testing.email/content in above file supress error (undefined method `email' nil:nilclass).
below users_controller.rb file.
class userscontroller < applicationcontroller before_action :set_user, only: [:show, :edit, :update, :destroy] #before_action :user_set, only: [:testing] # /users # /users.json def index @users = user.all @test = params[:name] @test_index = params[:age] end # /users/1 # /users/1.json def show end def testing @testing = user.find_by(name: params[:name]) #if @testing.name == "siri" # #render text: "hello #{@testing.name}" #redirect_to action: :index #end end ......... private # use callbacks share common setup or constraints between actions. def set_user @user = user.find(params[:id]) end # never trust parameters scary internet, allow white list through. def user_params params.require(:user).permit(:name, :email, :content) end end
the log-file shows following.
processing userscontroller#testing html parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"view schedule"}
i tried use strong params user.find_by(name: params[:users][:name]) throws error "undefined method `[]' nil:nilclass".
i think going wrong somewhere. please correct me. thank time.
issue lies here:
parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"view schedule"}
do see /users
key in params? shoudn't there. indicates problem form:
<%= form_for users_path, :url => {:action => "testing", :name => :name}, :html => {:method => :get} |f| %>
first argument expected string (which used name of form params) or activemodel object. in case, string returned users_path
, '/users'
. should @testing
<%= form_for @testing, :url => {:action => "testing", :name => :name}, :html => {:method => :get} |f| %>
that fix current issue, shortly after that, should go separate question.
Comments
Post a Comment