Search code examples
ruby-on-railsruby-on-rails-3meta-search

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? i.e. ?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.


Solution

  • 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.