Reputation: 337
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
. 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. I am stuck trying to figure out why this wouldn't work only in production?
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
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
.red{
background-color: #5e0009;
color: white;
}
.gray{
background-color: gray;
color: white;
}
.green{
background-color: #648293;
color: white;
}
Upvotes: 0
Views: 1020
Reputation: 340
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.
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.
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. Execute Code.find_by_sql
query from your codes_controller/admin, inspect and verify the results are as expected. Next, within the rails console 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 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.
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. If that is the case, it is a great illustration of reasons never to use SQLite. Otherwise, perhaps you used MySQL locally in dev and Postgres in prod? 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. 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.
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!
Upvotes: 2
Reputation: 680
You're probably running different database types in development and production (i.e. MySQL in prod and SQLite in dev). Different databases handle Boolean values differently
e.g. 't'
vs. 'true'
vs. '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.
To solve this, make sure you're running the same database in both prod and dev!
Upvotes: 1
Reputation: 372
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
Also check if the css is being imported correctly (just add red, gray or green using inspect element.)
Upvotes: 0