简体   繁体   English

Rails 4 - 通过ajax在bootstrap模式中显示数据

[英]Rails 4 - Display data inside a bootstrap modal via ajax

I have a pretty simple Ruby On Rails app with bootstrap 3 for the front end: I have Deals, and each Deal has_many Opportunities. 我有一个非常简单的Ruby On Rails应用程序,前端有bootstrap 3:我有交易,每个交易都有很多机会。

On each Deal page (url of deal 1 is like myapp.com/id=1), I want to have a textual link that says "view deal's opportunities" and that must trigger when clicked the appearance of content inside a Bootstrap modal. 在每个交易页面(交易1的网址类似于myapp.com/id=1),我希望有一个文本链接,说明“查看交易的机会”,并且必须在单击Bootstrap模式中的内容外观时触发。

Very basic but for specific reasons, I wish to load the content of the modal via Ajax. 非常基本但由于特定原因,我希望通过Ajax加载模态的内容。 (the content is not visible when users load the Deal page but only when they click on the link 'view deal's opportunities' (当用户加载“交易”页面时,内容不可见,但仅当他们点击“查看交易机会”链接时才会显示

I found many resources online for Rails4/ajax/Forms and other stuff with Ajax but not a simple "display content/text" inside a modal via Ajax. 我在网上找到了许多资源,用于Rails4 / ajax / Forms以及其他Ajax资源,但不是简单的“显示内容/文本”在模式中通过Ajax。 Should be easy but I'm stuck. 应该很容易,但我被卡住了。

controller/deals_controller.rb 控制器/ deals_controller.rb

def showcase    
    deals = Deal.friendly.find(params[:id])   

    respond_to do |format|
      format.html # showcase.html.erb
      format.json { render json: @deal }
    end
end 

def show_opportunities

    @opportunities = Opportunity.where('deal_id = ? AND deal_type = ?',
                             @deal.id, "high tech").first

    respond_to do |format|
      format.js
    end

  end

app/views/deals/showcase.html.erb 应用程序/视图/交易/ showcase.html.erb

this is the beginning
<%= render 'deals/deal_info_zone' %>
this is the end

views/deals/_deal_info_zone.html.erb 意见/交易/ _deal_info_zone.html.erb

<div id="zoneA">  

  <div style="color:red;padding-top: 150px;">
    <%= link_to "view infos of Opportunities", deal_opportunities_modal_path, remote: true %>
  </div>
</div>

Here is the modal I'd like to trigger via ajax: views/deals/opportunity_moda l 这是我想通过ajax触发的模态:views / deals / opportunity_moda l

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">

      <div class="modal-body"> 

          <%= @opportunity.name %> <br/>     

      </div>

    </div>
  </div>
</div>

routes.rb 的routes.rb

match '/deals/:id', # this is the Deal page
    to:   'deals#showcase',
    via:  'get',
    as:   :deal_page

match '/opportunity_modal',
    to:   'deals#show_opportunities',
    via:  'get',
    as:   :deal_opportunities_modal

I'm a rookie in RoR and have never used ajax, ujs maybe... I feel it's missing things like javascript code and xhr request... 我是RoR的新手,从未使用过ajax,也许是......我觉得它缺少像javascript代码和xhr请求......

The current situation is this one: when I go on a Deal page (for ex page /deal id=1) the textual link "view infos of Opportunities". 目前的情况是这样的:当我进入交易页面(对于ex page / deal id = 1)时,文本链接“查看机会的信息”。 and the browser points to http://localhost:3000/opportunity_modal but it does not trigger anything when I click on it. 并且浏览器指向http:// localhost:3000 / opportunity_modal但是当我点击它时它不会触发任何内容。 Nothing happens. 什么都没发生。 Nothing gets displayed btw on firebug console. bbw在firebug控制台上没有显示任何内容。

When you click a remote: true link, rails is not looking for opportunity_modal.html.erb , it's looking for opportunity_modal.js.erb . 当您单击remote: true链接时,rails不会查找opportunity_modal.html.erb ,它正在寻找opportunity_modal.js.erb What you need to to do is put your modal in a partial, then render it with JavaScript. 你需要做的是将你的模态放在局部,然后用JavaScript渲染它。

  1. Put your modal code into a partial named views/deals/_opportunity_modal.html.erb . 将您的模态代码放入部分命名的views/deals/_opportunity_modal.html.erb

  2. Create a views/deals/opportunity_modal.js.erb file that will render and show the modal when the this action is triggered. 创建一个views/deals/opportunity_modal.js.erb文件,该文件将在触发此操作时呈现并显示模态。

$('body').append('<%= j render partial: "views/deals/opportunity_modal" %>');
$('#myModal').modal('show');

Also, you have a variable @opportunity in the modal, but it's not defined in your controller, so make sure to define that as well. 此外,你在模态中有一个变量@opportunity ,但它没有在你的控制器中定义,所以一定要定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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