我是 Rails 新手,并已按照这个问题的答案。
这就是我的项目中的情况:
控制器:
def create
def create
current_user.likes.create(:post_id => params[:post_id])
render :layout => false
end
end
.js 文件:
$ ->
$(".like").click ->
post_id = $(this).attr('id')
$.ajax
type: 'POST'
url: 'likes/' + post_id
success: ->
alert "succsess!"
路由.rb
Sample::Application.routes.draw do
resources :likes
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :posts, only: [:create, :destroy]
root to: 'pages#home'
match '/about', to: 'pages#about'
match '/contact', to: 'pages#contact'
match '/help', to: 'pages#help'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy'
post '/likes/4', to: 'likes#create', :as => :like
end
(我使用 '/likes/4' 来测试提议(将来会是 'post_id'))。
当我单击视图中的 Like 按钮时,like 存储在数据库中,但我收到此错误(当我在 Chrome 中使用检查器查看控制台时)...
POST http://0.0.0.0:3000/likes/4 500 (Internal Server Error)
...而且我从来没有从 ajax 收到成功警报。
当我运行时wget --post-data='' http://localhost:3000/likes/4
,我得到:
--2012-07-22 19:35:01-- http://localhost:3000/likes/4
Resolving localhost... ::1, 127.0.0.1, fe80::1
Connecting to localhost|::1|:3000... failed: Connection refused.
Connecting to localhost|127.0.0.1|:3000... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2012-07-22 19:35:02 ERROR 500: Internal Server Error.
有谁知道是什么导致了这个错误?