rails params [:user_id]即使它们出现也会返回nil

时间:2014-04-23 04:59:41

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord params

当我提交表单时,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"}
当我检查if params[:user_id].present?时控制器中的

即使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

在视图中我是jst打印名称

  Coordinator<%= @coo.firstname %>

4 个答案:

答案 0 :(得分:4)

执行此操作,此处:user_id位于:rfp

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

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:0)

因为params[:user_id]不存在。

然而,

params[:rfp][:user_id]是64。

答案 3 :(得分:0)

user_id位于rfpparams[:user_id]仅仅params[:rfp][:user_id] #=> 64 不足以获得此值。因此返回 nil

你必须这样做

user_id

获取if params[:rfp]{:user_id].present? #=> true的值。

所以现在做true

将返回{{1}}