开发者

Before and After Filters/Functions for Rails Routes

开发者 https://www.devze.com 2023-02-04 19:08 出处:网络
I have an idea and I am not sure if is is already done.Right now to make url search engine friendly developers customize to_param function of model class. Then they call to_s function to get the id of

I have an idea and I am not sure if is is already done.Right now to make url search engine friendly developers customize to_param function of model class. Then they call to_s function to get the id of the elmenet.

What if I want to create url for not model but for string. Lets say I create link for post controller's search action. The only variable in the url is search_string. So how can i create seo link for this search page. I know how to create links etc etc. but my problem is that I want to call function on this string such as to_param or something like that to make seo string, also in the controller to perform a search I have to humanize this string again. So what I want is this:

In every view, I dont want to use urlizing method to make it se friendly

In every controller, I do not want to call any function to humanize string back again, this should done by router

In the router:

match 'search/:str开发者_C百科ing' => 'Post#search', :as => :search, before => some_before_router_helper_function, after => some_after_router_helper_function

In these helper function what I will do is that i will parametrize any params I want, then I will humanize any params back again

example before/after router helpers:

def some_before_router_helper_function
  string = string.underscore.dasherize
end

def some_after_router_helper_function
  param[:string] = param[:string].undasherize.un_underscore
end


As Heikki pointed: https://github.com/svenfuchs/routing-filter

0

精彩评论

暂无评论...
验证码 换一张
取 消