按first_name和last_name

时间:2017-05-25 10:37:12

标签: ruby-on-rails

我正在尝试通过另一个raport模型搜索来自两个不同模型的数据。我需要按用户的名字和姓氏过滤索引视图中列出的数据。

##如何在data_filter.html.erb视图中搜索和显示过滤后的数据?     ## in raports / index.html.erb:

    <div>
    <div class="" id="pro_content">
      <div class="main_container">
        <p id="notice"><%= notice %></p>

        <h1>Raports </h1>
        <hr>
        <%= button_to "Filter the data", {:controller => 'raports',:action=>'data_filter'},class: "btn btn-primary kerkes1", :method => "get" %>
        <table class="table ">
          <thead>
            <tr>
              <th>User</th>
              <th>Vacation type</th>
              <th>Start Data</th>
              <th>End Data</th>
              <th>Ditet</th>
              <td>status</td>
              <th colspan="3"></th>
            </tr>
          </thead>
          <tbody>

            <% @vacation_requests.each do |vacation_request| %>





            <tr class="<%= vacation_request.request_level%>">
              <td>
                <% @home.each do |u|%>
                  <% if u.id == vacation_request.user_id%>
                    <%= u.first_name + " "+ u.last_name%>
                  <%end%>
                <%end%>
              </td>
              <td>
                <% @v_r.each do |vr|%>
                  <%if vr.id == vacation_request.vacation_type_id %>
                    <%= vr.vacation_type%>
                <% end end%>
              </td>
              <td><%= vacation_request.start_data %> </td>
              <td><%= vacation_request.end_data %></td>
                 <td>

                     <%=vacation_request.skip_holidays%>


              </td>
               <% if vacation_request.request_level == "accepted"%>
                <td style = "color: green"><i class="fa fa-check"></td>
                <%elsif vacation_request.request_level == "rejected"  %>
                <td style = "color: red"><i class="fa fa-times"></i></td>
                <%else%>
                <td style = "color: #daa520"><i class="fa fa-hourglass-half"></td>
                <%end%>
              <!-- <td><%= vacation_request.description %></td> -->
              <td><%= link_to 'Show', vacation_request %></td>
              <td><%= link_to 'Destroy', vacation_request, method: :delete, data: { confirm: 'Are you sure?' } %></td>
              <!-- <td><%= link_to 'Edit', edit_vacation_request_path(vacation_request) %></td>
              <td><%= link_to 'Destroy', vacation_request, method: :delete, data: { confirm: 'Are you sure?' } %></td> -->
            </tr>

            <% end %>
          </tbody>
        </table>
        <br>


    <p>
      Shiko ne formatin Excel: <br>
        <%= button_to "Excel ",raports_path(format: "xls") ,class: "btn btn-primary ", :method => "get" %>

    </p>


      </div>
      </div>
      </div>
      <div style="clear:both"></div>  

    <div style="clear:both"></div>

##在raport.rb中:

    def client_full_name
        "#{User.first_name} #{User.last_name}"
        end

        def self.search_by_client_full_name(query)
          where("(User.first_name ||  User.last_name) LIKE :q", :q => "%#{query}%")
        end

在raports_controller.rb

    def data_filter
        @ndalo_userin = User.find(session[:user_id])
          if @ndalo_userin.status.to_i == 1
            redirect_to root_path

          end
        @home=User.all
        @useri = User.find(session[:user_id])
        @adminRequest =VacationRequest.where(:request_level => "to_admin")
        @vacation_types  = VacationType.all
        @vacation_requests = VacationRequest.all


       if params[:search]
          @home = User.search_by_client_full_name(params[:search])
            else
          @home = User.all
       end
          # head 404 if @users.blank?
      # @raport=Raport.new

          end

在data_filter.html.erb

<h3>Search for vacations by fullname </h3>
      <%= form_tag raports_path, :method => :get do %>
      First_Name:<%= text_field_tag :first_name, params[:first_name]%>
      Last_name:<%= text_field_tag :last_name, params[:last_name]%>
     <%=print%>
      <%= submit_tag "Search", :client_full_name => nil , :class => "btn btn-primary"%>
      <%end%>

## how can i search for a certain user displayed in raports/index.html.erb even though firs_name and last_name are not saved in the report model just in user model?

1 个答案:

答案 0 :(得分:0)

您在查询中不需要User.first_name。此外,您需要引用搜索字符串。

def self.search_by_client_full_name(query)
  where("first_name LIKE :q OR last_name LIKE :q ", q: "'%#{query.downcase}%'")
end

使用ILIKEdowncase搜索字符串

def self.search_by_client_full_name(query)
  where("first_name ILIKE :q OR last_name ILIKE :q ", q: "'%#{query}%'")
end
  

名字和姓氏仅在user.rb模型中

您正在User

上调用该方法
User.search_by_client_full_name(params[:search])

您应该将方法移至User模型

注意

此外,这应该是User模型

def client_full_name
  "#{first_name} #{last_name}"
end