【发布时间】: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