我遇到有关基本CRUD控制器上的更新操作的问题。每次调用它时,此操作都会创建一条新记录。我已经检查了几次,但仍然无法弄明白。
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 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 %>
答案 0 :(得分:1)
您的form_for
始终指向create
路由,您可以检查在您的应用根目录中运行rake routes
的路由。
相反,请替换_form
partial:
<%= form_for :post, url: posts_path do |post| %>
为:
<%= form_for @post do |post| %>
现在,Rails会识别@post是新记录,还是持久记录并使用适当的操作(分别创建或更新)编写HTML表单