当从我的应用程序中的不同页面调用时,如何让rails控制器执行不同的javascript?(How can I get a rails controller to execute different

编程入门 行业动态 更新时间:2024-10-18 22:37:27
当从我的应用程序中的不同页面调用时,如何让rails控制器执行不同的javascript?(How can I get a rails controller to execute different javascript when called from different pages in my app?)

在我的应用程序中,我有sales_opportunities,这些属于两个用户(无论用户通常是哪个用户输入),还有公司(sales_opportunity正在销售的公司)。 我正在使用Bootstrap 3模式来捕获每个sales_opportunity的详细信息,使用AJAX / JS来更新数据库,然后使用新的详细信息在同一页面上重新呈现部分(例如,当用户通过以下内容添加新的sales_opportunity时) modal他的销售渠道被重新渲染以显示新增加的机会)。 到目前为止,这一切都很好。

我还想做的是为用户销售的系统中的公司提供一个页面。 此页面包含基本详细信息(例如公司名称以及是否为现有客户等)。 我也有一个类似的管道显示,所以用户可以看到他们与该特定公司有多少sales_opportunities,我想重新使用上面的模式,使用户能够直接从公司页面添加新的sales_opportunity,然后呈现该页面的一些其他部分,包含更新的信息。 这是我遇到问题的地方,因为我的sales_opportunity控制器尝试从sales_opportunity文件渲染create.js,它似乎执行该文件中的任何js,无论我正在瞄准的页面上的目标div。 理想情况下,如果AJAX请求来自/ users页面,我可以调用一组js,如果它来自/ companies页面,我可以调用另一组js - 但是我无法提供这个。

另外,我需要以两种不同的方式定义@company,具体取决于是否通过用户页面添加sales_opportunity(当他们可以选择系统中的任何公司或通过AJAX添加新公司时)或通过公司页面(当它显然,只应将机会添加到您当前正在查看的任何公司)。

有些代码如下 - 如果您需要更多,请告诉我们:

销售机会控制器:

def create @sales_opportunity = SalesOpportunity.new(sales_opportunity_params) @pipeline_statuses = SalesOpportunity.pipeline_statuses @user = current_user @company = Company.new(organization_id: params[:organization_id]) respond_to do |format| if @sales_opportunity.save format.html { redirect_to @sales_opportunity.user, :flash => {:success => 'Sales opportunity was successfully created.'} } format.json { render :show, status: :created, location: @sales_opportunity } format.js else format.html { render :new } format.json { render json: @sales_opportunity.errors, status: :unprocessable_entity } format.js { render json: @sales_opportunity.errors, status: :unprocessable_entity } end end

结束

sales_opportunities视图文件夹中的create.js:

//code to work on the sales opportunity modal from the user view screen $('#sales_opportunity_modal').modal('hide') .clear_previous_errors(); resetForm($('#new_sales_opportunity')); $input = $('#sales_opportunity_error'); $input.closest('.form-group').removeClass('has-error').find('.warning-block').html(''); //render the newly added sales_opportunity only if the AJAX call succeeded $(document).ajaxSuccess(function() { $('#chevrons').html("<%= escape_javascript(render :partial => 'shared/users_chevron')%>"); $(document).chevron_js() $('#sales-opportunities-table-div').html("<%= escape_javascript(render :partial => 'shared/sales_opportunities_table')%>"); // $("#sales-opportunities").dataTable().fnDestroy(); $('#sales-opportunities').DataTable({ retrieve: true, }); }); //code to work on the sales opportunity modal from the company view screen $('#company_sales_opportunity_modal').modal('hide') .clear_previous_errors(); resetForm($('#new_sales_opportunity')); $input = $('#sales_opportunity_error'); $input.closest('.form-group').removeClass('has-error').find('.warning-block').html(''); //render the newly added sales opportunity only if the AJAX call succeeded $(document).ajaxSuccess(function() { $('#single_company').html("<%= escape_javascript(render :partial => 'companies/company_table')%>"); $('#chevrons').html("<%= escape_javascript(render :partial => 'shared/company_chevron')%>"); $(document).chevron_js() $('#sales-opportunities-table-div').html("<%= escape_javascript(render :partial => 'shared/company_sales_opportunities_table')%>"); // $("#sales-opportunities").dataTable().fnDestroy(); $('#sales-opportunities').DataTable({ retrieve: true, }); });

正在呈现的项目的详细信息可能无关紧要,但为了完整性,#chevrons id指的是sales_pipeline显示,#sales_opportunities_table_div只是所有sales_opportunities的爆炸视图,而#company_table只是一个行视图。公司(例如,名称,是否是现有客户,当前销售总额等)。 该公司表当前导致我的渲染过程失败,可能是因为sales_opportunities控制器认为@company是新公司而不是现有公司。

谁能帮我理解这个更好吗? 在我的想法中,我将定义@company并从用户页面呈现js(如果请求来自那里),然后如果AJAX请求来自公司页面则以不同方式定义。 然而,可能有更好的选择,我愿意接受任何建议。

谢谢!

