Nick
Nick

Reputation: 3090

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

For the new method below I'm running in the problem that for current_user there is no organization_id if that user directly visits the new path rather than trough a link that is provided (the link passes the id but directly visiting the url there's no id). Without an id there's the error message 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

How can I adjust the new method so that when no organization_id is provided there's an error flash and redirect? Within the existing if... line, I tried:

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

but this gave the error undefined method 'nil' for nil:NilClass.

Upvotes: 1

Views: 34

Answers (1)

potashin
potashin

Reputation: 44581

The method is called .nil?, not .nil:

params[:organization_id].nil?

Upvotes: 1

Related Questions