我做错了什么? 我的rails应用程序中有一些按钮。例如 体育旅游政治
当我点击'体育' 时,它会带我走这条路 / tags / sports 这是我想要的,但如果我在浏览器中手动编辑路线,从/ tags / sports到 / tags / something (在/ tags /之后的任何东西)它仍然可以在它应该给出错误的地方工作,例如路线不存在或者slug不存在。
模型
class Tag
include Mongoid::Document
include Mongoid::Timestamps
has_and_belongs_to_many :articles
before_validation :generate_slug_from_tag
field :name, type: String
field :slug, type: String
def generate_slug_from_tag
self.slug = self.name.downcase.gsub(' ','-')
end
def to_param
self.slug
end
end
查看
<% @tags.each do |tag| %>
<%= link_to tag.name,tag_id_path(tag.slug),class: 'label label-default' %>
<% end %>
路线
get "/tags", to: "tag#index", as: "tags_index"
get "/tags/new", to: "tag#new", as: "tags_new"
post "/tags/new", to: "tag#create"
get "/tags/:slug", to:"tag#show", as: "tag_id"
get "/tags/:id/edit", to: "tag#edit", as: "tags_edit"
patch "/tags/:id", to: "tag#update"
put "/tags/:id", to: "tag#update"
delete "/tags/:id", to: "tag#destroy"