2017-03-31 59 views
0

我是rails的初学者,我创建了一个包含标题图像,标题,作者,摘录和正文文本的博客文章。该博文似乎被正确创建,但不显示任何博客文章。当我检查轨道控制台时,博客帖子显示,但它们被保存为零。请提供任何支持,你可以!Rails博客在发布后保存为'nil'

谢谢

#应用程序/控制器/ 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 

轨控制台

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

你应该传递post_paramsPost.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