Reputation: 6597
I was wondering if you would be able to help out. First off I have a view as follows:
<%= form_tag all_path, :method => 'get' do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<p>
Search:
<%= text_field_tag :search %>
by
<%= select_tag :search_column, options_for_select(MyTable.translated_searchable_columns(['col1', 'col2']), params[:search_column]) %>
<%= submit_tag 'Search' %>
</p>
<% end %>
I am using same code for number of other views because they are similar pages. I am trying to make this DRY. The values that change are "MyTable", "col1", "col2"
Everything else stays the same. What I don't understand is how do I do this in a helper method, I tried just copy and pasting and passing those values in as parameters but that does not works because helper is ruby code? not html. How would I go about creating this in the application_helper.rb? Do I build a string that pass back or is there another way because I am also using MyTable, this part confuses me the most.
Upvotes: 0
Views: 58
Reputation: 1313
y suggestion would be to keep this whole view file as a partial. and instead of MyTable, col1 and col2 use some variables. You can render this partial with different values of those variables from controllers.
All you have to do is use render_partial with parameters as those three variables.
Create a partial.(mypartial) shown as below
<%= form_tag all_path, :method => 'get' do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<p>
Search:
<%= text_field_tag :search %>
by
<%= select_tag :search_column, options_for_select(my_table.constantize.translated_searchable_columns([col1, col2]), params[:search_column]) %>
<%= submit_tag 'Search' %>
</p>
<% end %>
now where ever you are calling this use render partial and pass the three parameters col1 col2 and my_table. It will look something like :
render :partial => 'mypartial', :locals => {:my_table => "ModelName", :col1 => 'col1', :col2 => 'col2'}
By that way you can send in your model name and col variables dynamically. BTW just curious, was this defined as a methods before ?? asking because of the use of params hash. You might have to sent those params hash in :locals too.
Upvotes: 3