【问题标题】:Ruby on Rails haml sql queryRuby on Rails haml sql 查询
【发布时间】:2016-05-19 20:37:50
【问题描述】:

我正在向 HAML 页面传递一个参数并成功检索它。

%h1 Welcome to your History
%p
  %strong Name:
  = params[:name]

%table#houses
  %thead
    %tr
      %th Name
      %th UserName
      %th User ID
      %th Email
      %th Rented House
      %th Start Date
      %th End Date
      %th Total Cost ($)
      %th Rating

我想根据查询Select * from histories where name = params[:name]显示一个表格。

我该怎么做?

【问题讨论】:

  • 但是问题出在哪里,您是否尝试过查看文档,或者您只是没有寻找简单的方法?!
  • 我现在只需要快速完成这就是寻求帮助的原因
  • 安库什,你现在是认真的吗?您是在浪费别人的时间,而不是快速查看一些基本编程内容的手册吗?我投票结束你的问题,因为可以通过简单的谷歌搜索找到。有大量的信息。聪明点,不要浪费别人的时间,试着自己搜索基本的东西。

标签: ruby-on-rails haml


【解决方案1】:

这很简单,并且假设您已经在控制器中执行了查询,如下所示:

def history
  @histories = History.where(name: params[:name]);
end

在您看来,您只需根据数据添加行和列:

%h1 Welcome to your History
%p
  %strong Name:
  = params[:name]

%table#houses
  %thead
    %tr
      %th Name
      %th UserName
      %th User ID
      %th Email
      %th Rented House
      %th Start Date
      %th End Date
      %th Total Cost ($)
      %th Rating
  - @histories.each do |history|
    %tr
      %td
       = history.name
      %td
       = history.user_name
      %td
       = history.user_id
      %td
       = history.email
      %td
       = history.rented_house
      %td
       = history.start_date
      %td
       = history.end_date
      %td
       = history.total_cost
      %td
       = history.rating

这将从历史数据中填充您的表格。

【讨论】:

  • @AnkushArora 如果这回答了您的问题,请单击答案旁边的复选标记以接受它。复选标记将颜色变为绿色。 Upvotes 也总是受到赞赏。 :D