简体   繁体   中英

Rails 3: Aliasing attribute names in meta_search

I am using meta_search as follows:

# app/controllers/articles_controller.rb
def index
  @search = Article.search(params[:search])
  @articles = @search.all
end

# app/views/articles/index.html.erb
<%= form_for @search, :url => articles_path, :html => {:method => :get} do |f| %>
  <%= f.text_field :my_very_long_attribute_name_contains %><br />
  <%= f.submit %>
<% end %>

This works as expected, by allowing the 'my_very_long_attribute_name' attribute to be searched.

The problem is, ?search[my_very_long_attribute_name_contains] appears in the query string. What is the best way to map a shorter name to this attribute? ie ?search[mvlan_contains]

This isn't just a case of wanting to make long attribute names shorter, but I also need to disguise the names of some potentially sensitive attributes for search purposes.

I have looked at alias_attribute , but couldn't get meta_search to recognise the attribute alias.

I welcome any suggestions.

You could do as rspeicher suggested. The custom search method will be displayed in your parameter list.

Re: sensitive info in your attributes, though... If someone knowing your attribute names in the view poses a potential problem for your application, I'd seriously consider what security steps are being taken in your model and controller layers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM