Created
March 3, 2015 03:47
-
-
Save jordanhudgens/a69fbbf50274bade9ae5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is to be used as a process flow, not an executable implementation | |
## Dynamic Ruby View | |
# Situation: You have a very similar views that needs to be rendered, currently they are two separate models/views/controllers. | |
# This isn't an ideal setup since it means that anytime you want to make changes to one view you also need to make changes to the other view. | |
# And if they ever want more views created that are like this it will mean you need to change more, this can be bad from a time and | |
# code duplication perspective. | |
# To fix this: | |
# Step 1: Create (or convert) a controller, view and model that will handle all of the work and can be adapted to multiple use cases | |
# Step 2: In the model, have a string column that will hold the value to denote what type of view should be rendered | |
# Step 3: Setup view partials that can be called dyanamically in the view, e.g.: | |
<% if params[:foo] %> | |
<%= render 'view_one' %> | |
<% else %> | |
<%= render 'view_two' %> | |
<% end %> | |
# This will let you dynamically have the page render depending on whatever criteria you want. You can put instance variables | |
# in the new method, or you can put a hidden field or parameter on the preview page that you check check with a call to: | |
# params[:foo] and that can make the decision on which partials to render | |
# Since you need the users sent to different places after they leave this particular page, you just need to place a check in the | |
# controller, since by this will be easy because once the form is submitted you will have the ability to check the value of the | |
# new database column, you can do something like: | |
def create | |
@foo = Foo.create!(bar: "etc") | |
if @foo.new_column == "Admin" | |
redirect_to special_admin_path | |
else | |
redirect_to other_path | |
end | |
end | |
# Please let me know if this makes sense |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment