简体   繁体   English

实施Rails动态部分

[英]Implementing Rails Dynamic Partials

I was building out a CMS and ran into trouble in implementing a new feature. 我当时正在建立CMS,但在实施新功能时遇到了麻烦。 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: 目前,我为用户show如下:

<% 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: 用户可以在侧面菜单中的render @restaurants处单击特定餐厅,该餐厅将转到部分餐厅:

<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? 我如何让用户单击菜单中的一家餐厅,然后将所选的特定餐厅发送回用户show页面中的<%= render @campaigns, object: @restaurants %>

one possibe way after having slected a user is 选择用户后的一种可能的方法是

@restaurants = @user.restaurants

is this what you are thinking of 这是你在想什么

@campaigns = @restaurant.campaigns

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

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