简体   繁体   English

Rails代码在开发中有效,但在生产中无效

[英]Rails code works in development but not in production

I have some ruby if / else statements in one of my view files that should highlight the row different colors based on if the yes attribute is true , false or null . 我的一个视图文件中有一些ruby if / else语句,根据yes属性为truefalsenull ,应突出显示行的不同颜色。 The rows change colors accordingly in development but not production. 这些行在开发中会相应更改颜色,但在生产中不会更改。 In production every thing shows up in red and says no response even when the yes is not null in the database. 在生产中,即使数据库中的yes不为null ,所有事物也会以红色显示并表示没有响应。 I am stuck trying to figure out why this wouldn't work only in production? 我一直在努力弄清楚为什么这仅在生产中不起作用?

codes/admin.html.erb codes / admin.html.erb

<tbody>
  <% @code.each do |c| %>
    <% if c.yes == 't' %>
      <tr class = "green">
    <% elsif c.yes == 'f' %>
      <tr class = "gray">
    <% else %>
      <tr class = "red">
    <% end %>
      <td><%= c.code %></td>
      <td><%= c.name %></td>      
      <td><%= c.email %></td>
    <% if c.yes == 't' %>
      <td >Yes</td>
    <% elsif c.yes == 'f' %>
      <td >No</td>
    <% else %>
      <td > No Response</td>
    <% end %>
      <td><%= c.meal %></td>
    </tr>
  <% end %>
</tbody>

codes_controller.rb codes_controller.rb

