我是 ROR 的新手,我正在努力通过浏览所有 stackoverflow 和 github 页面来解决这个问题,我保证我已经浏览了所有这些页面,但仍然找不到解决我的问题的方法。我希望你们能帮助我。
问题是,在我实现 thumbs_up gem 之后,我在这里按照 Brady 的说明进行操作: Clarification on how to use "thumbs_up" vote gem with Rails 3
但是我的查看页面会有错误信息:
No route matches {:action=>"vote_up", :controller=>"posts", :id=>nil}
我检查了 rake 路线,并且 vote_up_post 路径在那里,所以我试着去
http://0.0.0.0:3000/posts/vote_up and this shows up:
ActiveRecord::RecordNotFound in PostsController#show
Couldn't find Post with id=vote_up
这是我的观点 - index.html.erb
<%= link_to('vote for this post!', vote_up_post_path(@post), :method => :post) %>
post_controller.rb
def vote_up
begin
current_user.vote_for(@post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
模型 - user.rb
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
acts_as_voter
模型 - post.rb
attr_accessible :title, :source, :description, :imageurl, :link
validates :title, presence: true, uniqueness:true, length: { maximum: 70 }
validates :source, presence: true, length: { maximum: 20 }
validates :description, presence: true, length: { maximum: 240 }
validates :imageurl, presence: true
validates :link, presence: true
default_scope order: 'posts.created_at DESC'
acts_as_voteable
路线.rb
devise_for :users
resources :posts do
member do
post :vote_up
end
end
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
get "sign_out", :to => "devise/sessions#destroy"
get "sign_up", :to => "devise/registrations#new"
end
resources :submissions
match '/submit', to: 'submissions#new'
match '/post', to: 'posts#new'
抱歉问了这么愚蠢的问题,非常感谢你们的帮助。