[英]How can I test which page sent an AJAX request in my create.js.erb file?
I have two different pages in my app that call an AJAX request to add a new phone_number ( to a key_contact
). 我的应用程序中有两个不同的页面,它们调用AJAX请求来添加新的phone_number( 到
key_contact
)。 These are the companies page ( key_contact belongs_to company
) and the sales_opportunity page ( where the sales_opportunity has_many key_contacts through sale_contacts ). 这些是“公司”页面(
key_contact belongs_to company
)和sales_opportunity页面( 其中sales_opportunity具有通过sale_contacts的许多key_contacts )。 Both pages have a table of key_contacts
that I want to reload once the phone_number is added. 两个页面都有一张
key_contacts
表,添加了phone_number后,我想重新加载它们。 I want to use the create.js.erb
file to reload ONLY the table that should be on the page ( the table on sales_opportunities is different to the one on the companies page ). 我想使用
create.js.erb
文件仅重新加载该页面上的表(sales_opportunities上的表与companys页面上的表不同 )。 As such I need to test the URL of the page that sends the AJAX request and then render a partial accordingly. 因此,我需要测试发送AJAX请求的页面的URL,然后相应地呈现部分。 I've tried the below, but it isn't working.
我已经尝试了以下方法,但无法正常工作。 Can anyone help me work out why?
谁能帮我找出原因?
//update the key contact table with the new phone number, and close the modal
$('#phone_number_modal').modal('hide')
.clear_previous_errors();
resetForm($('#new_phone_number'));
$input = $('#phone_number_error');
$input.closest('.form-group').removeClass('has-error').find('.warning-block').html('');
//render the newly added key contact to the table if the AJAX call succeeded
<% if (URI(request.referer).path == '/companies') %>
$('#key-contacts-table-div').html("<%= escape_javascript(render :partial => 'shared/key_contacts_table')%>");
<% end %>
<% if (URI(request.referer).path == '/sales_opportunities') %>
$('#sale-contacts-table-div').html("<%= escape_javascript(render :partial => 'sale_contacts/table')%>");
<% end %>
$('.alert').remove();
$('#key-contacts').DataTable({
retrieve: true,
});
Instead of using referer
, you can just add an extra params
in your url, let's say you call it :action_type
. 除了使用
referer
,您还可以在URL中添加一个额外的params
,假设您将其称为:action_type
。 Then in your controller, you can do: 然后在您的控制器中,您可以执行以下操作:
class AAAController < ActionController::Base
def your_action
if params[:action_type] == "companies"
render "create_1"
elsif params[:action_type] == "sales_opportunities"
render "create_2"
end
end
end
Then you can put different logic in create_1.js.erb
or create_2.js.erb
separately. 然后,您可以将不同的逻辑分别放在
create_1.js.erb
或create_2.js.erb
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.