【问题标题】:ActionController::RoutingError (No route matches [PATCH] - Rails 7 - Action UPDATEActionController::RoutingError (No route matches [PATCH] - Rails 7 - Action UPDATE
【发布时间】:2022-12-26 12:54:50
【问题描述】:

When I try to update a product it shows the error

ActionController::RoutingError (No route matches [PATCH] "/despensa")

I don't know what i did wrong

This is my archives:

Controller:

  def edit
        id = params[:id]
        @produto = Produto.find(id)
        @corredors = Corredor.all
        render :new
    end

    def update
        id = params[:id]
        @produto = Produto.find(id)
        valores = params.require(:produto).permit(:nome, :quantidade, :unidade_de_medida, :corredor_id)
        if @produto.update valores
            flash[:notice] = "Produto atualizado"
            redirect_to despensa_index_path
        else
            @corredors = Corredor.all
            render :new
        end
    end

Routes:

resources :corredors
    get "despensa/busca", to: "despensa#busca", as: :busca_despensa
    resources :despensa, only: [:index, :new, :create, :destroy, :edit, :update]

    root to: "home#index"

Form:

<%@produto.errors.full_messages.each do |msg| %>
   <div class="alert alert-danger mt-2" role="alert">
        <%= msg %>
    </div>
<%end%>


<%= form_for(@produto, url: despensa_index_path) do |form|%>

    <div class="row">
        <div class="col-sm-8">
            <div class="form-group">
                <%= form.label :nome %>
                <%= form.text_field :nome, class: "form-control" %>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-8">
            <div class="form-group">
                <%= form.label :quantidade %>
                <%= form.number_field :quantidade, step:0.01, class: "form-control" %>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-8">
            <div class="form-group">
                <%= form.label :unidade_de_medida %>
                <%= form.text_field :unidade_de_medida, class: "form-control" %>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-8">
            <div class="form-group">
                <%= form.label :corredor %>
                <%= form.collection_select :corredor_id, @corredors, :id, :nome, {}, class:"form-control" %>
            </div>
        </div>
    </div>


    <%= form.submit "Adicionar produto", class: "btn btn-primary mt-3"%>

<% end %>

Maybe is something wrong with the form, but i dont know. It works when i try to edit, but when i save it goes to the error.

【问题讨论】:

    标签: crud ruby-on-rails-7


    【解决方案1】:

    /despensa is not available

    Your route defines despensa as resources INSIDE corredors.

    So the path should be called something like corredors_despensa_path and not despensa_path check your routes using rails routes.

    Side note: you have to be careful naming stuff, you are using a singular name for a plural definition (despensa instead of despensas). If you can, try to write down the routes and most of the code in English, even if you are a non-native speaker.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-15
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      相关资源
      最近更新 更多