首页 > 解决方案 > 请查看以下问题: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)*usersidusersid

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>

标签: ruby-on-railsruby

解决方案


您的错误来自belongs_to :homePost.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您的创建操作中。


推荐阅读