I have a reddit-like app that allows users to vote for links with the Activerecord Reputation System gem.
I am trying to create a mobile version, based on Railscast#199, in which a mobile.erb format is used if the site is accessed via a mobile device.
The voting method works fine in the non-mobile format (defaults to javascript, but also works in html), but returns an error in mobile as follows. Note that it is trying to Get instead of 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"):
What might be causing this? Is the before_filter preventing the javascript? How can I fix this? Thanks much for your input!
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 %>;
Here’s what I did:
config/initializers/mime_types.rb
application_controller.rb