Rails 博客:编辑正在创建重复记录
Rails Blog: Edit is Creating a Duplicate Record
在 Rails 中创建博客,编辑功能会复制一个 post,然后将更改保存到新记录中。我想要编辑所以只编辑原始的 post,不要做任何重复。
想知道是否有人可以帮助我。
这是一些代码...如果我需要 post 其他任何东西,请告诉我!
posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all.order('created_at DESC')
end
def new
if user_signed_in?
@post = Post.new
else
redirect_to new_user_session_path
end
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
def edit
if user_signed_in?
@post = Post.find(params[:id])
else
redirect_to new_user_session_path
end
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :category, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
if user_signed_in?
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
else
redirect_to new_user_session_path
end
end
private
def post_params
params.require(:post).permit(:title, :category, :body)
end
end
_form.html.erb
<%= form_for :post, url: posts_path do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :category %>
<%= f.select :category, ['x', 'y', 'z'] %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p class="actions"><%= link_to 'Back to Posts', posts_path %> | <%= f.submit %></p>
<% end %>
routes.rb
Rails.application.routes.draw do
resources :posts, :lines
get 'home/index'
devise_for :users
root 'home#index'
devise_scope :user do
get "/admin" => "devise/sessions#new"
end
end
您必须为新表单和编辑表单提供不同的 form action paths
:
<%= form_for :line, url: lines_path do |f| %>
错误在url
.
如果您按照惯例设置路由,例如在您的路由文件中使用 resources :posts
,那么您可以从表单辅助方法中删除 url
参数,然后 Rails 处理它你.
如果您的路线不符合 REST
约定。比你应该提供 url 作为部分变量的局部变量。
您的创建视图:
<%= render partial: 'form', locals: { url: path_to_create_action } %>
您的更新视图:
<%= render partial: 'form', locals: { url: path_to_update_action } %>
在你的 _form.html.erb:
<%= form_for :line, url: url do |f| %>
<!-- ... -->
<% end %>
您将在 Docs 中找到更多信息。
您的表单正在使用 POST
操作构建,因为您在 form_for
中的第一个参数是符号 :post
而不是实例 @post
。如果因为要将字段同时用于编辑视图和新视图而将表单放在部分中,则应将 Ruby 用于部分中的字段,但将 form_for
调用放在编辑和新观点。在编辑视图中,您应该使用
<%= form_for @post do |f| %>
因此它为现有记录而不是新记录构建表单。
在 Rails 中创建博客,编辑功能会复制一个 post,然后将更改保存到新记录中。我想要编辑所以只编辑原始的 post,不要做任何重复。
想知道是否有人可以帮助我。
这是一些代码...如果我需要 post 其他任何东西,请告诉我!
posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all.order('created_at DESC')
end
def new
if user_signed_in?
@post = Post.new
else
redirect_to new_user_session_path
end
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
def edit
if user_signed_in?
@post = Post.find(params[:id])
else
redirect_to new_user_session_path
end
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :category, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
if user_signed_in?
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
else
redirect_to new_user_session_path
end
end
private
def post_params
params.require(:post).permit(:title, :category, :body)
end
end
_form.html.erb
<%= form_for :post, url: posts_path do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :category %>
<%= f.select :category, ['x', 'y', 'z'] %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p class="actions"><%= link_to 'Back to Posts', posts_path %> | <%= f.submit %></p>
<% end %>
routes.rb
Rails.application.routes.draw do
resources :posts, :lines
get 'home/index'
devise_for :users
root 'home#index'
devise_scope :user do
get "/admin" => "devise/sessions#new"
end
end
您必须为新表单和编辑表单提供不同的 form action paths
:
<%= form_for :line, url: lines_path do |f| %>
错误在url
.
如果您按照惯例设置路由,例如在您的路由文件中使用 resources :posts
,那么您可以从表单辅助方法中删除 url
参数,然后 Rails 处理它你.
如果您的路线不符合 REST
约定。比你应该提供 url 作为部分变量的局部变量。
您的创建视图:
<%= render partial: 'form', locals: { url: path_to_create_action } %>
您的更新视图:
<%= render partial: 'form', locals: { url: path_to_update_action } %>
在你的 _form.html.erb:
<%= form_for :line, url: url do |f| %>
<!-- ... -->
<% end %>
您将在 Docs 中找到更多信息。
您的表单正在使用 POST
操作构建,因为您在 form_for
中的第一个参数是符号 :post
而不是实例 @post
。如果因为要将字段同时用于编辑视图和新视图而将表单放在部分中,则应将 Ruby 用于部分中的字段,但将 form_for
调用放在编辑和新观点。在编辑视图中,您应该使用
<%= form_for @post do |f| %>
因此它为现有记录而不是新记录构建表单。