Nick
Nick

Reputation: 3100

SyntaxError: how to pass a parameter to a before_action?

I get a SyntaxError on the second line (second before_action) below. How can I pass in a variable (@organization) that results from the first before_action to the second before_action?


In my controller I call upon the following two before actions:

before_action :find_organization
before_action :rights?(@organization)

find_organization
  @organization = Organization.find_by(id: params[:id])
  if @organization == nil
    render json: { errors: "Organization not found" }, status: :bad_request
  end
end

The second before_action is located in my base controller:

private
def rights?(organization)
  unless rights(organization) == "full"   #Uses a helper method
    render json: {errors: "You do not have the right" }, status: :unauthorized
  end
end

Upvotes: 1

Views: 2139

Answers (1)

Omnigazer
Omnigazer

Reputation: 831

You can specify before_action with a block, like this:

before_action do
  find_organization
  rights?(@organization)
end

Or you could call rights? from find_organization, if you deem this approach applicable.

Upvotes: 3

Related Questions