dnmxm
dnmxm

Reputation: 300

How to update with CRUD system in RAILS?

I'm learning Rails and I'm trying to use CRUD system. I want to update my article when I click on the "Edit" button. Problem, I get this error "ActionController::RoutingError (No route matches [POST] "/articles/1/edit"):"

I used "resources :articles" to create all routes for CRUD system in my routes.rb file.

Here my routes :

edit_article GET    /articles/:id/edit(.:format)  articles#edit                                                       

             PATCH  /articles/:id(.:format)       articles#update                                                             

             PUT    /articles/:id(.:format)       articles#update                                                           

Here is my form :

<%= form_with do %>
  <div class="form-group">
    <label>Titre de l'article</label>
    <%= text_field :title, '', class:'form-control' %>
  </div>
  <div class="form-group">
    <label>Contenu de l'article</label>
    <%= text_area :content, '', class:'form-control'%>
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-primary">Modifier l'article</button>
  </div>
<% end %>

I think it's related to the HTTP protocol of the update action which is in PATCH/PUT when we use :resource.

Link to documentation I used for doing this : https://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

Thank for your help !

Upvotes: 0

Views: 210

Answers (3)

Chiara Ani
Chiara Ani

Reputation: 1083

Try this:

<%= form_with model: @article do |t| %>
  <div class="form-group">
    <label>Titre de l'article</label>
    <%= t.text_field :title, class:'form-control' %>
  </div>
  <div class="form-group">
    <label>Contenu de l'article</label>
    <%= t.text_area :content, class:'form-control'%>
  </div>
  <div class="form-group">
    <%= t.submit "Modifier l'article", class: "btn btn-primary" %>
  </div>
<% end %>

Upvotes: 0

cercxtrova
cercxtrova

Reputation: 1673

form_with will replace form_tag and form_for that will be depreciated in the future, because it combines these two helpers.

In your form, you forgotten your object, so the form is not able to know what should be updated.

Replacing form_with do by form_with model: @article do should do the trick

<%= form_with model: @article do %>
  <div class="form-group">
    <label>Titre de l'article</label>
    <%= text_field :title, '', class:'form-control' %>
  </div>
  <div class="form-group">
    <label>Contenu de l'article</label>
    <%= text_area :content, '', class:'form-control'%>
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-primary">Modifier l'article</button>
  </div>
<% end %>

Upvotes: 0

Raju Rekadi
Raju Rekadi

Reputation: 98

Try this you need to give controller action through form submit

<%= form_for @artice, url: {action: :update} do |f| %>
  <div class="form-group">
    <label>Titre de l'article</label>
    <%= text_field :title, '', class:'form-control' %>
  </div>
  <div class="form-group">
     <label>Contenu de l'article</label>
     <%= text_area :content, '', class:'form-control'%>
  </div>
 <div class="form-group">
    <button type="submit" class="btn btn-primary">Modifier l'article</button>
 </div>
<% end %>

Upvotes: 2

Related Questions