【问题标题】:Controller not rendering in erb file控制器未在 erb 文件中呈现
【发布时间】:2018-02-14 07:20:55
【问题描述】:

我已经为一个简单的博客设置了一个帖子控制器。

但是没有发生任何事情,红宝石没有显示在控制器的任何标签中。它只是空的。我已经附加了我的页面 + posts_controller.rb 页面我已经按照本教程中的说明进行操作 https://scotch.io/tutorials/build-a-blog-with-ruby-on-rails-part-1

我确定我只是错过了一些小东西,刚刚起步!

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>

帖子控制器:

    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

【问题讨论】:

  • 您是否已经通过rails c 检查您的posts 表中是否保存了记录?

标签: ruby-on-rails


【解决方案1】:

你缺少路由,添加到 config/routes.rb 文件中。

resources :posts

【讨论】:

  • 你怎么知道的?如果他没有那条路线,它将显示错误。你可以在他的帖子However nothing is happening, the ruby isn't displaying in any of the tags from the controller. It's just coming up as empty. 中注意到他没有收到错误,对象没有显示。
  • 感谢@araratan,这是真的。我已将资源 :posts 包含在我的路线中
【解决方案2】:

您的Post 表中似乎没有数据。只需按照以下步骤检查并创建记录。

  1. 使用命令转到 Rails 控制台:rails c

  2. 使用查询检查记录数:Post.all.count

  3. 如果返回计数0,那么您应该首先使用查询创建记录:Post.create(title: "Sample title" , body: "Sample Body")

【讨论】:

  • 是的,我做了这个,帖子肯定会保存到表格中。即使在索引页面上,也有 4 个“阅读更多”链接。表示已保存。只是表单的实际输入没有显示
【解决方案3】:

在控制器的创建动作中,你正在做:

if @post.save(post_params)

您不应将 post_params 传递给 save 方法,而应传递给 new 或 create。正在创建帖子,但没有保存标题和正文。

试试吧:

def create
  @post = Post.new(post_params)
  if @post.save
  ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-28
    • 2012-07-02
    • 2013-08-19
    • 2011-12-18
    • 2018-07-10
    • 2018-12-27
    • 1970-01-01
    • 2018-09-07
    相关资源
    最近更新 更多