Please review the problems below: Rails created post

Ludovic

New Member
#1
I dont't know why but when i'm trying to create post, i got this error. I created a scaffold "post" Then i wanted to get my posts in a different controller, in my Controller home.
I put "belongs_to :home" in my Post.rb
In this step, all is fine. But when i'm trying to create post, i got this "Please review the problems below:"
Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"0QgNSnhZWnsa+U9iYi1RB2Yk+qoW1be0Mj/o3579Es74oKBD452HQxZF144KBhR+in7UaSf9OLpzAyn8aJrB6A==", "post"=>{"title"=>"sfsf", "content"=>"sdfsfsfs", "author"=>"sdfsfsdf"}, "commit"=>"Create Post"} (0.4ms) BEGIN (0.3ms) ROLLBACK Rendering posts/new.html.erb within layouts/application Rendered posts/new.html.erb within layouts/application (14.6ms) User Load (0.7ms) SELECT users. FROM users WHERE users.id = 2 ORDER BY users.id 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)*
post_controller.rb
Mã:
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
post/new.html.erb
Mã:
<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>
 

Admin

Administrator
Thành viên BQT
#2
Your error comes from belongs_to :home in Post.rb:
The belongs_to association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. (source)
If you add the error full messages in your form like this:
Mã:
<%= simple_form_for @post, wrapper: :vertical_form do |f| %>
  <ul>
    <% @post.errors.full_messages.each do |message| %>
    <li><%= message %></li>
    <% end %>
  </ul>
  ...
<% end %>
you should see this error: Home must exist

To fix your issue you can remove the belongs_to association or, if Home is really a model, add the home_id to @post in your create action before saving.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top