how to redirect_to a custom html file
-
07-03-2021 - |
Question
I have a link that I want users to press and it will go to a custom view (new.worker.html.erb) inside the absences object how do I do this?
I currently have a link_to a method inside my controller called render which checks the user's session for staff_type to decide where the user should be redirected to
edit: as pointed out by the commenters the answer is to call the relevant controller to your view in your link_to, then perform the check for the right render inside the controller like so:
if session[:user].staff_type == 3
render "new_worker"
which points to the new_worker view inside the relevant view
Solution
Ok, so, different staff members are different staff types, right? and worker is one of them?
What I would probably do is in your staffs controller, in show -- which is where I assume you want to render a different template, when you're looking at a particular person who has a particular staff_type -- is to find the Staff Member, and then look at their :staff_type attribute, and do an if or a case to render the corresponding partial.
def show
@staff = Staff.find(params[:id])
case @staff.staff_type
when "worker"
render :action => "show", :layout => "worker"
else
#other options
end
end
The above renders for the show action (passes those variables) but with a custom layout called worker.
Or, instead of a render as above, I'd recommend using the same skeleton and then rendering a different partial for each different staff type, using this render:
render :partial => "worker", :object => @staff
In that case, the view would be called probably _worker.rhtml
See info here: http://rails.rubyonrails.org/classes/ActionController/Base.html#M000464
You can use the above in any controller action, really. But you need to do this in a controller, or else re-organize your resources.
OTHER TIPS
It is not clear at all, give us a bit more information what are absence, staff_type, worker ? do you have route, resource and controller for all these things, are they linked through relations ?
The basic answer would be : With rails, in order to render a template you need a controller and a route to access the controller. then a quick redirect_to new_worker_path
should work
EDIT : routes point to actions, which render views (or other http response such as redirect_to).
Rails (I'm guessing yours is 2.3.11), is a REST, resource oriented framework. IMHO you should keep this, and redirect to new_absence_path, if you want to have a different template for a staff_type, you can add an argument to your link :type=>'worker'
and use this in your controller to render a specific template.
the not best practice way, is to create a route and an action. for rails 2.3.11
map.resources :staff, :member => {:new_worker => :get}
then define in your staff controller :
def new_worker
...
render 'new.worker'
end