简体   繁体   中英

Implementing Rails Dynamic Partials

I was building out a CMS and ran into trouble in implementing a new feature. Essentially, I aim to have a user own several locations that exist in a menu, and as a 'User' clicks on each one of the 'Locations', the partial on that page then displays all the 'Signs' that belong_to that 'Location' and only those 'Signs' that belong_to that 'Location'. The data model is very straight forward as:

class User < ActiveRecord::Base 
    has_many :restaurants, dependent: :destroy
end

class Restaurant < ActiveRecord::Base

    belongs_to :user
    validates  :user_id, presence: true

    has_many :campaigns, dependent: :destroy
end

class Campaign < ActiveRecord::Base
    belongs_to :restaurant
end

In terms of implementation however, I am unaware of how to make the partials in the page dependent on what the user has selected and then display only that associated content.

At present I have the show for the users as:

<% provide(:title, @user.name) %>
<div class="row">
    <aside class="span4">
        <section>
            <h1>
                <%= gravatar_for @user %>
                <%= @user.name %>
            </h1>
        </section>
        <section>
            <% if @user.locations.any? %>
                <div>
                    <%= link_to "Start a Campaign", newcampaign_path, class: "btn btn-medium btn-primary" %>
                </div>
                <%= link_to "Manage Locations", uploadlocations_path %>     
            <% else %>
                <%= link_to "Upload Locations", uploadlocations_path, class: "btn btn-medium btn-primary" %> 
            <% end %>
        </section>
        <section>

            <%# disply list of restaurants%>

            <%= link_to "Add New Restaurant", newrestaurant_path, class: "btn btn-medium btn-primary" %>
            <ul class ="campaigns">
                <%= render @restaurants %>
            </ul>

        </section>
    </aside>
    <div class="span8">
        <% if @restaurants.any? %>

            <ol class="campaigns">
                <%= render @campaigns, object: @restaurants %>
            </ol>

        <% end %>
    </div>
</div>

with the user able to click on the specific restaurant in the side menu at render @restaurants , which goes to the partial:

<ul>
    <span class="content">
        <%= link_to restaurant.name, '#' %>
    </class>
</ul>

How can I have a user click on a restaurant in the menu and send that specific restaurant selected back into the <%= render @campaigns, object: @restaurants %> in the users show page?

one possibe way after having slected a user is

@restaurants = @user.restaurants

is this what you are thinking of

@campaigns = @restaurant.campaigns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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