def admin      
  @children = Child.all
  @code = Code.find_by_sql("SELECT c.code, g.name, g.email, g.yes, g.id as guest_id, m.name as meal
                            FROM codes c
                            LEFT OUTER JOIN guests g on g.code_id = c.id
                            LEFT OUTER JOIN meals m on g.meal_id = m.id
                           ")
end

css 的CSS

.red{
  background-color: #5e0009;
  color: white;
}

.gray{
  background-color: gray;
  color: white;
}

.green{
  background-color: #648293;
  color: white;
}

The stylesheet might not be loading due to your production environment configuration, or the @code query (within your admin action) does not return what you expect. 由于您的生产环境配置,样式表可能未加载,或者@code查询(在admin操作中)未返回您期望的结果。

Inspect the element that is not the color you expect. 检查不是您期望的颜色的元素。 Within developer tools, use the the "Sources" tab to verify that the stylesheet is present. 在开发人员工具中,使用“源”选项卡来验证样式表是否存在。 If your stylesheet is not in sources, the issue is assets related. 如果您的样式表不在源文件中,则说明问题与资产有关。 Meaning either your prod environment is misconfigured, the asset file itself is not in the right directory, or you are missing a require statement. 这意味着您的产品环境配置错误,资产文件本身不在正确的目录中,或者您缺少require语句。

Even before inspecting via browser a couple debugging steps you can do are most efficiently done via rails console so fire that up on prod. 甚至在通过浏览器进行检查之前,您可以通过Rails控制台最有效地完成几个调试步骤,以便在产品上进行调试。 Execute Code.find_by_sql query from your codes_controller/admin, inspect and verify the results are as expected. 从您的code_controller / admin执行Code.find_by_sql查询,检查并确认结果是否符合预期。 Next, within the rails console helper.asset_path('name_of_your_stylesheet.css') . 接下来,在rails控制台中helper.asset_path('name_of_your_stylesheet.css') The results should indicate where your problem is. 结果应指出您的问题所在。

If its a config thing, check that your config/environments/production.rb is configured properly. 如果是配置文件,请检查config / environments / production.rb的配置是否正确。 Config can vary based on platform, make sure you follow their documentation. 配置因平台而异,请确保遵循其文档。 If you are using heroku, check that you have config.assets.compile = false and also config.public_file_server.enabled = true if it's rails 5. 如果您使用的是heroku,请检查是否具有config.assets.compile = false以及config.public_file_server.enabled = true如果它是轨道5)。

George mentioned you might be using sqlite in dev and possibly Postgres for your production database, which would result in your else branch executing always. George提到您可能在开发数据库中使用了sqlite,并可能在生产数据库中使用了Postgres,这将导致您的else分支始终执行。 If that is the case, it is a great illustration of reasons never to use SQLite. 如果是这样,那就很好地说明了从不使用SQLite的原因。 Otherwise, perhaps you used MySQL locally in dev and Postgres in prod? 否则,也许您在开发人员本地使用MySQL,而在生产中使用Postgres? I'm sure you can sort that one out. 我敢肯定,你可以解决这个问题。

Finally, get rid of the conditionals you have polluting your view code. 最后,摆脱污染视图代码的条件。 There are three possible colors that the tr class can be and 1 of 3 strings will get enclosed in a td via. tr类可以是三种可能的颜色,并且3个字符串中的1个将封装在td via中。 You don't need a bunch of conditionals for that, you can create a helper method. 您不需要那么多条件,可以创建一个辅助方法。 Or create a hash and lookup the values you want ( td string and tr class) for each of the codes. 或创建一个哈希并为每个代码查找所需的值( td字符串和tr类)。

To do with hash, you'd have hash like this: 要进行哈希处理,您将具有如下所示的哈希值:

hash = {t: {color: 'green', td_string: 'yes'}, f: {color: "gray", td_string: "No"}}

And loop like: 像这样循环:

 <% @code.each do |c| %>
    <tr class='<%= hash.dig(c.yes.to_sym, :color) || \'red\' %>'>
      <td><%= c.code %></td>
      <td><%= c.name %></td>      
      <td><%= c.email %></td>
      <td><%= hash.dig(c.yes.to_sym, :td_string) || 'No Response'</td>
      <td><%= c.meal %></td>
    </tr>
  <% end %>

Bye bye conditionals! 再见有条件!

You're probably running different database types in development and production (ie MySQL in prod and SQLite in dev). 您可能在开发和生产中运行不同的数据库类型(即prod中的MySQL和dev中的SQLite)。 Different databases handle Boolean values differently eg 't' vs. 'true' vs. '1' . 不同的数据库以不同的方式处理布尔值,例如't''true''1'

I'll bet that if you compare your prod/dev logs you'd see the Boolean values are not being returned as 't' and 'f' in prod, causing your conditional to hit the else every time. 我敢打赌,如果您比较prod / dev日志,则会发现在prod中布尔值未以't''f'形式返回,从而导致条件每次都击中else

To solve this, make sure you're running the same database in both prod and dev! 要解决此问题,请确保在prod和dev中都运行相同的数据库!

Try to run your project in production in your local machine, to see if works with the local database. 尝试在本地计算机上的生产环境中运行项目,以查看是否可与本地数据库一起使用。 Just use RAILS_ENV=production rails s 只需使用RAILS_ENV=production rails s

Also check if the css is being imported correctly (just add red, gray or green using inspect element.) 还要检查css是否正确导入(只需使用inspect元素添加红色,灰色或绿色)。

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

相关问题 在 Rails 6 中:视差在开发中有效但不在生产中 - In Rails 6: Parallax works in development but not in production Rails 5 CarrierWave Gem在生产中工作但不在开发中 - Rails 5 CarrierWave Gem Works in Production But Not In Development Rails:检测用户代理是否在开发中工作但不在生产中? - Rails: Detecting user agent works in development but not production? Rails 4-服务器在开发模式下工作,但不在生产模式下工作 - Rails 4 - server works in development mode, but not in production Rails Ajax Spinner正在开发中,但未在生产中工作 - rails ajax spinner works in development but not production 方法在开发中有效,但在生产中不起作用Rails MongoDB - Method works in development but not production Rails MongoDB Rails 3:caches_page在开发中起作用,而不在生产中起作用 - Rails 3: caches_page works in development and not in production Rails Log Formatter在开发中可用,但不能在生产中使用 - Rails Log Formatter works in Development, but not Production Rails应用程序在开发中有效,但在生产中为空 - Rails application works in development but is empty in production 生产中使用的代码在开发中不再起作用? - Code that is working in production no longer works in development?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM