2
我有一個類似reddit的應用程序,允許用戶投票與Activerecord Reputation System gem鏈接。Javascript方法在移動格式下不工作(既不是JS也不是HTML)
我想創建一個基於Railscast#199的移動版本,如果通過移動設備訪問站點,則會使用mobile.erb
格式。
投票方法在非移動格式下工作正常(默認爲javascript,但也適用於html),但在移動設備中返回錯誤,如下所示。請注意,它正在嘗試Get
而不是Post
。
Started GET "/links/98/vote?type=like" for 127.0.0.1 at 2012-12-25 19:16:45 -0500
ActionController::RoutingError (No route matches [GET] "/links/98/vote"):
什麼可能導致這種情況? before_filter
是否阻止了javascript?我怎樣才能解決這個問題?非常感謝您的意見!
application_controller.rb
before_filter :prepare_for_mobile
def prepare_for_mobile
session[:mobile_param] = params[:mobile] if params[:mobile]
request.format = :mobile if mobile_device?
end
def mobile_device?
if session[:mobile_param]
session[:mobile_param] == "1"
else
request.user_agent =~ /Mobile/
end
end
helper_method :mobile_device?
的routes.rb
resources :links do
member { post :vote }
end
links_controller.rb
def vote
value = params[:type] == "like" ? 1 : 0
@link = Link.find(params[:id])
@link.add_or_update_evaluation(:link_votes, value, current_user)
respond_to do |format|
format.html { redirect_to :back }
format.mobile { redirect_to :back }
format.js
end
end
_link.mobile.erb
<div id="link_<%= @link.id %>">
<%= link_to "like", vote_link_path(@link, type: "like"), method: "post", remote: true %>
</div>
<div id="link_points_<%= @link.id %>">
<%= @link.reputation_for(:link_votes).to_i %>
</div>
vote.js.erb
$('#link_<%= @link.id %>').html("<%= j link_to "up", vote_link_path(@link, type: "like"), method: "post", remote: true %>");
$('#link_points_<%= @link.id %>').html("<%= @link.reputation_for(:link_votes).to_i %>;