How does <% = f.submit%> know where to send?

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
+4
source share
5 answers
+5

Rails form builder, url .

, . Rails form_for. ,

builder rails

+1

<%= f.submit %> , POST. submit .

form_for ( form_tag ). (POST, GET ..).

http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object

+1

, : polymorphic_path

, ( persisted?), object model_name, , ( ActiveModel form_for form_for, ).

, polymorphic_path , URL- creatures_path. . creature_path, .

form_for , POST, .

+1

f.submit , submit HTML-.

, Rails ( HTML) - HTML. , f.submit submit. "" HTML action:

<form action="url/for/action">
   <input type="submit" value="Submit">
</form>

-

, Rails "", , RESTful Rails.

:

#config/routes.rb
resource :controller

, .

, form_for, , . IE, new, create; , update.

You will get a lot by reading the form_forassistant .

0
source

Source: https://habr.com/ru/post/1608096/


All Articles