我正在建立一个应用程序,用户可以将他们的数据与朋友数据进行比较。
我设法通过将用户引导到特定的比较页面(见下文)来实现它的工作
但现在我正在改变外观,我想在比较手册中打开“比较页面”。
所以...这是它位于users/show.html.erb
的好友列表的代码,当前用户可以将他的数据与好友数据进行比较。
<ul>
<% @user.friendships.each do |friendship| %>
<li>
<%= link_to user_profile_path(friendship.friend), :method => :get do %>
<%= friendship.friend.profile.name %>
(<span class="glyphicon glyphicon-erase"></span> <%= link_to "Destroy Friendship", friendship, :method => :delete %>)</li>
<li>
<%= link_to friendship do %>
<span class="glyphicon glyphicon-send"></span> <%= link_to 'Send Messages', new_message_path(to: friendship.friend.id) %> </li>
<li>
<%= link_to friendship do %>
<span class="glyphicon glyphicon-stats"></span> <%= link_to 'Compare', compare_friends_path(to: friendship.friend.id)%></li>
<% end %>
<% end %>
<% end %>
<% end %>
</li>
</ul>
和compare_friends_path
<div class="row">
<%= render 'users/shared/paper_compare' %>
<%= render 'users/shared/electro_compare' %>
<%= render 'users/shared/hwater_compare' %>
<%= render 'users/shared/cleaning_compare' %>
<%= render 'users/shared/travel_compare' %>
</div>
以下是其中一个部分的代码......在这种情况下electro_compare.html.erb
...所有部分基本相同,因为它们都有比较不同数据的逻辑。
<div class="col-md-8 col-md-offset-1 well">
<h3>Compare Electricity</h3>
<div class="col-md-6">
<table class="table table-hover table-box-compare ">
<thead>
<th>compare -Electricity</th>
<th><%= @user.profile.name %></th>
<th><%= current_user.profile.name %></th><br>
</thead>
<tbody>
<tr>
<td>Total Electricity</td>
<td><strong><%= @electro_total_user %> kwst </strong></td>
<td><strong><%= @electro_total %> kwst </strong></td>
</tr>
<tr>
<td>kwst á hvert stöðugildi</td>
<td> <strong><%= @electro_total_per_capita_user %> kwst </strong></td>
<td><strong><%= @electro_total_per_capita %> kwst </strong></td>
</tr>
<br>
<tr>
<td> kwst. f hvert stöðugildi</td>
<td> <strong><%= @electro_total_per_m2_user %> kwst/fm. </strong></td>
<td> <strong><%= @electro_total_per_m2 %> kwst/fm. </strong></td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-6">
Rafmagnsnotkun Samanburður við <%= @user.profile.name %>
<%= bar_chart [
{name: "Rafmagnsnotkun #{@user.profile.name}", data: @electro_total_user, 'interpolateNulls':true},
{name: "Rafmagn á hvert stöðugildi #{@user.profile.name}", data: @electro_total_per_capita_user, 'interpolateNulls':true},
{name: "Rafmagnsnotkun #{current_user.profile.name}", data: @electro_total, 'interpolateNulls':true},
{name: "Rafmagn á hvert stöðugildi #{current_user.profile.name}", data: @electro_total_per_capita, 'interpolateNulls':true} ] %>
</div>
</div>
以下是users_controller.rb的代码
class UsersController < ApplicationController
def index
@users = User.all
@users = User.order('created_at DESC').paginate(page: params[:page], per_page: 30)
end
def create
end
def show
# @user = current_user
@user = User.find(params[:id])
@users = User.order('created_at DESC').paginate(page: params[:page], per_page: 30)
#@electro_total = current_user.electros.sum(:electricity_kwst)
@paper = current_user.papers.build
@electro = current_user.electros.build
@hwater = current_user.hwaters.build
@cleaning = current_user.cleanings.build
@transport = current_user.transports.build
@papers = current_user.papers
end
def compare
if current_user.profile
#@user = User.find(params[:id])
#@#users = User.friendships.friend(params[:id])
@user = User.find_by(id: params[:to].to_i) if params[:to]
@paper_weight_total_user = @user.papers.sum(:paper_weight)
@paper_weight_per_capita_user = @user.papers.sum(:paper_weight) / (@user.profile.staff)
@env_paper_weight_user = @user.papers.sum(:env_paper_weight)
@env_paper_ratio_user = (@env_paper_weight_user / @paper_weight_total_user) * 100
@paper_tree_ratio_user = (@user.papers.sum(:paper_weight) / 1000) *15
@paper_tree_co2_rescue_user = @paper_tree_ratio_user * (0.492)
#Rafmagn
@electro_total_user = @user.electros.sum(:electricity_kwst)
@electro_total_per_capita_user = @user.electros.sum(:electricity_kwst) / @user.profile.staff
@electro_total_per_m2_user = @user.electros.sum(:electricity_kwst) / @user.profile.building_size
#HotWater
@hwater_total_m3_user = @user.hwaters.sum(:hot_water_cubic_meter)
@hwater_total_m3_per_capita_user = @user.hwaters.sum(:hot_water_cubic_meter) /@user.profile.staff
@hwater_m3_m2_ratio_user = @user.hwaters.sum(:hot_water_cubic_meter) / @user.profile.building_size
#Ræsting
@cleaning_total_user = @user.cleanings.sum(:cleaning_liter)
@cleaning_staff_ratio_user = @user.cleanings.sum(:cleaning_liter) / @user.profile.staff
@cleaning_building_ratio_user = @user.cleanings.sum(:cleaning_liter) / @user.profile.building_size
#Ferðalog-bilar
@transport_co2_km_user = @user.transports.sum(:transport_km) * (0.1404)
@transport_co2_km_staff_ratio_user = @user.transports.sum(:transport_km) * (0.1404) / @user.profile.staff
@transport_km_staff_ratio_user = @user.transports.sum(:transport_km) / @user.profile.staff
@transport_km_user = @user.transports.sum(:transport_km)
#Ferðalog-flug
@transport_flight_co2_user = @user.transports.sum(:transport_flight_km) * (0.1722)
@transport_flight_co2_staff_ratio_user = @user.transports.sum(:transport_flight_km) * (0.1722) / @user.profile.staff
@transport_flight_km_user = @user.transports.sum(:transport_flight_km)
@transport_flight_km_staff_ratio_user = @user.transports.sum(:transport_flight_km) / @user.profile.staff
#Co2 vegna ferðalaga
@co2_due_to_transport_user = (@transport_flight_co2 + @transport_co2_km) / 1000
@tree_count_rescue_user = @co2_due_to_transport * 492
else
redirect_to user_path
end
end
end
然后是标签的代码....我有一个Accordion打开标签....当标签#10打开时,我希望能够在compare.html.erb
中显示代码在标签10中。我将代码复制到名为compare_users.html.erb
的部分,但它无效。
<div class="col-sm-12 col-md-12">
<div class="well">
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="tab1">
<%= render 'conversations/msg_part' %>
</div>
<div role="tabpanel" class="tab-pane" id="tab2">
<%= render 'users/shared/users_find' %>
</div>
<div role="tabpanel" class="tab-pane" id="tab3">
<%= render 'users/shared/friends' %>
</div>
<div role="tabpanel" class="tab-pane" id="tab4">
<%= render 'pages/partials/paper_part' %>
</div>
<div role="tabpanel" class="tab-pane" id="tab5">
<div class="electro">
<%= render 'pages/partials/electro_part' %>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="tab6">
<h1>Tab6</h1>
</div>
<div role="tabpanel" class="tab-pane" id="tab7">
<div class="hwater">
<%= render 'pages/partials/hwater_part' %>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="tab8">
<h1>Tab8</h1>
</div>
<div role="tabpanel" class="tab-pane" id="tab9">
<h1>Tab9</h1>
</div>
<div role="tabpanel" class="tab-pane" id="tab10">
<%= render 'users/shared/compare_users' %>
</div>
</div>
</div>
我在这里感觉有点失落。任何人都可以指引我走上正确的道路吗?
答案 0 :(得分:0)
首先,rails部分需要有一个下划线,所以你的部分应该是Users
。
您还需要将所有控制器代码移动到show方法中。
它应该工作。
你有一个非常大的控制器,你可能想考虑使用一个演示者来清理它。
希望这能帮到你!
幸福 肖恩