如何调整新方法以提供 id 参数不存在的情况?

How to adjust new method to provide for situation that id parameter is not present?

对于下面的 new method 我 运行 的问题是 current_user 没有 organization_id 如果该用户直接访问新路径而不是通过提供的 link(link 传递 id 但直接访问 url 没有 id)。没有 id 就会出现错误消息 Couldn't find Organization with 'id'=

def new
  if current_user && current_user.admin?
    @organization = Organization.find(params[:organization_id])
    @member = @organization.members.build
  elsif current_member && current_member.admin?
    @member = current_organization.members.build
  else
    flash[:danger] = "Error"
  end
end

如何调整 new method 以便在未提供 organization_id 时出现错误提示和重定向?在现有的 if... 行中,我尝试了:

if params[:organization_id].nil
  flash[:danger] = "Please select an organization first"
  redirect_to organizations_path
end

但这给出了错误 undefined method 'nil' for nil:NilClass

调用的方法是.nil?,不是.nil:

params[:organization_id].nil?