I wanted to get some advice on how I can refactor some code to make it more DRY, as Rails is very much about DRY. In my controller I have some methods like this:
def filter
if params[:status] == "All" && params[:department] == "All"
redirect_to admin_organizations_path
elsif params[:status] != "All" && params[:department] == "All"
redirect_to admin_organizations_path(status: params[:status])
elsif params[:status] == "All" && params[:department] != "All"
redirect_to admin_organizations_path(department: params[:department])
else
redirect_to admin_organizations_path(status: params[:status], department: params[:department])
end
end
def filter_manage
if params[:status] == "All" && params[:department] == "All"
redirect_to manage_organizations_path
elsif params[:status] != "All" && params[:department] == "All"
redirect_to manage_organizations_path(status: params[:status])
elsif params[:status] == "All" && params[:department] != "All"
redirect_to manage_organizations_path(department: params[:department])
else
redirect_to manage_organizations_path(status: params[:status], department: params[:department])
end
end
def filter_index
if params[:department] == "All"
redirect_to organizations_path
else
redirect_to organizations_path(department: params[:department])
end
end
def filter_user
if params[:department] == "All"
redirect_to user_organizations_path
else
redirect_to user_organizations_path(department: params[:department])
end
end
As you can see, the only real difference is where they are redirecting back to, how can make this a into a reusable piece of code that intelligently knows where to redirect back to given the view its coming from, because these methods are just redirecting back to the view they came from.
Any help is greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire