【发布时间】:2014-03-02 03:37:10
【问题描述】:
我有一个应该发送 POST 请求的 Rails 应用程序,但由于某种原因正在发送 GET。
查看:
<% if @competition.users.exclude?(@user) %>
<%= link_to 'Attend Competition', attend_competition_path(@competition.id), :method => :post %>
<% else %>
<%= link_to 'Withdraw', withdraw_competition_path(@competition.id), :method => :post %>
<% end %>
控制器:
def attend
p current_user.daily
@competition = Competition.find(params[:id])
if @competition.users.include?(current_user)
flash[:error] = "You're already attending this competition."
elsif current_user.daily == []
flash[:error] = "You must have a working device to compete."
else
current_user.competitions << @competition
flash[:success] = "Attending competition!"
end
redirect_to @competition
end
def withdraw
p "WITHDRAWING"
@competition = Competition.find(params[:id])
p @competition
attendee = Attendee.find_by_user_id_and_competition_id(current_user.id, @competition.id)
if attendee.blank?
flash[:error] = "No current attendees"
else
attendee.delete
flash[:success] = 'You are no longer attending this competition.'
end
p attendee
redirect_to @competition
end
路线:
resources :competitions do
post 'attend', on: :member
end
resources :competitions do
member do
post 'withdraw'
end
end
所以我单击按钮并转到页面,但收到一个错误,即没有 GET 请求的路由。不应该有 get 请求的路由,但应该是发送帖子。
ActionController::RoutingError (No route matches [GET] "/competitions/1/withdraw")
【问题讨论】:
-
您的浏览器是否禁用了javascript?
-
来自 rails 的 link_to 文档:
Note that if the user has JavaScript disabled, the request will fall back to using GET -
我需要启用哪些 javascript?我有 Jquery,但我需要 Jquery-ujs 还是 Jquery-ui
标签: ruby-on-rails ruby-on-rails-3