Reputation: 26131
I have a application that allows user to download the latest builds of an application. I need to record whenever a user clicks on a link. I have create a UserVersions model that tracks this data. But I'm not sure how to make the call, via javascript or some rails utility to create a new record. I have an idea of how to do it using jquery. But I'm not sure how to get the version_id for each link.
config/routes.rb
post 'user_versions/create/:user_id/:version_id', to: 'user_versions/#create'
app/controllers/user_versions_controller.rb
class UserVersionsController < ApplicationController
def create
user = params[:user_id]
version = params[:version_id]
UserVersion.find_or_create_by(user: user, version: version)
end
end
app/view/version/_full.html.erb
...
<li class="download cols-lg-4 pull-right">
<% if (version.app[:app_type] == 'ios') %>
<%= button_to 'Download', "itms-services://?action=download-manifest&url=#{CGI::escape version.plist_url}", class:"btn-primary btn-small pull-right" %>
<% else %>
<%= button_to 'Download', version.plist_url, class: 'download-btn btn-primary btn-small pull-right', method: :get %>
<% end %>
</li>
...
app.js.erb
...
$('.download-btn').on('click', function(){
$.ajax({
type: 'POST',
url: "/user_versions/create/" + current_user[:id] +"/" + :version_id"
});
});
Upvotes: 0
Views: 72
Reputation: 1937
Have the link go to a URL controlled by your rails app. Then when the request arrives at your controller you can record the hit then redirect to the actual download URL.
Upvotes: 1