控制器无法在erb文件中呈现

时间:2018-02-14 07:20:55

标签: ruby-on-rails

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

然而,没有任何事情发生,ruby不会显示在控制器的任何标签中。它只是空洞的。我已经关注了我的网页+ 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

3 个答案:

答案 0 :(得分:0)

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

resources :posts

答案 1 :(得分:0)

SELECT i.id , i.mon , i.item , SUM(v.value) totalValue FROM items i LEFT JOIN values v ON v.item_id = i.id WHERE mon = 'Y' GROUP BY i.id, i.mon , i.item ORDER BY SUM( v.value) DESC LIMIT 1; 表中似乎没有数据。只需按照以下步骤检查并创建记录即可。

  1. 使用命令Post转到rails控制台。

  2. 使用查询检查记录计数:rails c

  3. 如果它返回计数Post.all.count,那么您应首先使用查询创建记录:0

答案 2 :(得分:0)

在控制器的创建操作中,您正在执行:

from bs4 import BeautifulSoup

textToParse = """
<html xmlns:o='dummy_url'>
  <head>
    <title>Something to parse</title>
  </head>
  <body>
    <p><o:p>This should go</o:p>Paragraph</p>
  </body>
</html>
"""

soup = BeautifulSoup(textToParse, "lxml-xml")

body = soup.find('body')

print ('this should find nothing')
for otag in body.find_all('o'):
  print(otag)

print ('this should find o:p')
for otag in body.find_all('o:p'):
  print(otag)

>>>
this should find nothing
this should find o:p
<o:p>This should go</o:p>

你不应该将post_params传递给save方法,而是传递给new或create。帖子正在创建,但标题和正文未被保存。

尝试改为:

if @post.save(post_params)