我正在使用“GET”方法的表单。我创建了一个文本字段(在testing.html.erb中)供用户输入名称。我希望将名称传递给控制器,并根据名称,我想通过查询从数据库中检索他的数据。 这里的问题是我在控制器动作中没有得到任何实例变量(当我在“testing.html.erb”中显示@testing时,屏幕上没有打印任何内容)。下面是我的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
.....
这是我的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 %>
请注意,我在上面的文件中评论了@ testing.email / content来压缩错误(未定义的方法`email'为nil:NilClass)。
以下是我的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
日志文件显示以下内容。
Processing by UsersController#testing as HTML
Parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"View Schedule"}
我还尝试使用强params作为User.find_by(name:params [:users] [:name]),它为nil抛出错误“undefined method` []':NilClass”。
我想我在某个地方出错了。请指正。谢谢你的时间。
答案 0 :(得分:3)
问题在于:
Parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"View Schedule"}
你看到你的参数中的/users
键吗?它不应该在那里。这表明您的表单存在问题:
<%= form_for users_path, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
第一个参数应该是一个字符串(然后用作表格参数的名称)或ActiveModel对象。在您的情况下,它是users_path
返回的字符串,仅为'/users'
。它应该是@testing
<%= form_for @testing, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
这将解决您当前的问题,在此之后不久您会得到另一个问题,这应该是一个单独的问题。
答案 1 :(得分:0)
您最好使用以下内容:
#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 %>
这将允许:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def testing
@user = User.find_by name: params[:name]
end
end
您也可以改善路线文件:
#config/routes.rb
resources :users, path: "", only: :index, path_names: { index: "index" } do #-> url.com/index
get :testing, on: :collection #-> url.com/testing
end