I have two different plans (plan with an ID of 1 and plan with an ID of 2). I've created a partial for each to show on the home page based on which plan the user is logged in as. I would like to create two different index pages for each plan to be directed to.
Plan ID 1 users need to be directed to an index of users with a plan ID of 2, and plan ID 2 users need to be directed to an index of users with a plan ID of 1. Here's the part of the code that controls this feature. How can I create an index feature which sends plan ID 1 and plan ID 2 user to different pages after click on the relative partial?
pages/home.html.erb
<div class="col-md-6">
<% if current_user.plan.name == "mentor" %>
<%= render partial: "pages/mentor" %>
<% else %>
<%= render partial: "pages/mentee" %>
<% end %>
</div>
pages/_mentee.html.erb
<div class="well">
<h2 class="text-center">Mentor Community</h2>
<h4 class='text-center'>Get the support you need.</h4>
<br><%= link_to "Find a Mentor", "#", class: 'btn btn-default btn-lg btn-block' %>
</div>
pages/_mentor.html.erb
<div class="well">
<h2 class="text-center">Mentee Com</h2>
<h4 class='text-center'>Give the support that's needed.</h4>
<br><%= link_to "Find a Mentee", "#", class: 'btn btn-default btn-lg btn-block' %>
</div>
Do this:
#config/routes.rb
root "pages#home"
resources :plans, only: :show #-> url.com/plans/1
#app/controllers/plans_controller.rb
class PlansController < ApplicationController
def show
@plan = Plan.find params[:id]
end
end
#app/views/plans/show.html.erb
<% @plan.users.each do |user| %>
<%= user.name %>
<% end %>
after click on the relative partial
You could send users to the specific plan page by using the following:
#app/views/pages/home.html.erb
<% @plans.each do |plan| %>
<%= link_to plan.id, plan %>
<% end %>
There is so much more you need to consider, but for now, the above should help you get an understanding on the overall structure.
Let me explain a little on how you need to adapt your thinking (this might seem off topic, but will help you profusely, I guarantee it)...
The above is how Rails is meant to work -- it takes a request from the user, matches it to a controller action, and then populates it with model data.
The correct way for Rails to work is with something called object orientated programming - each time you initiate an action / request, it has to invoke & manipulate objects of data.
Whilst this may seem complicated, the sooner you get your head around it, the quicker you'll be able to make much more intricate rails applications.
--
Your question implies that you've not considered the full potential of a data-driven rails application.
Not that it matters, but if you changed your approach so that you got rid of pages
and instead had plans
with users
, you'd be able to have as many plans as you required.
That is the correct way to think about programming (to make a system , not just a quick fix), which you can then use to expand the functionality as you need.
A good way to remove the conditional would be using the plan name to find the partial. It would be something like this
<div class="col-md-6">
<%= render partial: "pages/#{current_user.plan.name}" %>
</div>
But you need to make sure that each plan has its correct partial file. Otherwise you'll get a rendering error, because the partial isn't found.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.