I am following as my select tag which triggers the ajax call:
= select('project', 'sort', sort_type, {prompt: 'Select method'})
#sorted{ style: 'display: none' }
#normal
= render 'projects_list'
my javascript, where #project_sort is id of select tag:
$("document").ready(function() {
$("#project_sort").change(function() {
var state = $('select#project_sort :selected').val();
jQuery.getJSON('/inspire/' + state+ '.js');
});
})
my routes:
get '/inspire/:sort' => 'projects#index'
in my controller:
def index
if params[:sort] != nil
@projects = [Project.first]
else
@projects = Project.all
end
respond_to do |format|
format.html
format.js { render 'populate_projects' }
end
end
populate_projects.js.haml file:
:plain
$("#sorted").html("#{escape_javascript render(partial: 'projects/projects_list')}");
$("#normal").hide();
$("#sorted").show();
and _projects_list.html.haml:
%section.album
%div
- @projects.each do |project|
%div
%p
# more code...
What should happen is when state of select tag is changed, an ajax call should be made and view must change from all projects to just the first but it doesn't. In my terminal I can see that call is made when state change, only first project is selected and populate_project.js.haml is rendered but I don't see any change in view of my broswer.
EDIT - console:
Started GET "/inspire/stars.js" for 127.0.0.1 at 2015-05-18 22:18:16 +0530
Processing by ProjectsController#index as JS
Parameters: {"sort"=>"stars"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Project Load (0.3ms) SELECT "projects".* FROM "projects" WHERE "projects"."deleted_at" IS NULL ORDER BY "projects"."id" ASC LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered projects/_forked_from.html.haml (0.2ms)
Rate Load (0.2ms) SELECT "rates".* FROM "rates" WHERE "rates"."rateable_id" = ? AND "rates"."rateable_type" = ? AND "rates"."dimension" = 'stars' AND "rates"."rater_id" = 1 [["rateable_id", 1], ["rateable_type", "Project"]]
RatingCache Load (0.1ms) SELECT "rating_caches".* FROM "rating_caches" WHERE "rating_caches"."cacheable_id" = ? AND "rating_caches"."cacheable_type" = ? AND "rating_caches"."dimension" = 'stars' LIMIT 1 [["cacheable_id", 1], ["cacheable_type", "Project"]]
Rendered projects/_projects_list.html.haml (22.9ms)
Rendered projects/populate_projects.js.haml (24.5ms)
Completed 200 OK in 37ms (Views: 29.0ms | ActiveRecord: 1.3ms)