If the plan numbers are in the database and there is a Plan
model then you could simply say something like:
@plan = Plan.find_by(:id => params[:plan])
if(!@plan)
flash[:notice] = "Please select a membership plan to register."
redirect_to root_url
end
Now you have access to the full plan details for the next view so that you can show them the name, description, price, ... and the existence of a plan is stored in only one place (i.e. the database). If you don't need @plan
then you could say:
if(!Plan.where(:id => params[:plan]).exists?)
...
end
The important point is that there should be exactly one thing that knows what the valid plans are and any time you need to know about plans, you ask that one thing and only that one thing.
The view that ends up calling select_plan
would also use the database (instead of the literal numbers one through eight) to get the valid plans:
<% Plan.order(...).each do |p| %>
whatever you need to display the plan as an option...
<% end %>
Add a new plan to the database and everything still works. Remove/disable a plan and everything still works. Your software will be easier to maintain, have fewer bugs, be easier to understand, and you'll have gained a new good habit rather than a bad one.
select_plan
. Fix the underlying problem andselect_plan
will clean itself up as a side effect. - mu is too short