I am building a form to update a record, it is displayed correctly and after filling in the fields and clicking submit it gives the following error: No route matches [POST] "/articles/1".
The version of rails that I have is 7.0.2.3 and the version of ruby is 3.1.1p18 and the OS is windwos 10.
routes.rb
get 'articles/:id/edit' => 'articles#edit'
put 'articles/:id' => 'articles#update'
driver articles
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_param)
redirect_to :action => :show, :id => @article.id
end
end
private def article_param
params.permit(:title, :content)
end
The formula
<%= form_for :posts_path, :url => { :action => :update, :id => @article.id }, :class => "w-25" do %>
<div class="mb-3">
<%= label_tag :title, nil, :class => "form-label" %><br>
<%= text_field_tag :title, nil, :class => "form-control" %>
</div>
<div class="mb-3">
<%= label_tag :content, nil, :class => "form-label" %><br>
<%= text_area_tag :content, nil, :class => "form-control" %>
</div>
<p>
<%= submit_tag :Enviar %>
</p>
<% end %>
If you are using rails 7, you should use
form_with
, as itform_for
will be deprecated in future releases .Regarding the use of PUT, following the Rails documentation , what you should do is: