Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4634

Where does `respond_to do |format|` come from when scaffolding a Rails controller?

$
0
0

When doing rails generate scaffold Banana, bananas_controller.rb has the following code:

  # POST /bananas or /bananas.json  def create    @banana = Banana.new(banana_params)    respond_to do |format|      if @banana.save        format.html { redirect_to banana_url(@banana), notice: "Banana was successfully created." }        format.json { render :show, status: :created, location: @banana }      else        format.html { render :new, status: :unprocessable_entity }        format.json { render json: @banana.errors, status: :unprocessable_entity }      end    end  end

But when I look at the railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt template in the Rails codebase, the create method is only defined like so:

  # POST <%= route_url %>  def create    @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>    if @<%= orm_instance.save %>      redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %>    else      render :new, status: :unprocessable_entity    end  end

There is no mention of respond_to do |format| whatsoever. Where in the Rails codebase is respond_to do |format| added? I grepped the whole project to no avail.


Viewing all articles
Browse latest Browse all 4634

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>