2011-03-01 49 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 

...并且任何特殊的凭证处理都驻留在服务模型中(应该如此)。