从View Outside Model(Rails)调用模型数据

时间:2016-09-13 14:52:19

标签: ruby-on-rails

我试图在ClubFitting页面上调用users#show模型中的数据并遇到困难。我已经检查过数据在我的数据库中是否正确保存了,所以我知道这只是从视图中调用它的问题。

我在users_controller

中有这个
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @full_swing = ClubFitting.where(user: current_user, club_type: "Full Swing")
    @wedge = ClubFitting.where(user: current_user, club_type: "Wedge")
    @putter = ClubFitting.where(user: current_user, club_type: "Putter")
    unless current_user && (current_user.admin || @user == current_user)
      redirect_to welcome_index_path
      flash[:alert] = "You do not have access to that page."
    end
  end
end

在我看来,我试图调用这样的数据(modelFullSwing中的一列):

        <% if @full_swing %>
          <p><strong>Model:</strong> <%= @full_swing.model %></p>
          <%= link_to "Retest", fitting_full_path %>
        <% else %>
          <%= link_to "Get Full Swing Club Recommendation", fitting_full_path %>
        <% end %>

但不是像&#34;模型:G&#34; (因为它应该基于user FullSwing.model的数据库值)我得到了&#34;模型:FullSwing&#34;在我看来。

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您需要在where()之后添加where(),因为@full_swing = ClubFitting.where(user: current_user, club_type: "Full Swing").first @wedge = ClubFitting.where(user: current_user, club_type: "Wedge").first @putter = ClubFitting.where(user: current_user, club_type: "Putter").first 会返回模型的集合,请执行此操作

find_by

或者,我建议您使用@full_swing = ClubFitting.find_by(user: current_user, club_type: "Full Swing") @wedge = ClubFitting.find_by(user: current_user, club_type: "Wedge") @putter = ClubFitting.find_by(user: current_user, club_type: "Putter") 代替

proc sql;
SELECT  *
FROM    CONNECTION TO ODBC                                       
       (
        SELECT   name, ID, timestamp

        FROM    TD.table
        WHERE   
        timestamp > date '2015-11-30' /* Teradata format */
        )
        where timestamp > 1764460800  /* SAS format */
;
quit;

希望有所帮助!