Reputation: 160
I have tried all my googling skills , but i am not able to figure out the solution to my problem yet.
I am trying to send parameter to controller method from jquery using ajax. Once method is called, it will render its respective view page. But its not even entering into the ajax code to call the controller method(By inspect). I guess i am doing something really silly , but unable to figure out, Please help me out.
My Controller Method code:
def profile
@prof = User.where(:id => params[:id])
end
My .js file
$(document).ready(function(){
$('.table tr').click(function(){
$.ajax({
url: "users/profile",
type: "POST",
data: {id: $(this).attr('id')},
success: function (data) {
alert(data);
}
});
});
});
My routes file:
resources :users, :only => [:new, :create, :index,:profile]
match 'users/profile' => 'users#profile', :as => :profile
Upvotes: 0
Views: 948
Reputation: 907
Try this in your routes.rb
resources :users, :only => [:new, :create, :index]
get 'users/profile/:id' => 'users#profile', :as => :profile
And change your js method:
$(document).ready(function(){
$('.table tr').click(function(){
$.ajax({
url: "/users/profile/" + $(this).attr('id'),
type: "GET",
success: function (data) {
alert(data);
}
});
});
});
And your method should serialize to json, like this:
def profile
@prof = User.where(:id => params[:id])
respond_to do |format|
format.json { render :json => @proof }
end
end
Upvotes: 1
Reputation: 2786
Change your js method:
$(document).ready(function(){
$('.tr_class').click(function(){
$.ajax({
url: "/users/profile?id="+$(this).attr('id'),
type: "GET",
success: function (data) {
alert(data);
}
});
});
});
And your method should serialize to json, like this:
def profile
@prof = User.where(:id => params[:id])
respond_to do |format|
format.json { render :json => @proof }
end
end
Upvotes: 0