0

我刚刚创建了一个评论模型,由于某种原因,我提交的歌曲不再显示在歌曲#index 中。

简而言之,最初我只有一个带有典型 CRUD 功能的 song_controller。当我创建一首新歌曲时,它将被发布并显示在索引中。然而,在创建评论模型之后,歌曲不再被显示(或者甚至可能完全被创建)。

我在下面的要点中附上了各个页面。我会根据要求用任何额外的页面更新这篇文章。

https://gist.github.com/Apane/affe60c5b9d0d33cbaf8

评论.rb

class Comment < ActiveRecord::Base
  belongs_to :song
  belongs_to :user
  validates_presence_of :author_name, :content
end

歌曲.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

用户.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
4

1 回答 1

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]

使用设计你可以使用内置的控制器过滤器:

before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy]

无论如何,问题出在您构建的过滤器上

于 2013-07-13T07:28:17.027 回答