我在我的rails 4应用程序上使用elasticsearch来搜索我的文章。在另一个Rails中使用控制器中的方法
但是,如果找不到文章,它会重定向到提醒用户未找到结果的页面,并允许请求页面。而不是它说“没有找到结果”我希望它说“没有找到______的结果”,其中需要搜索栏查询。
为了获得查询,我在articles_controller中定义了一个方法,但它不会显示在页面上,因为“无结果页面”使用不同的控制器。
在这种情况下将方法拉到另一个控制器或视图上的最佳方法是什么?无法找到我想要做的事情的直接答案。非常感谢你。
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方法在视图页面上显示,但它不显示,因为它是在不同的控制器上定义的。任何建议都会很棒。
正确。这样,你就拥有了你所需要的所有环境。 –
嗯,得到'Template is missing'错误。 – Kathan
您是否将偏分量与索引视图放在同一个文件夹中?看起来应该是'app/views/articles'。 –