ruby-on-railsrespond-to

add a local variable to a respond_to render view


I currently have the following code and it works fine:

respond_to do |format|
  format.json { render :show, status: :ok }
end

I believe :show is referring to a jbuilder view.

However, if I wanted to add a local variable to the json render, how would I do that?

I've tried the following but it hasn't worked.

auth = true
format.json { render :show, :include => auth, status: :ok }

and I've also tried

auth = true
format.json { render :show.include(auth), status: ok }

From what I found at render :json does not accept options


Solution

  • try this one

    format.json { render :show, locals:{auth: auth}, status: :ok }
    

    now you have a variable auth in your show view