2011-06-02 57 views
0

我的控制器管理問題的軌道+ JSON

def index_all_for_question_id 

     @answers  = Question.find(params[:id]).answers 


    respond_to do |format| 
     format.ext_json { render :json => @answers.to_ext_json(:class => Answer, :include => [:respondent]) } 
    end 
    end 

我在ExtJS的數據存儲中

var answers_datastore = new Ext.data.Store({ 
    autoLoad: true, 
    proxy: new Ext.data.HttpProxy({ 
     url: '/answers/index_all_for_question_id/<%= @questions.first.id %>?format=ext_json', 
     method: 'GET'}), 
     reader: answers_reader 
     }); 

我的問題是:當我試圖進入 localhost:3000/answers/index_all_for_question_id/551?format=ext_json例如,

我得到:

{"results":2,"answers":[{"answer":{"scale":1,"inquiry_id":277,"created_at":"2011-05-30T07:10:22Z","updated_at":"2011-05-30T07:10:22Z","text":"dfgfdghfdhfdh","id":275,"respondent":{"created_at":"2011-05-16T06:47:08Z","updated_at":"2011-05-16T06:47:08Z","id":109,"user_id":6,"email":"[email protected]"}}},{"answer":{"scale":1,"inquiry_id":278,"created_at":"2011-05-31T12:33:36Z","updated_at":"2011-05-31T12:33:36Z","text":"lolololol","id":290,"respondent":{"created_at":"2011-05-25T11:22:55Z","updated_at":"2011-05-25T11:22:55Z","id":110,"user_id":6,"email":"[email protected]"}}}]}

所以我不知道它!每個用戶都可以進入這一行,我可以得到答案(如果他不是管理員)。我能如何解決這個問題?只有管​​理員可以看到這一點,因爲其他人應該在某個頁面上重定向或出現文本「對不起!」。

謝謝

+0

-1這是很不清楚的問題。我在這裏看不到JSON的任何問題。我想你只想讓管理員訪問這個方法,對吧?管理員目前如何進行身份驗證?管理員= 0在網址是一個非常糟糕的認證想法。在這種情況下,我建議使用HTTP身份驗證(它是內置的Rails的一部分):http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.html – 2011-06-02 09:43:37

+0

感謝您的幫助。是的exatcly,buuuuut我想要的:如果用戶輸入的網址,我張貼,所以他被重定向到另一個頁面。管理員可以。我不知道如何去做,我已經閱讀過你給我看的網址上的這些東西,但仍然沒有任何東西 – 2011-06-02 10:11:44

+0

這對HTTP身份驗證實施有點棘手。看到我下面發佈的答案。 – 2011-06-02 11:57:18

回答

0

您好我認爲,最簡單的解決方法是編寫自定義before_filter在那裏你可以檢查,如果用戶具有管理員角色,並重定向到一些網頁,如果沒有也可以用CanCan進行授權

+0

是啊,謝謝bt,如果我有這個url:http:// localhost:3000/respondents?format = ext_json&_dc = 1306937167419&start = 0&limit = 50&fields = [%22 respondent [email]%22%2C%22 respondent [created_at]%22] &query =,如何在RoR我可以說如果管理員,我可以看到這個網址,如果管理員= 0,重定向,??這是在url中的高級查詢:( – 2011-06-02 09:02:13

+0

prams字符串真的很長:)但是,如果我有你的權利用戶應該無法看到頁面,如果特定的參數存在所以問題是 - 什麼參數?管理員應該看到用戶不應該使用哪些參數(在url中)? – Bohdan 2011-06-02 15:12:18

0

我會用HTTP authentication。重定向實施起來有點棘手,下面是一個例子(使用會話):

class AdminController < ApplicationController 
    before_filter :authenticate 

    USER_NAME = "admin" 
    PASSWORD = "xyz" # PS: you should use hashed passwords 

    def admins_only 
    render :text => "TOP SECRET STUFF!" 
    end 

    protected 

    def authenticate 
    if authenticate_with_http_basic { |u,p| u == USER_NAME && p == PASSWORD }  
     true 
    else 
     if session[:http_auth_requested] 
     session[:http_auth_requested] = nil 
     redirect_to '/sorry_page' and return false 
     end 

     session[:http_auth_requested] = 1 
     request_http_basic_authentication 
    end 
    end 
end