2014-06-20 43 views
0

Ruby on Rails的新手和我有一個顯示頁面,其中包含從新頁面填寫的信息,包括允許或拒絕請求表單的單選按鈕。我希望每個能夠使用已設置的ldap身份驗證登錄的人都能夠查看顯示頁面信息。但我只需要兩個用戶,即管理員,可以看到單選按鈕以允許或拒絕請求表單。我使用了許多不同的教程,我在網上看到的類似問題並不能真正幫助我。 is_admin?在articles_controllers中的方法不是一個真正的方法,並希望能有所幫助。使用其ldap憑證用戶名登錄的用戶採用「john_smith」形式。我提前爲我糟糕的編碼技能道歉。如果有任何額外的信息需要我可以很快提供。Ruby on Rails設置兩個用戶作爲管理員w/ldap身份驗證

這是顯示頁面的代碼。應用程序\意見\文章\ show.html.erb

<!DOCTYPE html> 
<html> 
<head> 
<style> 
p.inq 
{ 
    width:400px; 
    word-wrap:break-word;; 
} 
</style> 

</head> 
<body> 
<div id="logo"></div> 
<center> 
<h1> Submitted Request </h1> 
<table><tr><p> 

<td><strong>Approval Status:</strong></td> 
<td> &nbsp &nbsp &nbsp  </td> 
<td><%= @article.approve %></td></p></tr> 
    <p><tr><td><strong>Name:</strong></td> 
    <td> &nbsp &nbsp &nbsp  </td> 
    <td><%= @article.name %></td> 
    </p></tr> 
    <tr><p> 
    <td><strong>Email:</strong></td> 
    <td>  </td> 
    <td><%= @article.email %></td> 
    </p></tr> 
    <tr> <p> 
    <td><strong>Phone Number:</strong></td> 
    <td>  </td> 
    <td><%= @article.phone_number %></td> 
    </p></tr> 
    <tr><p> 
    <td><strong>Department Name</strong></td> 
    <td>  </td> 
    <td><%= @article.dept_name %></td> 
    </p></tr> 

     <tr><p> 
    <td><strong>Desc. of Business Need:</strong></td> 
     <td>  </td> 
    <td><%= @article.dob %></td> 
    </p></tr> 

    <tr><p> 
    <td><strong>Desc. of Changes to Firewall:</strong></td> 
    <td>  </td> 
    <td><%= @article.doc %></td> 
    </p></tr> 
    <tr><p> 

    <tr><p> 
    <td><strong>Additional Information(NAT's, VIPS's, Servers, etc.:</strong></td> 
    <td>  </td> 
    <td><%= @article.info %></td> 
    </p></tr> 

    <tr><p> 
    <td><strong>Inquiry:</strong></td> 
    <td>  </td> 
    <td><p class="inq"><%= @article.inquiry %></p></td> 
    </p></tr></table> 
    <%= :username %> 
    <% if is_admin? %> 

    <%= form_for :article, :method => :patch, url: article_path(@article) do |f| %> 
    <%= f.radio_button :approve, 'Approved' %> 
    <%= f.label :approve, 'Approve Request', :value => 'Approved' %> &nbsp &nbsp 
    <%= f.radio_button :approve, 'Denied' %> 
    <%= f.label :approve, 'Deny Request', :value => 'Denied' %>&nbsp &nbsp 
    <%= f.radio_button :approve, 'Unapproved' %> 
    <%= f.label :approve, 'Keep Request Unapproved', :value => 'Unapproved' %><br><br> 
    <%= f.submit "Submit" %><br> 
<% end %><% end %> 

    <%= link_to 'Back', articles_path %> 
    </center></body> 

應用程序\型號\ user.rb

class User < ActiveRecord::Base 

    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
      :recoverable, :rememberable, :trackable, :validatable, :omniauthable 

    def self.generate_random_password 
     Digest::SHA1.hexdigest(Time.now.to_s) 
    end 

    def create 
     User.create(user_params) 
    end 
    private 
    def user_params 
     params.require(:user).permit(:firstname, :lastname, :displayname, :username, :email,  :password, :password_confirmation, :remember_me) 
    end 
    end 

應用程序\控制器\ articles_controllers.rb

class ArticlesController < ApplicationController 
    def new 
    @article = Article.new 
    end 

    def show 
    @article = Article.find(params[:id]) 
    end 

    def create 
    @article = Article.new(article_params) 

    respond_to do |format| 
    if @article.save 
      FormMailer.confirmation_email(@article).deliver 
      AdminMailer.confirmation_email(@article).deliver 
      format.html { redirect_to @article, notice: 'successful' } 
      format.json { render :show, status: :created, location: @article } 
     #redirect_to @article 
     else 
     format.html { render :new } 
     format.json { render json: @article.errors, status: :unprocessable_entity } 
     #render 'new' 
    end 
    end 
    end 

    helper_method :is_admin? 
    def is_admin? 
     notsure == 'john_smith' 
    end 

    def index 
    @articles = Article.all 
    end 

    def edit 
    @article = Article.find(params[:id]) 
    end 

    def update 
    @article = Article.find(params[:id]) 

    if @article.update(art_param) 
     redirect_to root_path 
    end 
    end 

    private 
    def article_params  
     params.require(:article).permit(:name, :email, :phone_number, :dept_name, :doc,    :inquiry, :dob, :info, :time) 
    end 

    def art_param 
    params.require(:article).permit(:approve) 
    end 

    end 

應用程序\意見\ devise \ sessions \ new.html.erb

<center><h2>Request Form - Sign In</h2> 
    <% if user_signed_in? %> 
     Welcome <%= current_user.email %> (<%= link_to "logout", destroy_user_session_path,    :method => :delete %>) 
    <% else %> 
     You are currently not logged in, <%= link_to "Log In Here",  user_omniauth_authorize_path(:ldap) %> 
    <% end %> 

    <!-- <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> 
    <div><%= f.label :username %><br /> 
    <%= f.text_field :username, autofocus: true %></div> 

    <div><%= f.label :password %><br /> 
     <%= f.password_field :password, autocomplete: "off" %></div> 

    <% if devise_mapping.rememberable? -%> 
     <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div> 
    <% end -%> 

    <div><%= f.submit "Sign in" %></div> 
    <% end %> 

    <%= render "devise/shared/links" %>--> 
    </center> 

回答

0

首先,你wa nt你的is_admin?方法在用戶能夠激活控制器中的任何動作之前觸發。這意味着,它應該是一個before_filter而不是輔助方法。現在,你可以填寫你的is_admin?方法,因此如果用戶是管理員,它可以做你想做的事情。這裏有一個例子說明它如何爲你工作(下面,只有管理員可以使用新的和創建方法)。您可以根據需要對其進行修改,如果用戶嘗試激活這些方法,則會將其重定向到其他位置或者收到警報。

before_filter :is_admin? , only: [:new, :create] 

def is_admin? 
return current_user 
end 
相關問題