Rails 4: form_for with nested resource and without -


in app, when regular user logs in, dropped in on dashboard displays service requests company belong_to.

when admin logs in, dropped onto dashboard displays of company logos can login , file service requests.

the views between regular user , admin user virtually exact same, outside of 1 or 2 entities on form (which controlled via cancan). trying able use same form if admin creates sr or regular user creates sr.

routes.rb:

  resources :service_requests     resources :notes   end    namespace :admin     '', to: 'dashboard#index', as: '/'      resources :companies       resources :service_requests, only: [:index, :new]     end   end 

if admin logs in , clicks on company logo , clicks create new sr, route /admin/companies/1/service_requests/new. if regular user logs in is, /service_requests/new. confused on how reuse same form both admin , non-admin side. because setting company_id on sr in create resource in servicerequestscontroller

i following use same form on admin , not admin using cancan

#models/ability.rb class ability   include cancan::ability    def initialize(user)     if user.is_admin? #replace returns true if logged-in user admin       can :generate_this_form_fields, user       # ^ can -> points method in controller, can       # create 1 if you're not validating method, use anywhere     end   end end 

while on view file form is, like

#views/form.html.erb <%= form_tag ... %>   <% if can? :generate_this_form_fields, user %>     fields displayed if admin account   <% else %>     fields displayed if not admin   <% end %> <% end %> 

actually in view, use

<% if current_user.is_admin? %> 

and not

<% if can? :generate_this_form_fields, user %> 

but i've made example in such way might fit you're trying solve. hope helps. cheers!


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -