[英]Routing to show action instead of custom action
news_controller.rb news_controller.rb
class NewsController < ApplicationController
before_action :set_news, only: [:show]
before_action :authenticate_user!, except: [:show, :index ]
def index
@news = News.where( state: true ).paginate(:page => params[:page], :per_page => 12).order('id DESC')
end
def new
@news = News.new
end
def show
@news_photos = @news.news_photos
end
def create
@news = News.new(news_params)
if @news.save
if params[:images]
params[:images].each do |image|
@news.news_photos.create(image: image)
end
end
@news_photos = @news.news_photos
redirect_to edit_news_path(@news), notice: "Saved..."
else
render :new
end
end
def destroy
@news = News.find(params[:id])
@news.destroy
respond_to do |format|
format.html { redirect_to news_index_path }
# format.json { head :no_content }
end
end
def search
@news = News.search(params[:search])
end
private
def set_news
@news = News.find(params[:id])
end
def news_params
params.require(:news).permit( :title, :description, :category, :keywords, :user_id, :email, :user_name)
end
end
news.rb news.rb
class News < ApplicationRecord
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :finders, :history]
def slug_candidates
[ :title,
[:title, :id]
]
end
def self.search(search)
pg_search_scope :search_full_text,
:against => :full_text,
:using => { :tsearch => { :prefix => true } }
end
end
index.html.erb index.html.erb
<div class="fh5co-box">
<h3 class="heading">Search</h3>
<%= form_tag(look_path, :method => "get") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search news" %>
<%= submit_tag "Search" %>
<% end %>
</div>
</div>
</div>
routes.rb routes.rb
resources :news, :except => [ :search]
get 'news/search' => 'news#search', :as => 'look'
As I submit the data inside the search field, it is routing to the show action instead of search action. 当我在搜索字段中提交数据时,它路由到显示操作而不是搜索操作。
the routes are 路线是
ews_index GET /news(.:format) news#index
POST /news(.:format) news#create
new_news GET /news/new(.:format) news#new
edit_news GET /news/:id/edit(.:format) news#edit
news GET /news/:id(.:format) news#show
PATCH /news/:id(.:format) news#update
PUT /news/:id(.:format) news#update
DELETE /news/:id(.:format) news#destroy
look GET /news/search(.:format) news#search
log: 日志:
Started GET "/news/search?utf8=%E2%9C%93&search=tax&commit=Search" for 183.83.117.57 at 2017-04-10 07:36:13 -0500
Processing by NewsController#show as HTML
Parameters: {"utf8"=>"▒~\~S", "search"=>"tax", "commit"=>"Search", "id"=>"search"}
^[[1m^[[36mNews Load (0.5ms)^[[0m ^[[1m^[[34mSELECT "news".* FROM "news" WHERE "news"."slug" = $1 ORDER BY "news"."id" ASC LIMIT $2^[[0m [["slug", "search"], ["LIMIT", 1]]
^[[1m^[[36mNews Load (0.7ms)^[[0m ^[[1m^[[34mSELECT "news".* FROM "news" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_id" = "news"."id" AND "friendly_id_slugs"."sluggable_type" = $1 WHERE ("friendly_id_slugs"."sluggable_type" = 'News' AND "friendly_id_slugs"."slug" = 'search') ORDER BY "friendly_id_slugs"."id" DESC LIMIT $2^[[0m [["sluggable_type", "News"], ["LIMIT", 1]]
Completed 404 Not Found in 5ms (ActiveRecord: 1.2ms)
ActiveRecord::RecordNotFound (can't find record with friendly id: "search"):
app/controllers/news_controller.rb:111:in `set_news'
Don't know where I have gone wrong.Is there anything to do with the slugs??? 不知道我哪里出了问题..有什么关系吗?
Any Help is highly Appreciated.Thanks in Advance!!! 非常感谢您的帮助。
Routes are search for in order. 路线按顺序搜索。
So in other words you show action matches the get
request for /news/(:id)
and your system processes SHOW
. 因此,换句话说,您显示的动作与
/news/(:id)
的get
请求匹配,并且系统处理SHOW
。 Move the line: 移动线:
get 'news/search' => 'news#search', :as => 'look'
higher up in the routes file and it should work fine, keep in mind though that this will block search
from being used as a id/slug. 在routes文件中更高的位置,它应该可以正常工作,请记住,尽管这样做会阻止
search
被用作id /段。 A cleaner route would be 更干净的路线是
get '/search/news' => 'news#search', :as => 'look'
as it'd get outside the news
scope and you wouldn't have conflicts with the news model. 因为它会超出
news
范围,并且不会与新闻模型发生冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.