2014-04-04 3 views
0

일반 사용자가 로그인하면 대시 보드에있는 belong_to에 대한 서비스 요청을 표시하는 대시 보드에 들어갑니다.레일 4 : 중첩 된 리소스가있는 form_for

관리자가 로그인하면 로그인하고 서비스 요청을 제출할 수있는 회사 로고를 모두 표시하는 대시 보드로 삭제됩니다.

일반 사용자와 관리 사용자 간의보기는 양식의 한두 개의 엔터티 (cancan을 통해 제어 됨) 외부에서 사실상 동일합니다. 그래서 관리자가 SR을 작성하거나 일반 사용자가 SR을 작성하는 경우 동일한 양식을 사용할 수 있도록 노력하고 있습니다.

Routes.rb : 회사 로고에 관리자가 로그인 및 클릭이 새로운 SR을 만들 클릭하면

resources :service_requests do 
    resources :notes 
    end 

    namespace :admin do 
    get '', to: 'dashboard#index', as: '/' 

    resources :companies do 
     resources :service_requests, only: [:index, :new] 
    end 
    end 

는 경로 /admin/companies/1/service_requests/new입니다. 일반 사용자가 로그인하면 /service_requests/new입니다. 나는 관리자 및 비 관리자 측면 모두에서 동일한 양식을 재사용하는 방법에 대해 약간 혼란스러워합니다. 나는 ServiceRequestsController

+0

생성은'admin/companies/1/service_requests' 경로에 의해 처리되지 않습니까? –

+0

@ManojMonga - 관리자보기에 있는지, 일반 사용자보기에 있는지에 따라 경로가 다르므로 어떻게 될지 모르겠다. – dennismonsewicz

답변

0

내 생성 자원의 SR에 company_id을 설정하고 있기 때문에 내가 좋아하는 뭔가를 할은 An admin에서 동일한 양식을 사용하고 not admincancan

#models/ability.rb 
class Ability 
    include CanCan::Ability 

    def initialize(user) 
    if user.is_admin? #replace this by something that returns true if logged-in user is admin 
     can :generate_this_form_fields, User 
     #^normally can -> points to a method in the controller, but you can 
     # create one even if you're not validating any method, just so you could use it anywhere 
    end 
    end 
end 

를 사용하려면 다음 뷰에있는 동안 보기에, 양식이고 사실

#views/form.html.erb 
<%= form_tag ... %> 
    <% if can? :generate_this_form_fields, User %> 
    This is where the fields are displayed if admin account 
    <% else %> 
    This is where the fields are displayed if not admin 
    <% end %> 
<% end %> 

같은 파일, 당신은 사용할 수

<% if current_user.is_admin? %> 

그리고하지

<% if can? :generate_this_form_fields, User %> 

하지만 난 당신이 해결하려고하는지에 맞게 할 수있는 방법으로 예를했습니다. 희망이 도움이됩니다. 건배!

관련 문제