简体   繁体   English

Rails生产从应用程序/视图/模型渲染JS部分

[英]Rails production render JS partial from app/views/model

Hello everyone! 大家好!

I have a partial file called _more_contacts.js.coffee in app/views/contacts/ 我在app/views/contacts/有一个名为_more_contacts.js.coffee的部分文件

This partial is rendered via an Ajax call to the ContactsController , and update another partial ( _more_contacts.html.erb ) in a view: 通过对ContactsController的Ajax调用来呈现此部分,并在视图中更新另一个部分( _more_contacts.html.erb ):

Ajax call: Ajax呼叫:

<%= link_to "blah", more_contacts_path(@smthg) , method: :post, remote: true, id: "more_contacts", data: {last: raw(@id)} %>

Controller code: 控制器代码:

def more_contacts  
  @contacts = Contact.search(params)
  respond_to do |format|  
    format.js { render partial: "more_contacts" }  
  end  
end  

JS Partial code: JS部分代码:

$('#more_contacts_table').append('<%=j render partial: "more_contacts", formats: [:html] %>')  

HTML Partial: HTML部分:

<% @contacts.each do |contact| %>
  <tr>
    <td><%= link_to contact.company, contact_path(contact.id) %></td>
    <td><%= contact.email %></td>
  </tr>
<% end %>

Note that I struggled a couple of hours to find out that it shouldn't have the .erb extension, even though it contains some ruby code... 请注意,我花了几个小时才发现它不应该带有.erb扩展名,即使它包含一些红宝石代码也是如此。

Anyway, everything works great in development, but I can't make it works in production :( 无论如何,一切都可以在开发中很好地工作,但是我不能使其在生产中工作:(
I have tried the following in my config/environments/production.rb 我已经在我的config/environments/production.rb尝试了以下内容

config.serve_static_assets = false  
config.assets.compress = true  
config.assets.compile = true  
config.assets.digest = true  

config.assets.precompile += %w( contacts/more_contacts.js app/views/contacts/more_contacts.js )  
config.assets.precompile << "contacts/_more_contacts.js"  
config.assets.precompile << "#{Rails.root}/app/views/contacts/_more_contacts.js"  
config.assets.precompile << "#{Rails.root}/app/views/contacts/_more_contacts.js.coffee"  
config.assets.precompile << "#{Rails.root}/app/views/contacts/more_contacts.js"  
config.assets.precompile << "#{Rails.root}/app/views/contacts/more_contacts.js.coffee"  

But when I run my rake task to compile the assets, more_contacts.js is nowhere to be found ( public/assets/manifest.yml or public/assets/ ) and the Ajax call fails... 但是当我运行我的rake任务来编译资产时,找不到more_contacts.js( public/assets/manifest.ymlpublic/assets/ ),并且Ajax调用失败了...

I'm stuck here :( Any idea, anyone? 我被困在这里:(任何想法,有人吗?

通过将Coffee-rails从我的Gemfile中的资产组中移出来解决此问题,这归功于Rails JavaScript视图在生产中不起作用

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

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