简体   繁体   English

rails params [:user_id]即使存在也返回nil

[英]rails params[:user_id] return nil even they present

When i submit the form and rails console shows parameter passing as 当我提交表单并且Rails控制台显示参数传递为

Processing by RfpsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OeKck54onJ6o72ABYvB6jMyBnU5X7BHY6NKH1sUtcHU=", "***rfp"=>{"user_id"=>"64***", "client_id"=>"3", "cms"=>"other2", "othercms"=>"dasda", "otherecommerce"=>"", "numberofpage"=>"", "designcomplexity"=>"", "browser"=>"","commit"=>"Create Rfp", "project_id"=>"18"}

in the controller when i check if params[:user_id].present? 在控制器中,当我检查if params[:user_id].present? it always shows nil even though user_id is 64 as shown above plz help me why does it happen 即使user_id为64,它始终显示nil,如上所示plz帮助我为什么会发生

EDITS 编辑

Started GET "/projects/18/rfps/157" for 127.0.0.1 at 2014-04-23 10:16:24 +0530
Processing by RfpsController#show as HTML
  Parameters: {"project_id"=>"18", "id"=>"157"}
  Current user: anonymous
Completed 500 Internal Server Error in 7ms

NoMethodError (undefined method `[]' for nil:NilClass):



def show
 @project = Project.find(params[:project_id])
@rfp = Rfp.find(params[:id])
    # r  = Rfp.find(params[:user_id])

    # m  = Rfp.find(params[:user_id])

    if params[:rfp][:user_id].present?
        @coo = User.where(:id => params[:rfp][:user_id]).first

    end
 end

in view i am jst printing name 鉴于我是第一个印刷名称

  Coordinator<%= @coo.firstname %>

这样做,这里:user_id:rfp

if params[:rfp][:user_id].present?

Try this 尝试这个

if params['***rfp'][:user_id].present?
    @coo = User.where(id: params['***rfp'][:user_id]).first
end

because params[:user_id] is not present. 因为不存在params[:user_id]

params[:rfp][:user_id] is 64, however. params[:rfp][:user_id]是64。

You have your user_id in rfp ,by doing just params[:user_id] is not sufficient to get the value here.so it returns nil . 仅通过执行params[:user_id]不足以在此处获取值,因此您在rfp拥有了user_id 。因此它返回nil

you have to do 你必须做

params[:rfp][:user_id] #=> 64

to get the value of user_id . 获取user_id的值。

So now by doing if params[:rfp]{:user_id].present? #=> true 因此,现在通过执行if params[:rfp]{:user_id].present? #=> true if params[:rfp]{:user_id].present? #=> true

will return true 将返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM