2014-10-27 30 views
0

我在我的應用程序中有一個命名空間管理員,在這裏你可以刪除一個客戶端,這很好。 在應用程序的管理員之外,@current_user有機會編輯客戶的地址。命名空間管理之外的更新記錄[rails]

我不知道如何訪問它並將新更新保留到數據庫。這是我迄今爲止

products_controller.rb(其中部分是從渲染)

class ProductsController < ActionController::Base 
    layout "application" 
    def index 
     @products = Product.all 
    end 

    def show 
     @products = Product.all 
     @current_user = Client.find_by(id: session[:client]) 
    end 
end 

_overlay_checkout.html.erb(此填充與客戶端的形式從數據庫地址,如果其沒有更新,他們可以改變它。)

<%= simple_form_for([:admin, @current_user], :url => edit_admin_client_path) do |f| %> 
     <%= f.text_field :address %><br /> 
     <%= f.text_field :address_line_2 %><br /> 
     <%= f.select(:state, options_for_select(us_states, "CA")) %> <br /> 
     <%= f.text_field :zipcode %><br /> 
     <%= f.text_field :city %><br /> 
     <%= f.submit %> 
    <% end %> 

IM不確定如何更新此記錄,...沒有在應用程序的管理部分是,..任何幫助,將不勝感激。

回答

0

我在這種情況下所做的一件事是在我的控制器操作中指定@submit_url作爲實例變量。

class ProductsController < ApplicationController 
def show 
    @submit_url = client_path(@client) 
end 
end 

您可以可以將此變量傳遞給你的form_for調用就像這樣:

<%= simple_form_for([:admin, @current_user], :url => @submit_path) do |f| %> 
    <%= f.text_field :address %><br /> 
    <%= f.text_field :address_line_2 %><br /> 
    <%= f.select(:state, options_for_select(us_states, "CA")) %> <br /> 
    <%= f.text_field :zipcode %><br /> 
    <%= f.text_field :city %><br /> 
    <%= f.submit %> 
<% end %> 

如果@submit_url不是零則表單動作將被否則將其設置爲@submit_url的價值將設置爲您指定的基於資源的路線。這意味着在管理控制器中根本不需要改變。只需要備用控制器(在本例中爲products_controller),您希望將表單提交到除自動生成的足智多謀之外的其他位置。