ruby-on-rails - 请查看以下问题:Rails created post
问题描述
我不知道为什么,但是当我尝试创建帖子时,我收到了这个错误。我创建了一个脚手架“帖子”然后我想在我的控制器家中的另一个控制器中获取我的帖子。
我把“belongs_to :home”放在我的 Post.rb
在这一步中,一切都很好。但是当我尝试创建帖子时,我得到了这个“请查看以下问题:”
PostsController#create 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"0QgNSnhZWnsa+U9iYi1RB2Yk+qoW1be0Mj/o3579Es74oKBD452HQxZF144KBhR+in7UaSf9OLpzAyn8aJrB6A==", "post"=>{"title"=>" sfsf", "content"=>"sdfsfsfs", "author"=>"sdfsfsdf"}, "commit"=>"Create Post"} (0.4ms) BEGIN (0.3ms) ROLLBACK 在布局/应用程序中渲染帖子/new.html.erb 在布局/应用程序中渲染帖子/new.html.erb (14.6ms) 用户负载 (0.7ms) SELECT布局/应用程序中的 erb (14.6ms) 用户负载 (0.7ms) SELECT布局/应用程序中的 erb (14.6ms) 用户负载 (0.7ms) SELECT users
. 从哪里来。users
_ = 2 订购方式。ASC LIMIT 1 Rendered layouts/_header.html.erb (6.8ms) Rendered layouts/_footer.html.erb (0.9ms) Completed 200 OK in 1512ms (Views: 381.2ms | ActiveRecord: 1.4ms)*users
id
users
id
post_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all.order("created_at DESC")
end
# GET /posts/1
# GET /posts/1.json
def show
@posts
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html {redirect_to @post, notice: 'Article crée.'}
format.json {render :show, status: :created, location: @post}
else
format.html {render :new}
format.json {render json: @post.errors, status: :unprocessable_entity}
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html {redirect_to @post, notice: 'Article édité.'}
format.json {render :show, status: :ok, location: @post}
else
format.html {render :edit}
format.json {render json: @post.errors, status: :unprocessable_entity}
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html {redirect_to posts_url, notice: 'Article supprimé.'}
format.json {head :no_content}
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :content, :author)
end
end
发布/new.html.erb
<div class="container left-box">
<h1 class="left-box-title">Ajouter un article</h1>
<%= simple_form_for @post, wrapper: :vertical_form do |f| %>
<%= f.error_notification %>
<%= f.input :title, label: 'Titre' %>
<p>Contenu</p>
<%= f.text_area :content, class: 'col-md-12 form-control content_post_create' %>
<%= f.input :author, label: 'Auteur' %>
<%= f.button :submit, class: "btn-primary", label: 'Mettre en ligne' %>
<%= f.button :button, "Recommencer", type: "reset", class: "btn-outline-secondary" %>
<% end %>
<%= link_to "Retour à l'accueil", posts_path %>
</div>
解决方案
您的错误来自belongs_to :home
Post.rb:
belongs_to 关联创建与另一个模型的一对一匹配。在数据库术语中,该关联表示此类包含外键。(来源)
如果您在表单中添加错误完整消息,如下所示:
<%= simple_form_for @post, wrapper: :vertical_form do |f| %>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
...
<% end %>
你应该看到这个错误:Home must exist
要解决您的问题,您可以删除belongs_to
关联,或者,如果Home
确实是模型,则在保存之前将其添加home_id
到@post
您的创建操作中。
推荐阅读
- facebook-graph-api - 如何使用 Graph API 从公共页面获取帖子?
- r - 如何重新排列 R 数据框中不同列中的值
- javascript - 在反应中,我有 JQuery npm,我在我的 chart-custom.js 文件中使用了 JQuery,但仍然收到 jQuery undefined 错误
- python - 导入后覆盖模块函数 - Python
- vba - VBA selenium 单击附近的网格列
- python - 使用 str.contains 创建新列 Pandas df 给出:值的长度与索引的长度不匹配
- c# - 如何根据 SQLite DB 表中的表制作 Picker 显示选项?
- node.js - 带有温度传感器的 SocketIO - 我几乎用 NodeJS 和 SocketIO 在我的 Raspberry Pi 上完成了 DS18B20,如何提取 Promise { data }?
- google-maps - 颤动谷歌地图缩放控制位置
- docker - 流利的 JSON 日志字段未被解析