Home Controller not rendering in erb file
Reply: 3

Controller not rendering in erb file

user3349825
1#
user3349825 Published in 2018-02-14 07:20:55Z

I've set up a post controller for a simple blog.

However nothing is happening, the ruby isn't displaying in any of the tags from the controller. It's just coming up as empty. I've attatched my pages + posts_controller.rb page I've followed the instructions from this tutorial https://scotch.io/tutorials/build-a-blog-with-ruby-on-rails-part-1

I'm sure im just missing something small, just starting out on rails!

show.html.erb

<div class="col-sm-10 col-md-8">
   <h2><%= @post.title %></h2>
   <p class="lead"> <%= raw @post.body %>

index.html.erb

<div class="container">
  <div class="col-sm-10 col-sm-offset-1 col-xs-12 blog-content">
    <% @posts.each do |post| %>
    <div class="col-xs-12">
      <div class="text-center">
        <h2><%= post.title %></h2>
        <h6></h6>
      </div>

      <div>
        <%= raw(post.body).truncate(358) %>
      </div>

      <div class="text-center">
        <%= link_to "READ MORE", post_path(post) %>
      </div>
      <br>
    </div>
    <% end %>
  </div>
</div>

Posts Controller:

    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
  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, :body)
  end

  def find_post
    @post = Post.find(params[:id])
  end
end
7urkm3n
2#
7urkm3n Reply to 2018-02-14 08:05:09Z

You are missing routing, add into config/routes.rb file.

resources :posts
Shrikant Banke
3#
Shrikant Banke Reply to 2018-02-14 18:24:19Z

Seems there is no data in your Post table. Just follow below steps to check and create a record.

  1. Go to rails console by using command: rails c.

  2. Check the record count by using query: Post.all.count

  3. If it returns count 0 then you should create record first by using the query: Post.create(title: "Sample title" , body: "Sample Body")

Evekay
4#
Evekay Reply to 2018-02-15 00:00:11Z

In the create action in the controller, you are doing:

if @post.save(post_params)

you should not pass post_params to save method, but to new or create. The posts are getting created, but the title and the body are not being saved.

Try instead:

def create
  @post = Post.new(post_params)
  if @post.save
  ...
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313988 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO