2013-05-18 110 views
-2

我搭建了一個授權(user_id,code,otherparam1,...),並且我想將一個custom_create方法添加到authorizations_controller,它只需要一個代碼,該方法生成其他參數。我需要這個方法使用JSON工作或其他方法,比如User.newcustom(代碼)Ruby自定義創建方法

def newcustom 
    case request.method 
    when :post #post 
    code = params[:code] 
    if(code and code != '') 
     ... 
     respond_to do |format| 
     if @authorization.save 
     format.html { redirect_to @authorizations, notice: 'Authorization was successfully created.' } 
     format.json { render json: @authorizations, status: :created, location: @authorization } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @authorization.errors, status: :unprocessable_entity } 
     end 
    end 
    else #get 
    respond_to do |format| 
     format.html 
    end 
    end 

叫這裏是我的newcustom.html.erb

<%= form_tag(newcustom_authorizations_path, :method => :post) do %> 
    <div class="field"> 
    <%= text_field_tag :code %> 
    </div> 
    <div class="actions"> 
    <%= submit_tag('Get tokens') %> 
    </div> 
<% end %> 

但它不通過JSON工作, 表格。並調用newcustom(:code => code)方法會引發太多參數(1代表0)。任何想法 ?

+0

你在哪裏叫newcustom? –

+0

這不是PHP,老兄!您需要RESTful操作。 – MikDiet

+0

向我們顯示錯誤消息/堆棧跟蹤和錯誤消息中引用的代碼。 –

回答

0

該附加給定的代碼到config/routes.rb中

resources :authorizations do 
    collection do 
     post :newcustom 
    end 
    end 
+0

已經將我的routes.rb – Lex

0

感謝大家的幫助。

我最終解決了我的問題,過濾create函數的參數,然後調用我的customcreate函數。 並且我是CHEAT :)