在另一个Rails中使用Controller中的方法

时间:2015-04-23 01:12:40

标签: ruby-on-rails ruby-on-rails-3 methods elasticsearch controller

我在rails 4 app上使用elasticsearch来搜索我的文章。

然而,当找不到文章时,它会重定向到一个页面,在该页面中它会提醒用户没有找到结果,并允许请求页面。而不是说"没有找到结果"我希望它说"找不到______的结果"其中是搜索栏查询。

我已经在articles_controller中定义了一个方法来获取查询但是它不会显示在页面上,因为" no results page"使用不同的控制器。

在这种情况下,将方法拉到另一个控制器或视图的最佳方法是什么?无法找到我正在尝试做的事情的直接答案。非常感谢你。

articles_controller:

  def index

    if params[:query].present?
      @articles = Article.search(params[:query], misspellings: {edit_distance: 1})
    else
      @articles = Article.all
    end

    if @articles.blank?
      return redirect_to request_path
    end

    get_query
  end

  private

  def get_query
    @userquery = params[:query]
  end

new.html.erb(查看"未找到结果"页面。使用与文章页面不同的控制器):

<body class="contactrequest">
 <div class="authform" style="margin-top:15px">
  <%= simple_form_for @request, :url => request_path do |f| %>

  <h3 style = "text-align:center; color:red;">No results found. <%= @userquery %></h3>
  <h1 style = "text-align:center; color:black;">Request a Page</h1>
  <h5 style = "text-align:center; color:black; margin-bottom: 25px;">Our experts will gather the information,<br> and have your page us ASAP!</h5>

  <%= f.error_notification %>

  <div class="form-group">

    <%= f.text_field :email, placeholder: 'Email', :autofocus => true, class: 'form-control' %>
  </div>
  <div class="form-group">

    <%= f.text_field :subject, placeholder: 'Item / Page Requested', class: 'form-control' %>
  </div>
  <div class="form-group">

    <%= f.text_area :content, placeholder: 'Any additional details...', class: 'form-control', :rows => '5' %>
  </div>

    <%= f.submit 'Request it', :class => 'btn btn-lg btn-danger btn-block' %>

<% end %>

正如您所看到的,我已经尝试将@userquery方法称为在视图页面上显示,但由于它在不同的控制器上定义,因此无法显示。任何建议都很棒。

0 个答案:

没有答案
相关问题