在另一个 Rails 中使用来自控制器的方法
Using a Method from Controller in Another Rails
我在我的 rails 4 应用程序上使用 elasticsearch 来搜索我的文章。
但是,当找不到文章时,它会重定向到一个页面,提醒用户未找到任何结果,并允许请求页面。而不是说 "no results found" 我希望它说 "no results for ______ found" 其中接受搜索栏查询。
我在 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(查看 "no results found" 页面。使用与文章页面不同的控制器):
<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 方法以显示在视图页面上,但它没有显示,因为它是在不同的控制器上定义的。任何建议都会很棒。
我建议您渲染结果和不渲染相同的结果 controller/view。一个干净利落的方法是使用 partials。您的视图可能类似于:
<% if @articles.blank? %>
<%= render 'no_results' %>
<% else %>
<%= render 'results' %>
<% end %>
然后,您只需创建 _no_results.html.erb
并用您当前的 new.html.erb
内容填充它,并对名义结果页面执行相同操作(部分 _results.html.erb
)。
我在我的 rails 4 应用程序上使用 elasticsearch 来搜索我的文章。
但是,当找不到文章时,它会重定向到一个页面,提醒用户未找到任何结果,并允许请求页面。而不是说 "no results found" 我希望它说 "no results for ______ found" 其中接受搜索栏查询。
我在 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(查看 "no results found" 页面。使用与文章页面不同的控制器):
<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 方法以显示在视图页面上,但它没有显示,因为它是在不同的控制器上定义的。任何建议都会很棒。
我建议您渲染结果和不渲染相同的结果 controller/view。一个干净利落的方法是使用 partials。您的视图可能类似于:
<% if @articles.blank? %>
<%= render 'no_results' %>
<% else %>
<%= render 'results' %>
<% end %>
然后,您只需创建 _no_results.html.erb
并用您当前的 new.html.erb
内容填充它,并对名义结果页面执行相同操作(部分 _results.html.erb
)。