During a jumble on rails, I encoded this:
<%= form_for :creature, url: "/creatures", method: "post" do |f| %>
<%= f.text_field :name %>
<%= f.text_area :description %>
<%= f.submit "save creature" %>
<% end %>
I clearly understand where these messages are. However, I found that this also works:
<%= form_for @creature do |f| %>
<%= f.text_field :name %>
<%= f.text_area :description %>
<%= f.submit "save creature", class: "btn btn-default"%>
<% end %>
I was browsing folders in my rails application, and I know that it is still sending post '/ creatures' => 'creatures # create' to this endpoint . And I want to know how on earth the second block of code knows that it is still sent to this endpoint. The transition from Node.JS to Rails is a twist of the mind.
Here is my controller for the curious:
class CreaturesController < ApplicationController
def index
@creatures = Creature.all
end
def new
@creature = Creature.new
end
def create
new_creature = params.require(:creature).permit(:name, :description)
creature = Creature.create(new_creature)
redirect_to "/creatures/#{creature.id}"
end
def show
id = params[:id]
@creature = Creature.find(id)
end
end
source
share