2017-03-31 4 views
0

私は初心者です。ヘッダー画像、タイトル、著者、抜粋、本文のブログ投稿を作成していました。ブログ投稿は正しく作成されたようですが、ブログ投稿は表示されません。私は、レールのコンソールをチェックしたときにブログの投稿が表示されますが、すべてのためにnilとして保存されます。できる限りのサポートを貸してください!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 

あなたはどんな提案を持っているか、あなたはこの問題を解決するためのより多くの情報が必要な場合は私に知らせてください場合。

は、あなたが代わりにPost#saveのコントローラ内でPost.newpost_paramsを渡すべきである、

答えて

1

をありがとう、あなたのアクションを作成し、次のようになります。

# 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 
関連する問題