【发布时间】:2014-06-05 18:06:51
【问题描述】:
我最近在 heroku 中上传了我的 Rails App。我有 3 个模型 -
- 用户
- 发布
- 区
我执行了将应用程序部署到 heroku 所需的步骤。我的设计用户模型和地区模型 CRUD 工作正常。所以我想我在heroku中的数据库迁移工作得很好。但是当我尝试在 heroku 中写一篇新文章时,它给了我错误“我们很抱歉,但出了点问题。”。我可以在本地主机上写新帖子而不会出现任何错误。
posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:show, :index]
# GET /posts
# GET /posts.json
def index
if params[:tag]
@posts = Post.tagged_with(params[:tag])
else
@posts = Post.all
end
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :tag_list, :location, :body, :user_id, :published)
end
end
_form.html.erb
<%= form_for(@post) 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 |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :user_id, value: current_user.id %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :tag_list, 'Insert Tags' %><br>
<%= f.text_field :tag_list %>
</div>
<div class="field">
<%= f.label :location %><br>
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :published %><br>
<%= f.check_box :published %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
【问题讨论】:
-
我刚刚测试了应用程序,可以确认错误。您的代码看起来不错 - 那么问题是您有我们可以看到的任何日志吗?您可以通过在控制台中输入
heroku logs --trace或安装 LogEntries 插件来完成此操作 -
"heroku logs --trace" 给了我错误'!无效参数:"--trace"'。所以我只运行“heroku 日志”并想出了这个日志pastebin.com/raw.php?i=xbBkK934。
-
Sorrrry - 我还是有点困!
-
谢谢,我想那太好了!
-
没问题 - 让我为你写吧!
标签: ruby-on-rails heroku