[英]Rails 4 Params not passed to the controller from the form
I am using a form for "GET" method. 我正在使用“GET”方法的表单。 I created a text field (in testing.html.erb) for user to enter the name.
我创建了一个文本字段(在testing.html.erb中)供用户输入名称。 I want the name to be passed into the controller and based on the name, I want to retrieve his data from the database through a query.
我希望将名称传递给控制器,并根据名称,我想通过查询从数据库中检索他的数据。 The problem here is that I am not getting anything into the instance variable in the controller action (Nothing is printed on screen when I display @testing in "testing.html.erb").
这里的问题是我在控制器动作中没有得到任何实例变量(当我在“testing.html.erb”中显示@testing时,屏幕上没有打印任何内容)。 Below is my routes.rb file.
下面是我的routes.rb文件。
Rails.application.routes.draw do
resources :users
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
get 'index' => 'users#index'
get 'testing' => 'users#testing'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
.....
This is my testing.html.erb file 这是我的testing.html.erb文件
<h1> Testing page </h1>
<%= form_for users_path, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :name %><br />
<%= f.submit "View Schedule" %>
<% end %>
<%= @testing %>
<%#= @testing.email %>
<%#= @testing.content %>
Please note that I commented @testing.email/content in above file to supress the error (undefined method `email' for nil:NilClass). 请注意,我在上面的文件中注释了@ testing.email / content来抑制错误(未定义的方法`email'为nil:NilClass)。
Below is my users_controller.rb file. 下面是我的users_controller.rb文件。
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
#before_action :user_set, only: [:testing]
# GET /users
# GET /users.json
def index
@users = User.all
@test = params[:name]
@test_index = params[:age]
end
# GET /users/1
# GET /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 to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email, :content)
end
end
The log-file shows the following. 日志文件显示以下内容。
Processing by UsersController#testing as HTML
Parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"View Schedule"}
I also tried to use strong params as User.find_by(name: params[:users][:name]) which throws error "undefined method `[]' for nil:NilClass". 我还尝试使用强params作为User.find_by(name:params [:users] [:name]),它为nil抛出错误“undefined method` []':NilClass”。
I think I am going wrong somewhere. 我想我在某个地方出错了。 Please correct me.
请指正。 Thank you for your time.
感谢您的时间。
Issue lies here: 问题在于:
Parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"View Schedule"}
Do you see /users
key in your params? 您是否看到
/users
键入您的参数? It shoudn't be there. 它不应该在那里。 This indicates problem with your form:
这表明您的表单有问题:
<%= form_for users_path, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
First argument is expected to be a string (which is then used as a name of form params) or ActiveModel object. 第一个参数应该是一个字符串(然后用作表单参数的名称)或ActiveModel对象。 In your case, it is string returned by
users_path
, which is just '/users'
. 在您的情况下,它是
users_path
返回的users_path
,它只是'/users'
。 It should be @testing
它应该是
@testing
<%= form_for @testing, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
That will fix your current issue, you will get another shortly after that, which should go into a separate question. 这将解决你当前的问题,在那之后不久你会得到另一个问题,这应该是一个单独的问题。
You'll be best using the following: 您最好使用以下内容:
#app/views/users/testing.html.erb
<%= @user.try(:name) %>
<%= form_tag users_testing_path, method: :get do |f| %>
<%= f.text_field :name %>
<%= f.submit "View Schedule" %>
<% end %>
This will allow: 这将允许:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def testing
@user = User.find_by name: params[:name]
end
end
You could improve your routes file too: 您也可以改进路线文件:
#config/routes.rb
resources :users, path: "", only: :index, path_names: { index: "index" } do #-> url.com/index
get :testing, on: :collection #-> url.com/testing
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.