Scott S.
Scott S.

Reputation: 759

Conditional display in View

On my index.html.erb I want to display one of two pieces of content based on a value the record has.

My splits have a column called "default" and if the content of that = 1 then I want to display a padlock icon indicating the user can't change that item. If the value is anything else I want to present a checkbox.

In my splits controller I have created "@default":

def index
    @splits = Split.all
    @chosen_splits = Issue.find(1).splits.order('updated_at desc')
    @split = Split.first
    @default = Split.find_by default: "1"
    # @balance = @split.balance
end

In my view I have the following code:

<% @chosen_splits.each do |split| %>

<tr>
  <td> <!-- trying to have conditional presentation -->
    <% if @default == '1' %>
    <i class="fa fa-2x fa-lock"></i>
    <% else %>
    <input type="checkbox" value>
    <% end %>
  </td> <!-- end of trying to have conditional presentation -->
  <td><%= link_to split.name, split_path(split) %></td>
  <td class="text-right"><%= number_with_delimiter(split.quantity) %></td>
  <td><%= link_to 'Edit', edit_split_path(split), :class => 'btn btn-xs btn-default' %></td>
  <td><%= link_to 'Delete', split, method: :delete, data: { confirm: 'Are you sure?'}, :class => 'btn btn-xs btn-danger' %></td>
</tr>
<% end %>

From what I saw here this should be working. But, it's not.

My log file is showing:

Processing by SplitsController#index as HTML
  [1m[35mIssue Load (0.4ms)[0m  SELECT  "issues".* FROM "issues" WHERE "issues"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mSplit Load (0.3ms)[0m  [1mSELECT  "splits".* FROM "splits"  ORDER BY "splits"."id" ASC LIMIT 1[0m
  [1m[35mSplit Load (0.2ms)[0m  SELECT  "splits".* FROM "splits" WHERE "splits"."default" = $1 LIMIT 1  [["default", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT  "issues".* FROM "issues" WHERE "issues"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35m (0.3ms)[0m  SELECT SUM("splits"."quantity") FROM "splits"
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT SUM("splits"."quantity") FROM "splits"[0m
  [1m[35mSplit Load (0.2ms)[0m  SELECT "splits".* FROM "splits"
  [1m[36mSplit Load (0.3ms)[0m  [1mSELECT "splits".* FROM "splits" WHERE "splits"."issue_id" = $1  ORDER BY updated_at desc[0m  [["issue_id", 1]]
  Rendered splits/index.html.erb within layouts/application (7.5ms)
Completed 200 OK in 625ms (Views: 620.9ms | ActiveRecord: 1.6ms)

All help is greatly appreciated.

Upvotes: 1

Views: 1251

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36880

@default doesn't contain a string with "1", it contains a Split object, the one where the column default contains "1".

Or, if it can't find that object, then it contains nil.

Maybe what you want to do is better done with...

<% @chosen_splits.each do |split| %>
    <tr>
      <td> <!-- trying to have conditional presentation -->
        <% if split.default == '1' %>
          <i class="fa fa-2x fa-lock"></i>
        <% else %>
          <input type="checkbox" value>
        <% end %>

Upvotes: 1

Related Questions