Mustafa Yilmaz
Mustafa Yilmaz

Reputation: 67

Rails - Duplicate record with update action

I am having a problem about update action on a basic CRUD controller. This action creates a new record everytime I have called it. I have checked everything a few times and still couldn't figure it out.

post_controller.rb

before_action :find_post, only: [:show, :edit, :update, :destroy]

def edit
end    

def update
    if @post.update(post_params)
        redirect_to @post
    else
        render 'edit'
    end
end

private

def post_params
    params.require(:post).permit(:id, :title, :body, :postdate)
end

post.rb

class Post < ActiveRecord::Base

validates :title, presence: true, length: { minimum: 5 }
validates :body, presence: true
validates :postdate, presence: true


def to_param
    "#{id} #{title}".parameterize
end

end

Terminal Output just for the requests;

Started GET "/yazilar/new" for ::1 at 2014-12-30 20:03:53 +0200
Processing by PostsController#new as HTML
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Rendered posts/_form.html.erb (23.9ms)
  Rendered posts/new.html.erb within layouts/application (31.6ms)
Completed 200 OK in 257ms (Views: 245.6ms | ActiveRecord: 0.4ms)

Started POST "/yazilar" for ::1 at 2014-12-30 20:04:05 +0200
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0A3pufznhApS6xDGrfHiVUdhgWO63kD0XGMQ+J/LxJHHsY6N7JtITc0udUjrdYFmvEa49rFzNzmsJ1sbXfM6FQ==", "post"=>{"title"=>"Post_title", "body"=>"<p>Post_body</p>", "postdate(1i)"=>"2014", "postdate(2i)"=>"12", "postdate(3i)"=>"30"}, "commit"=>"Save Post"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  (0.2ms)  BEGIN
  SQL (18.4ms)  INSERT INTO "posts" ("title", "body", "postdate", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["title", "Post_title"], ["body", "<p>Post_body</p>"], ["postdate", "2014-12-30 00:00:00.000000"], ["created_at", "2014-12-30 18:04:05.769804"], ["updated_at", "2014-12-30 18:04:05.769804"]]
  (1.1ms)  COMMIT
  Redirected to http://localhost:3000/yazilar/23-post_title
  Completed 302 Found in 29ms (ActiveRecord: 20.0ms)


 Started GET "/yazilar/23-post_title" for ::1 at 2014-12-30 20:04:05 +0200
 Processing by PostsController#show as HTML
  Parameters: {"id"=>"23-post_title"}
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", 23]]
  Rendered posts/show.html.erb within layouts/application (1.2ms)
  Completed 200 OK in 191ms (Views: 186.1ms | ActiveRecord: 0.3ms)

Started GET "/yazilar/23-post_title/edit" for ::1 at 2014-12-30 20:04:15 +0200
Processing by PostsController#edit as HTML
  Parameters: {"id"=>"23-post_title"}
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", 23]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Rendered posts/_form.html.erb (5.1ms)
  Rendered posts/edit.html.erb within layouts/application (8.1ms)
Completed 200 OK in 190ms (Views: 186.9ms | ActiveRecord: 0.5ms)


Started POST "/yazilar" for ::1 at 2014-12-30 20:04:23 +0200
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"qy4qSDrdpqJa1SD4oGgXrGRqFway3PajR8PNQpOJEWO8kk18KqFq5cUQRXbm7HSfn00uk7lxgW63h4ahUbHv5w==", "post"=>{"title"=>"Post_title *update", "body"=>"<p>Post_body</p>", "postdate(1i)"=>"2014", "postdate(2i)"=>"12", "postdate(3i)"=>"30"}, "commit"=>"Save Post"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  (0.2ms)  BEGIN
  SQL (0.2ms)  INSERT INTO "posts" ("title", "body", "postdate", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["title", "Post_title *update"], ["body", "<p>Post_body</p>"], ["postdate", "2014-12-30 00:00:00.000000"], ["created_at", "2014-12-30 18:04:23.969118"], ["updated_at", "2014-12-30 18:04:23.969118"]]
  (6.0ms)  COMMIT
Redirected to http://localhost:3000/yazilar/24-post_title-update
Completed 302 Found in 13ms (ActiveRecord: 6.7ms)


Started GET "/yazilar/24-post_title-update" for ::1 at 2014-12-30 20:04:23 +0200
Processing by PostsController#show as HTML
  Parameters: {"id"=>"24-post_title-update"}
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", 24]]
  Rendered posts/show.html.erb within layouts/application (0.8ms)
Completed 200 OK in 186ms (Views: 183.8ms | ActiveRecord: 0.3ms)

This is the _form partial

<%= form_for :post, url: posts_path do |post| %>

<% if @post.errors.any? %>
    <h2><%= pluralize(@post.errors.count, "error") %> prevented this post from saving</h2>
    <ul>
        <% @post.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
        <% end %>
    </ul>
<% end %>

<%= post.label :title %><br>
<%= post.text_field :title %><br>
<br>
<%= post.label :body %><br>
<%= post.text_area :body, :class => "redactor", :rows => 40, :cols => 40 %><br>
<br>
<%= post.label :postdate %><br>
<%= post.date_select :postdate %><br>
<br>
<%= post.submit %>

Upvotes: 0

Views: 724

Answers (1)

Alexandre Magro
Alexandre Magro

Reputation: 1181

Your form_for is always pointing to create route, you can check your routes running rake routes in you app root.

Instead, replace first line in _form partial:

<%= form_for :post, url: posts_path do |post| %>

to:

<%= form_for @post do |post| %>

Now, the Rails will recognize if @post is a new record, or persisted record and write HTML form with the appropriate action (create or update respectively)

Upvotes: 1

Related Questions