我刚创建了一个评论模型,由于某些原因,我的歌曲提交不再显示在歌曲#index中。
简而言之,最初我只有一个具有典型CRUD功能的songs_controller。当我创建一首新歌时,它将被发布并显示在索引中。但是,在创建评论模型后,歌曲不再显示(或者甚至可能完全创建)。
我已将各种页面附在下面的要点中。我会根据要求更新此帖子以及任何其他页面。
https://gist.github.com/Apane/affe60c5b9d0d33cbaf8
comment.rb
class Comment < ActiveRecord::Base
belongs_to :song
belongs_to :user
validates_presence_of :author_name, :content
end
song.rb
class Song < ActiveRecord::Base
has_many :comments, :dependent => :destroy
belongs_to :user
has_attached_file :track,
:url => "/assets/songs/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/songs/:id/:style/:basename.:extension"
validates_attachment :track, :presence => true
validates :title, length: { minimum: 10 }
validates :bio, length: { maximum: 300 }
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :songs
end
答案 0 :(得分:2)
在控制台中添加新歌时,您有:
[2013-07-13T10:07:00.739137 #11358] INFO -- : Started POST "/songs" for 127.0.0.1 at 2013-07-13 10:07:00 +0300
[2013-07-13T10:07:00.739187 #11358] INFO -- : Started POST "/songs" for 127.0.0.1 at 2013-07-13 10:07:00 +0300
[2013-07-13T10:07:00.749151 #11358] INFO -- : Started GET "/users/sign_in" for 127.0.0.1 at 2013-07-13 10:07:00 +0300
[2013-07-13T10:07:00.749230 #11358] INFO -- : Started GET "/users/sign_in" for 127.0.0.1 at 2013-07-13 10:07:00 +0300
这是因为您使用的是before_filter :authorize, only: [:create ,:edit, :update, :destroy]
使用Devise,您可以使用内置过滤器作为控制器:
before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy]
无论如何,问题在于您构建的过滤器