mardi 13 décembre 2016

How to pass a local string to a partial in ruby on rails

TL;DR Version of Question

Is it possible to pass a string to a partial and have the partial use that string in an if statement or switch case.

Am I breaking some valuable rule of Rails by having the views talk directly to each other?

Long Version w/ Code Snippets

I think it's safe to say that at this point in time I've read every post there is about how to pass a local variable. I'm still having trouble with accomplishing this. I'm very new to rails, like.. 1 week of learning, so what I'm trying to do might be completely incorrect.

Anyways, I have it set up so that when a user clicks a link on the navbar a bootstrap modal loads with the correct form inside of it. I've set each unique form as their own partial so that I can just drop there in where needed and I've also setup the modal as a partial. I'm trying to follow this DRY method and make it so that in the modal partial it has a if/else or switch logic that only loads the correct form based on what the link passes it.

Modal Partial:

<!-- Modal -->
<div class="modal fade" id="modalTemplate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal Template</h4>
      </div>
      <div class="modal-body">
        <% if %VARIABLE% == "%PASSED-INPUT%" %>
          <% @customer = Customer.new %>
          <%= render "customers/new" %>
        <% elsif %VARIABLE% == "%PASSED-INPUT%" %>
          --> Do something else
        <% end %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

NavBar Partial:

<%= render 'layouts/modal_template' %>

<nav class="navbar navbar-default navbar-fixed-side">
    <h6 class="navbar-heading">Customer Management</h6>
  <div class="list-group">
    <%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', what_to_render: "Test", class: 'list-group-item' %>
    <%= link_to "Manage Customers", '#', class: 'list-group-item' %>
    </div>
    <h6 class="navbar-section-heading">Customer Status Management</h6>
    <div class="list-group">
    <%= link_to "Add New Customer Status", new_customers_status_path, class: 'list-group-item' %>
    <%= link_to "Manage Customer Statuses", '#', class: 'list-group-item' %>
    </div>
</nav>

The Main View for this Page:

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-3 col-lg-2">
      <%= render 'layouts/side-nav' %>
    </div>
    <div class="col-sm-9 col-lg-10">
      <div class="well">
        <%= render 'test' %>
        <%= render 'view_customers' %>
      </div>
    </div>
  </div>
</div>

Now, the "Added New Customer" link_to is the part that is giving me the most trouble. I've tried every different combination provided online, but it always seems to be based around the idea of working with an object or something from the database. Thing is, I'm just trying to load a different page based on the selected button all while using the same modal partial.

link_to Examples that I've Tried:

<%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', what_to_render: "Test", class: 'list-group-item' %>
<%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', :locals => { what_to_render: "Test" }, class: 'list-group-item' %>

There are more, but I've deleted so many lines of code at this point that I've lost them. Some included using the link_to with :partial and :locals

Aucun commentaire:

Enregistrer un commentaire