Rails博客被保存为' nil'出版后

时间:2017-03-31 18:48:15

标签: ruby-on-rails ruby blogs

我是rails的初学者,我正在创建一个带有标题图片,标题,作者,摘录和正文的博客文章。博客文章似乎是正确创建的,但没有显示任何博文。当我检查rails控制台时,博客帖子会显示,但是对于所有内容,它们都保存为零。请借给你任何支持!

谢谢,

#应用程序/控制器/ posts_controller.rb

class PostsController < ApplicationController
  before_action :find_post, only: [:edit, :update, :show, :delete]

  # Index action to render all posts
  def index
    @posts = Post.all
  end

  # New action for creating post
  def new
    @post = Post.new
  end

  # Create action saves the post into database
  def create
    @post = Post.new
    if @post.save(post_params)
      flash[:notice] = "Successfully created post!"
      redirect_to post_path(@post)
    else
      flash[:alert] = "Error creating new post!"
      render :new
    end
  end

  # Edit action retrives the post and renders the edit page
  def edit
  end

  # Update action updates the post with the new information
  def update
    if @post.update_attributes(post_params)
      flash[:notice] = "Successfully updated post!"
      redirect_to post_path(@posts)
    else
      flash[:alert] = "Error updating post!"
      render :edit
    end
  end

  # The show action renders the individual post after retrieving the the id
  def show
    @post = Post.find(params[:id])
  end

  # The destroy action removes the post permanently from the database
  def destroy
    if @post.destroy
      flash[:notice] = "Successfully deleted post!"
      redirect_to posts_path
    else
      flash[:alert] = "Error updating post!"
    end
  end

  private

  def post_params
    params.require(:post).permit(:title, :excerpt, :image, :author, :body)
  end

  def find_post
    @post = Post.find(params[:id])
  end
end

rails console

irb(main):002:0> @posts = Post.all
  Post Load (0.2ms)  SELECT "posts".* FROM "posts"
=> #<ActiveRecord::Relation [#<Post id: 1, title: nil, excerpt: nil, author: nil, body: nil, created_at: "2017-03-31 17:27:18", updated_at: "2017-03-31 17:27:18", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>, #<Post id: 2, title: nil, excerpt: nil, author: nil, body: nil, created_at: "2017-03-31 17:46:04", updated_at: "2017-03-31 17:46:04", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>]>

show.html.erb

<div class="col-sm-11 col-xs-12 blog-content">
  <h2><%= @post.title %></h2>
  <h5><%= @post.created_at.strftime('%b %d, %Y') %></h5>
  <%= image_tag(@post.image.url(:large)) %>
  <div><%= @post.body %></div>
</div>

index.html.erb

<% @posts.each do |post| %>
  <%= post.title %>
  <%= post.created_at.strftime('%b %d, %Y') %>
  <%= image_tag post.image %>
  <%= post.body %>
  <%= link_to "READ MORE", post_path(post) %>
<% end %>

_form.html.erb

<%= simple_form_for @post, html: { multipart: true } do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:" %>
      </h2>
      <ul>
        <% @post.errors.full_messages.each do |msg| %>
          <li>
            <%= msg %>
          </li>
          <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.input :title, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.input :excerpt, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.input :author, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :image %>
    <%= f.file_field :image, class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= f.input :body, :as => :ckeditor, input_html: {:ckeditor => {:toolbar => 'FULL'}}, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.button :submit %>
  </div>
<% end %>

post.rb

class Post < ApplicationRecord
  has_attached_file :image, styles: { icon: "150x150", small: "350x350", med: "500x500", large: "750x750" }
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

如果您有任何建议或需要更多信息以帮助解决此问题,请告知我们。

谢谢,

1 个答案:

答案 0 :(得分:1)

您应该将post_params传递给控制器​​中的Post.new,而不是Post#save您的创建操作应如下所示:

# Create action saves the post into database
def create
  @post = Post.new(post_params)
  if @post.save
    flash[:notice] = "Successfully created post!"
    redirect_to post_path(@post)
  else
    flash[:alert] = "Error creating new post!"
    render :new
  end
end