2011-03-01 35 views
0

我需要提供一個沒有AR模型支持的表單。 (我知道這個問題已經以各種方式被問了幾十次,但儘管大量閱讀了實驗,但我仍然無法做到。)RoR:使用沒有AR模型的form_tag

描述我需要的最簡單的方法是通過逆向工程:假設我想要一個帶有「用戶名」字段和「訪問碼」字段的表單。然後我想[提交]按鈕調:

ServicesController#update 

與params哈希表設置爲(至少):

params={"service"=>{"credentials"=>{"username"=>"fred", "accesscode"=>"1234"}}, "commit"=>"update credentials", "action"=>"update", "controller"=>"services", "id"=>"54"} 

或者只是

params={"credentials"=>{"username"=>"fred", "accesscode"=>"1234"}, "commit"=>"update credentials", "action"=>"update", "controller"=>"services", "id"=>"54"} 

,其中 '54'是我想要更新的服務對象的ID。 (我的update()方法會將憑證從params散列中取出並與它們一起做正確的事情。)(我沒有顯示路由,但我不確定這與此有關。)

但是我還沒有想出如何獲得form_tag或form_for來開始我的投標。建議?

更新 根據下面的光圈建議,form_tag似乎是正確的。我收到路由錯誤'沒有路由匹配「/ services/54」'。我去改變我目前的路線之前,我目前有:

resources :premises, :shallow => true do 
    resources :services 
end 

這給了我(部分):

edit_service GET /services/:id/edit(.:format) {:action=>"edit", :controller=>"services"} 
    service GET /services/:id(.:format)  {:action=>"show", :controller=>"services"} 
      PUT /services/:id(.:format)  {:action=>"update", :controller=>"services"} 
      DELETE /services/:id(.:format)  {:action=>"destroy", :controller=>"services"} 

回答

1

的routes.rb

 
match '/services/update/:id', :to => 'services#update', :as => 'update_service' 

那麼你的表格:

 
- form_tag @service, :url => update_service_path do 
    = text_field :credentials, :userid, 'value' => 'fred' 
    = text_field :credentials, :accessid, 'value' => '1234' 

然後在你的控制器中:

 
def update 
    @service = Service.find_by_id(params[:id]) 
    @service.update_attribute('userid', params[:credentials][:userid]) 
    @service.update_attribute('accessid', params[:credentials][:accessid]) 
end 

我不知道服務對象是什麼,如果它不是模型。所以我不知道用id來找到服務需要什麼方法,如果不是模型,update_attribute調用可能不起作用,但很難說沒有更多信息

這完全沒有經過測試。但希望是接近的工作...

+0

@aperture:也許關閉。例如,form_for產生了「未定義的方法'merge:for userid:Symbol」錯誤。我發現text_field()需要一個散列作爲其第三個參數(或者第二個,當你通過form.text_field()調用它時) - 是你想要的嗎? – 2011-03-01 22:18:41

+0

@aperture:我應該提到Service *是一個完整的ActiveRecord模型。這裏的練習是創建一個表單來編輯/更新與Service相關的輔助數據*,而不是模型本身的一部分。 – 2011-03-01 22:26:28

+0

應該是form_tag,我的錯誤。檢查上面的編輯 – aperture 2011-03-01 22:41:57

1

我給對號@aperture,但這裏是一個基於光圈的指導下最終版本後,已被應用於所有的調整:

<%= form_tag @service, :url => service_path(@service), :method => :put do %> 
    <%= text_field :service, 'credentials[userid]' %> 
    <%= text_field :service, 'credentials[accessid]' %> 
    <%= submit_tag 'update credentials' %> 
<% end %> 

點擊[提交]按鈕結束調用服務#update()方法與params哈希表設置爲:

params={... "_method"=>"put", "service"=>{"credentials"=>{"userid"=>"xxx", "accessid"=>"xxx"}}, "commit"=>"update credentials", "action"=>"update", "controller"=>"metered_services", "id"=>"54"} 

注意,我從@光圈的版本修改了text_field ARGS - 這種做法包裹憑據自己的憑據散列,因此Service#更新方法可以是規範:

def update 
    @service = Service.find(params[:id]) 
    @premise = @service.premise 
    if (@service.update_attributes(params[:service])) 
    redirect_to edit_premise_path(@premise), :notice => 'info was successfully updated' 
    else 
    redirect_to edit_premise_path(@premise), :error => 'could not update information' 
    end 
end 

...並且任何特殊的憑證處理都駐留在服務模型中(應該如此)。