简体   繁体   中英

Rails Ajax Request Controller Action not working

I've been battling this for a while now and can't figure out what I'm doing wrong. I'm just trying to send a simple ajax request to get details for a modal.

Ajax Call:

$('.invite-button').on('click', function() {
  var path = $(this).data('path');
  $.get(path).success( function(response) {
    console.log(response);
  });
});

Controller Action:

def invite_details
    @user = User.find(params[:id])
    @campaigns = current_user.campaigns
    respond_to do |format|
      format.js {}
    end
end

invite_details.js.erb :

$('.invite-modal').html("<%= j render partial: 'users/invite', locals: {campaigns: @campaigns} %>")

_invite.html.erb :

<h1> Hello! </h1>
<ul>
  <% campaigns.each do |campaign| %>
    <li><%= campaign.title %></li>
  <% end %>
</ul>

Server Logs:

Started GET "/users/8/invite_details" for 127.0.0.1 at 2014-09-30 10:06:43 -0400
Processing by UsersController#invite_details as */*
  Parameters: {"id"=>"8"}
  User Load (0.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1 LIMIT 1
  Transaction Load (0.6ms)  SELECT "transactions".* FROM "transactions"  WHERE "transactions"."brand_id" = 1
  Rendered users/invite_details.js.erb (0.1ms)
  SQL (2.4ms)  UPDATE "users" SET "last_activity_at" = '2014-09-30 14:06:43.484838' WHERE "users"."id" = 1
Completed 200 OK in 14ms (Views: 5.4ms | ActiveRecord: 3.7ms)

Everything in the logs looks fine except that it's not actually querying the database for @user or @campaigns, and they're empty in the layouts. I've also tried putting a binding.pry into the controller action and it doesn't get run. Any ideas?

Thanks!

Is this s typo just here or on the actual code too. The code within li tags in _invite.html.erb is not wrapped properly. It needs to be this:

<h1> Hello! </h1>
<ul>
  <% campaigns.each do |campaign| %>
    <li><%= campaign.title %></li>
  <% end %>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM