gpt4 book ai didi

ruby-on-rails - Rails 4.2 Nested Forms--SQL在加载 'Edit'页面时自动删除嵌套表单内容

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:00 24 4
gpt4 key购买 nike

我用这个Blog作为在 Ruby on Rails 4.2 上学习嵌套表单 的指南。我尝试使用此指令中的相同步骤在 Post 模型中创建位置嵌套表单。

使用嵌套位置表单创建新的post 后,位置内容正确显示在post 的 显示页面上。但是,当我打开 edit 页面时,位置内容都没有了。仔细查看日志,发现如下信息:

Started GET "/categories/category#name/posts/323/edit"...
Location Load (0.1ms) SELECT "locations".* FROM "locations" WHERE "locations"."post_id" = ? LIMIT 1 [["post_id", 323]]
(0.1ms) begin transaction
SQL (0.4ms) DELETE FROM "locations" WHERE "locations"."id" = ? [["id", 27]]

似乎嵌套形式创建的内容在加载编辑页面时被 SQL 自动删除。

这是我的代码:

   class Post < ActiveRecord::Base
belongs_to :user
belongs_to :category

has_one :location, dependent: :destroy

accepts_nested_attributes_for :location, reject_if: lambda { |a| a['street'].blank? },
allow_destroy: true
def to_s
title
end
end

位置模型:

class Location < ActiveRecord::Base
belongs_to :post
has_one :user, through: :post


validates :city, presence: true
validates :zipcode, presence: true,
format: { with: /\A\d{5}\z/i }


end

帖子 Controller :

 class Categories::PostsController < ApplicationController
...
def new
@category = Category.friendly.find(params[:category_id])
@post = Post.new
@post.build_location
end

def create
@category = Category.friendly.find(params[:category_id])
@post = current_user.posts.build(post_params)
@post.category = @category

if @post.save
flash[:success] = "You have succesfully created a new post."
redirect_to [@category, @post]
else
flash[:error] = "Error occured. Please try again."
render :new
end
end

def edit
@category = Category.friendly.find(params[:category_id])
@post = Post.find(params[:id])
@post.build_location
end

def update
@category = Category.friendly.find(params[:category_id])
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
flash[:success] = "Post was upated."
redirect_to [@category, @post]
else
flash[:error] = "There was an error saving a post, please try again."
render :new
end
end
...
private

def post_params
params.require(:post).permit(:title, :body, location_attributes: [:id, :street, :city, :zipcode, :_destroy])
end

app/views/posts/_form.html.erb

<%= form_for [@category, @post] do |f| %>
...
<strong>Location:</strong>
<div class="well">
<%= f.fields_for :location do |location| %>
<%= location.label :street %>
<%= location.text_field :street, placeholder: "If you don't want to provide your address, just put 'NA' in this field" %>

<%= location.label :city %>
<%= location.text_field :city %>

<%= location.label :zipcode%>
<%= location.text_field :zipcode %>

<%= location.check_box :_destroy %>
<%= location.label :_destroy, 'remove' %>
<% end %>
</div>

虽然按照我所遵循的说明一切正常,但当我在服务器上测试我的应用程序时,我不断收到“静默”错误。如何使嵌套表单中的现有数据显示在编辑页面上?

欢迎任何见解,并提前致谢!

最佳答案

更新 Controller 中的编辑方法。仅在帖子没有关联位置时构建实例。

@post.build_location if @post.location.blank?

关于ruby-on-rails - Rails 4.2 Nested Forms--SQL在加载 'Edit'页面时自动删除嵌套表单内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32344562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com