0
我有一個應該發送POST請求的Rails應用程序,但由於某些原因正在發送GET。Rails的發送GET請求時,它應該是POST
查看:
<% 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請求的路由。不應該有獲取請求的路由,但應該發送帖子。
ActionController::RoutingError (No route matches [GET] "/competitions/1/withdraw")
您的瀏覽器中禁用JavaScript腳本嗎? – usha
從rails文檔link_to:'請注意,如果用戶禁用JavaScript,請求將回退到使用GET' – usha
我需要啓用什麼JavaScript?我有Jquery,但是我需要Jquery-ujs還是Jquery-ui – Marcus