Passing a variable between actions within same controller Rails -
my show action:
def show # multiple keywords if current_user.admin? @integration = integration.find(params[:id]) else @integration = current_user.integrations.find(params[:id]) end @q = @integration.profiles.search(search_params) @profiles = @q.result.where(found: true).select("profiles.*").group("profiles.id, profiles.email").includes(:integration_profiles).order("cast( translate(meta_data -> '#{params[:sort_by]}', ',', '') int) desc nulls last").page(params[:page]).per_page(20) @profiles = @profiles.limit(params[:limit]) if params[:limit] end there can many different filters taking place in here whether ransacker, params[:limit] or others. @ end have subset of profiles.
now want tag these profiles result of search query.
profiles model:
def self.tagging_profiles #some code end i'd create action within same controller show execute self.tagging_profiles function on @profiles show action given profiles have been filtered down.
def tagging @profiles.tagging_profiles end i want user able create search query, have profiles in view if satisfied tag of them, there need of form
update:
this how got around it, don't know how clean here:
def show # multiple keywords
if current_user.admin? @integration = integration.find(params[:id]) else @integration = current_user.integrations.find(params[:id]) end @q = @integration.profiles.search(search_params) @profiles = @q.result.where(found: true).select("profiles.*").group("profiles.id, profiles.email").includes(:integration_profiles).order("cast( translate(meta_data -> '#{params[:sort_by]}', ',', '') int) desc nulls last").page(params[:page]).per_page(20) @profiles = @profiles.limit(params[:limit]) if params[:limit] tag_profiles(params[:tag_names]) if params[:tag_names] end private def tag_profiles(names) @profiles.tagging_profiles end in view, created form calling self:
<%= form_tag(params.merge( :controller => "integrations", :action => "show" ), method: :get) %> <%= text_field_tag :tag_names %> <%= submit_tag "search", class: "btn btn-default"%> <% end %> is best way it?
rails public controller actions correspond http request. here there no need 2 http requests. simple solution creating private controllers methods filter_profiles(params) , tag_profiles(profiles) , phone call them sequentially.
you can extract problem exclusively serviceobject, this:
class profiletagger attr_reader :search_params def initialize(search_params) @search_params = search_params end def perform search tag end def tag #tag found profiles end def search @profiles = #do search end end as processing 30,000 records time consuming operation, create sence perform outside of rails request in background. construction allow delegate operation sidekiq or delayed_job worker ease
ruby-on-rails ruby-on-rails-4
No comments:
Post a Comment