【问题标题】:How to adjust new method to provide for situation that id parameter is not present?如何调整新方法以提供 id 参数不存在的情况?
【发布时间】:2015-05-02 13:20:32
【问题描述】:

对于下面的new method,我遇到的问题是,对于 current_user,如果该用户直接访问新路径而不是通过提供的链接而不是通过提供的链接(链接传递 id 但直接访问 url没有身份证)。如果没有 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 以便在未提供组织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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 null


    【解决方案1】:

    该方法被称为.nil?,而不是.nil

    params[:organization_id].nil?
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多