我有一个在本地运行SQLite的Rails 4应用程序和在Heroku上运行PostgreSQL。
我遇到了PostController的索引操作问题。我得到了上述错误,但仅限于Heroku。
我仔细检查了所有代码并运行所有迁移并重新启动了Heroku服务器。我还仔细检查了本地和Heroku上的迁移是否相同。该问题似乎与通过Post模型访问User模型中的数据特别相关。例如,我试图通过Heroku上的rails控制台访问其他属性,例如电子邮件,但我得到了相同的未定义方法错误。所以我很确定它与表连接有关。
另外澄清一下,我在生产数据库中有数据
这是我的PostsController行动:
class PostsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
end
我的帖子模型:
class Post < ActiveRecord::Base
belongs_to :user
end
用户模型中的代码:
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
belongs_to :qualification
has_many :posts
validates :phone, presence: true, format: { with: /\d{3}.\d{3}.\d{4}/, message: "Must be in 555.555.5555 format" }
validates :name, presence: true
validates :email, presence: true
validates :acknowledgement, presence: true
# new function to set the password without knowing the current password used in our confirmation controller.
def attempt_set_password(params)
p = {}
p[:password] = params[:password]
p[:password_confirmation] = params[:password_confirmation]
update_attributes(p)
end
# new function to return whether a password has been set
def has_no_password?
self.encrypted_password.blank?
end
# new function to provide access to protected method unless_confirmed
def only_if_unconfirmed
pending_any_confirmation {yield}
end
def password_required?
# Password is required if it is being set, but not for new records
if !persisted?
false
else
!password.nil? || !password_confirmation.nil?
end
end
end
我的index.html.erb文件:
<% @posts.each do |post| %>
<h2 class='subheader'><%= link_to post.title, post %></h2>
<h6>written by <%= post.user.name %> on <%= post.created_at.to_s(:long) %></h6>
<div><%= post.body.html_safe %></div>
<% if user_signed_in? %>
<% if current_user.has_role? :admin %>
<div><%= link_to 'Edit', edit_post_path(post) %></div>
<div><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %> </div>
<% end %>
<% end %>
<% end %>
<br>
<%= link_to 'New Post', new_post_path %>
我的Heroku日志的输出:
2013-12-19T06:31:13.280899+00:00 app[web.1]: Started GET "/posts" for 64.114.175.126 at 2013-12-19 06:31:13 +0000
2013-12-19T06:31:13.280899+00:00 app[web.1]: Started GET "/posts" for 64.114.175.126 at 2013-12-19 06:31:13 +0000
2013-12-19T06:31:13.283663+00:00 app[web.1]: Processing by PostsController#index as HTML
2013-12-19T06:31:13.283663+00:00 app[web.1]: Processing by PostsController#index as HTML
2013-12-19T06:31:13.292344+00:00 app[web.1]: Rendered posts/index.html.erb within layouts/application (6.9ms)
2013-12-19T06:31:13.292553+00:00 app[web.1]: Completed 500 Internal Server Error in 9ms
2013-12-19T06:31:13.292344+00:00 app[web.1]: Rendered posts/index.html.erb within layouts/application (6.9ms)
2013-12-19T06:31:13.292553+00:00 app[web.1]: Completed 500 Internal Server Error in 9ms
2013-12-19T06:31:13.295581+00:00 app[web.1]:
2013-12-19T06:31:13.295581+00:00 app[web.1]: 6: <div><%= post.body.html_safe %></div>
2013-12-19T06:31:13.295581+00:00 app[web.1]: 3: <% @posts.each do |post| %>
2013-12-19T06:31:13.295581+00:00 app[web.1]: 7: <% if user_signed_in? %>
2013-12-19T06:31:13.295581+00:00 app[web.1]: 2:
2013-12-19T06:31:13.295581+00:00 app[web.1]: 4: <h2 class='subheader'><%= link_to post.title, post %></h2>
2013-12-19T06:31:13.295581+00:00 app[web.1]: ActionView::Template::Error (undefined method `name' for nil:NilClass):
2013-12-19T06:31:13.295581+00:00 app[web.1]: 5: <h6>written by <%= post.user.name %> on <%= post.created_at.to_s(:long) %></h6>
2013-12-19T06:31:13.295778+00:00 app[web.1]: app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb__338969566965495969_70155247178440'
2013-12-19T06:31:13.295778+00:00 app[web.1]: 2:
2013-12-19T06:31:13.295778+00:00 app[web.1]:
2013-12-19T06:31:13.295778+00:00 app[web.1]: ActionView::Template::Error (undefined method `name' for nil:NilClass):
2013-12-19T06:31:13.295778+00:00 app[web.1]:
2013-12-19T06:31:13.295778+00:00 app[web.1]: 3: <% @posts.each do |post| %>
2013-12-19T06:31:13.295778+00:00 app[web.1]:
2013-12-19T06:31:13.295581+00:00 app[web.1]: 8: <% if current_user.has_role? :admin %>
2013-12-19T06:31:13.295581+00:00 app[web.1]: app/views/posts/index.html.erb:5:in `block in _app_views_posts_index_html_erb__338969566965495969_70155247178440'
2013-12-19T06:31:13.295778+00:00 app[web.1]: 5: <h6>written by <%= post.user.name %> on <%= post.created_at.to_s(:long) %></h6>
2013-12-19T06:31:13.295778+00:00 app[web.1]: 6: <div><%= post.body.html_safe %></div>
2013-12-19T06:31:13.295947+00:00 app[web.1]: 7: <% if user_signed_in? %>
2013-12-19T06:31:13.295947+00:00 app[web.1]: 8: <% if current_user.has_role? :admin %>
2013-12-19T06:31:13.295947+00:00 app[web.1]: app/views/posts/index.html.erb:5:in `block in _app_views_posts_index_html_erb__338969566965495969_70155247178440'
2013-12-19T06:31:13.295947+00:00 app[web.1]: app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb__338969566965495969_70155247178440'
2013-12-19T06:31:13.295778+00:00 app[web.1]: 4: <h2 class='subheader'><%= link_to post.title, post %></h2>
2013-12-19T06:31:13.295947+00:00 app[web.1]:
2013-12-19T06:31:13.295947+00:00 app[web.1]:
2013-12-19T06:31:13.303637+00:00 heroku[router]: at=info method=GET path=/posts host=www.kettlecreekventure.com fwd="64.114.175.126" dyno=web.1 connect=8ms service=25ms status=500 bytes=1266
2013-12-19T06:31:14.630725+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=www.kettlecreekventure.com fwd="64.114.175.126" dyno=web.1 connect=1ms service=4ms status=200 bytes=0
2013-12-19T06:31:22.445573+00:00 heroku[run.7634]: Process exited with status 0
2013-12-19T06:31:22.459444+00:00 heroku[run.7634]: State changed from up to complete
同样,代码在我的本地计算机上运行完美,但在部署到Heroku后失败并出现上述错误。当应用程序尝试引用post.user.name时,index.html.erb文件中存在错误。我把头发拉出来试图搞清楚。 如果有人能看到我的问题所在,我将不胜感激。我做错了什么?
答案 0 :(得分:1)
根据您的更新,我会建议:每次删除有帖子的用户,然后尝试列出他们的帖子<%= post.user.name %>
将返回nil,因为此帖子不再有用户可以参考。你可以使用
has_many :posts, dependent: :destroy
如上所述,在用户模型中,或者为删除用户添加了一些功能,以防止在尝试查看帖子时出错。一些选择是:
当用户被删除时,您可以删除除用户表格行中的名称和ID之外的所有内容,这样您就可以<%= post.user.name %>
返回一个值。
当用户被删除时,您可以删除其用户表信息中的所有内容,并添加“-user-deleted”,类似于Tumblr在用户停用其帐户时所执行的操作,并且其他人的姓名显示在其他人的帖子中tumblr。
您可以在名为“用户删除”的数据库中创建用户,并让该用户继承已删除用户的每个帖子。在用户控制器中,在delete
操作中添加一些内容,将其帖子中的user_id字段更改为“用户删除”帐户。
或者最后你可以查看post.user是否返回一个值,如果不是只打印“系统中不再存在用户”这样的内容
如果你甚至想要删除用户但仍然保留他们的帖子,那么这完全取决于你。如果这不仅仅是一个类项目或者你只是为了学习Rails而做的事情,这肯定是一种可能的情况。希望这可以帮助。这当然让我想到了允许用户删除他们的个人资料而没有在他们之后清理的东西的缺陷。
答案 1 :(得分:0)
看起来我可能在Heroku DB中有一些损坏的数据。 我最终通过rails控制台删除了所有现有帖子。现在一切正常!
我跑了 @posts = Post.all @ posts.each do | post | post.destroy 端然后我继续前进并没有任何问题地输入新帖子。问题是我删除了一个用户,但没有删除相应的帖子