When editing a user pops up an error

No route matches [POST] "/users/14/edit" 

What is the reason for this? Controller

  def update if @user.update(user_params) redirect_to users_path else render 'edit' end end 

View with form

 = form_for :user do |f| = f.text_field :type, class: "form-control", placeholder: "Поменять роль" = f.submit 

In routes.rb

 resources :users 

I understand that I need a post-request, and get pops up, but why is this happening?

    2 answers 2

    Prior to the update action, it did not even reach.

    form_for takes a whole form_for options, actually. But almost all are optional.

    1. the key under which the form parameters will lie
    2. URL to which the form should be sent (current page by default)
    3. HTTP method ( GET / POST / PUT , etc., default POST )
    • (and several others, but others are not relevant)

    Many of them can be transferred in the form of "here's an object of the model for you, figure it out yourself" :

    1. The key will be the model name in snake_case and singular.
    2. URL:
      • if .new? on the corresponding resource модели_path
      • otherwise (if .persisted? ) on the edit_модель_path(объект) corresponding resource
    3. Similar to the choice above, POST or PUT respectively.

    Sometimes it makes sense to set the parameters manually:

     form_for :ключ, url: адрес, method: http_метод 

    By passing only the character, you specified only the key .
    The URL and method remain the default for forms in general (current page and POST ).

    But usually still use the model object. For a form, Модель.new taken for creation, and the object being edited, respectively, for editing.

      = form_for :user do |f| - output form to create a new entity. To display a form for editing a specific user, you must first get it in the controller:

       def edit @user = User.find(params[:id]) end 

      To draw a form for editing it, in a view:

       = form_for @user do |f| ... = f.submit 
      • Thank. Those. for action new you need to use the symbol :user , and for editing - @user . But how then to use them in partials? I usually use @user . - Andrey
      • Just now I noticed: “withdrawal of the form for creating a new entity” - no. Although this may work in the case when the creation form is located at the traditional place for index , since the path will turn out to be the same, and the form makes POST by default. - D-side