In my app I have sales_opportunities, these belong_to both users (whichever user input them typically), and also to companies (whichever company the sales_opportunity is selling to). I'm using Bootstrap 3 modals in order to capture the details of each sales_opportunity, and AJAX/JS to update the DB and then re-render partials on the same page with the new details (e.g. when a user adds a new sales_opportunity via the modal his sales pipeline gets re-rendered to show the newly added opportunity). All of this works fine so far.

What I also want to do is to have a page for the Companies within the system that the user is selling to. This page has basic details (e.g. company name and whether it's an existing customer etc). I also have a similar pipeline display so the user can see how many sales_opportunities they have with that particular company, and I want to re-use the modal above to enable the user to directly add a new sales_opportunity from the companies page, and then render some additional parts of that page with the updated information. This is where I'm running into problems, because my sales_opportunity controller tries to render create.js from the sales_opportunity file, and it seems to execute any js within that file, regardless of the target divs on the page I'm aiming for. Ideally I would be able to call one set of js if the AJAX request comes from the /users page and another if it comes from the /companies page - but I've not been able to deliver this.

In addition, I need to define @company in 2 different ways depending on whether the sales_opportunity is added via a user page (when they can select any company in the system or add a new one via AJAX) or via the company page (when it should obviously only add the opportunity to whatever company you were currently viewing).

Some code is below - please let me know if you need more:

Sales Opportunities Controller:

def create @sales_opportunity = SalesOpportunity.new(sales_opportunity_params) @pipeline_statuses = SalesOpportunity.pipeline_statuses @user = current_user @company = Company.new(organization_id: params[:organization_id]) respond_to do |format| if @sales_opportunity.save format.html { redirect_to @sales_opportunity.user, :flash => {:success => 'Sales opportunity was successfully created.'} } format.json { render :show, status: :created, location: @sales_opportunity } format.js else format.html { render :new } format.json { render json: @sales_opportunity.errors, status: :unprocessable_entity } format.js { render json: @sales_opportunity.errors, status: :unprocessable_entity } end end

end

create.js from the sales_opportunities view folder:

//code to work on the sales opportunity modal from the user view screen $('#sales_opportunity_modal').modal('hide') .clear_previous_errors(); resetForm($('#new_sales_opportunity')); $input = $('#sales_opportunity_error'); $input.closest('.form-group').removeClass('has-error').find('.warning-block').html(''); //render the newly added sales_opportunity only if the AJAX call succeeded $(document).ajaxSuccess(function() { $('#chevrons').html("<%= escape_javascript(render :partial => 'shared/users_chevron')%>"); $(document).chevron_js() $('#sales-opportunities-table-div').html("<%= escape_javascript(render :partial => 'shared/sales_opportunities_table')%>"); // $("#sales-opportunities").dataTable().fnDestroy(); $('#sales-opportunities').DataTable({ retrieve: true, }); }); //code to work on the sales opportunity modal from the company view screen $('#company_sales_opportunity_modal').modal('hide') .clear_previous_errors(); resetForm($('#new_sales_opportunity')); $input = $('#sales_opportunity_error'); $input.closest('.form-group').removeClass('has-error').find('.warning-block').html(''); //render the newly added sales opportunity only if the AJAX call succeeded $(document).ajaxSuccess(function() { $('#single_company').html("<%= escape_javascript(render :partial => 'companies/company_table')%>"); $('#chevrons').html("<%= escape_javascript(render :partial => 'shared/company_chevron')%>"); $(document).chevron_js() $('#sales-opportunities-table-div').html("<%= escape_javascript(render :partial => 'shared/company_sales_opportunities_table')%>"); // $("#sales-opportunities").dataTable().fnDestroy(); $('#sales-opportunities').DataTable({ retrieve: true, }); });

The details of the items being rendered probably don't matter, but for completeness the #chevrons id refers to a sales_pipeline display, the #sales_opportunities_table_div is just the exploded view of all the sales_opportunities and the #company_table is just the one line view of the company (e.g. name, whether it's an existing customer, current totals for sales made etc). That company table causes a failure in my rendering process currently, probably due to the fact that the sales_opportunities controller thinks @company is a new company rather than the existing one.

Can anyone help me understand this better please? In my thinking I'd define @company and render js from the users page (if the request came from there) and then define both differently if the AJAX request came from the companies page. However there may be a better alternative, and I'm open to any suggestions.

Thanks!

最满意答案

我通过在sales_opportunities控制器中使用以下代码解决了这个问题:

if @sales_opportunity.company.id? @company = @sales_opportunity.company else @company = Company.new(organization_id: params[:organization_id]) end

这并不能完全解决这个问题,因为create.js文件最终会呈现甚至不在页面上的项目,但我应该能够使用以下方法解决这个问题:

if (/companies/.test(self.location.href)) { ... };

或者类似于确保js代码只呈现与公司显示页面相关的项目。

I solved this by using the following code in the sales_opportunities controller:

if @sales_opportunity.company.id? @company = @sales_opportunity.company else @company = Company.new(organization_id: params[:organization_id]) end

This doesn't totally solve this issue, as the create.js file ends up rendering items that aren't even on the page, but I should be able to fix that using:

if (/companies/.test(self.location.href)) { ... };

Or similar to ensure the js code will only render the items relevant to the companies show page.

更多推荐

本文发布于:2023-07-16 12:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   应用程序   页面   rails   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!