2014-12-30 2 views
0

기본 CRUD 컨트롤러에서 업데이트 작업에 문제가 있습니다. 이 작업은 내가 호출 할 때마다 새 레코드를 만듭니다. 나는 모든 것을 몇 번 확인했지만 여전히 알아 내지 못했습니다.Rails - 업데이트 동작이있는 중복 레코드

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 
단지 요청에 대한

터미널 출력;

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) 

당신의 form_for 항상 create 경로를 가리키는

<%= 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 %> 
+1

업데이트가 '/ yazilar/23'에 게시되어야하기 때문에 모양은 무엇입니까 – Doon

+1

Accordi 로그에 저장하면 게시물을 만들 수있는 요청이 표시됩니다. 업데이트되지 않습니다. 귀하의 양식은 어떻게 생겼습니까? – usha

+0

도움이 더 필요하면보기를 올리세요. 귀하의 양식 시작이 잘못되었다고 생각합니다. 객체를 참조해야하며 객체에 ID가 있으면 레일스는 제출 버튼을 올바른 URL로 가리 킵니다. 예 : form_for @ post와 같은 것 ... – pduey

답변

1

, 당신은 당신이 응용 프로그램 루트에 rake routes를 실행하는 경로를 확인할 수 있습니다 일부이든 _form입니다.

는 대신, _form 부분에서 첫 번째 라인을 교체 :

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

에 : 이제

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

, 레일은 @post 새 레코드 인 경우 인식, 또는 기록을 지속하고 HTML 양식을 작성합니다 적절한 작업 (각각 생성 또는 업데이트)

+0

감사합니다. 내 문제가 해결되었습니다. –

+0

좋아,이 답변을 수락하고이 문제를 닫으려면 선택 표시하십시오. –