2012-10-15 45 views
1

我有索引頁users_controller呼籲採取行動:如何從的link_to

def index 
    @companies = Company.where(:is_confirmed => "f") 
    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @companies } 
    end 
    end 

,我想在一個按鈕的觸摸,該公司改變了狀態,以證實

def confirm 
    company = Company.find(params[:id]) 
    company.is_confirmed = "t" 
    company.save 
    redirect_to users_path 
    end 

按鈕,應該調用確認

= link_to '<i class="icon-ok icon-white"></i> '.html_safe + t('Confirm'), users_path, confirm: t('Are you sure'), :controller => "users", :action => "confirm", :class => 'btn btn-small btn-success' 

請告訴我如何解決或告訴我你在哪裏可以看到一個工作版本

回答

3
= link_to confirm_company_path(company), confirm: 'Are you sure', method: :post do 
    %i{class: "icon-ok icon-white"} 
    = t('Confirm') 

在routes.rb中

post '/company/:id/confirm' => "users#confirm", as: :confirm_company 

1)當您更改對象不要使用GET請求,請使用POST來代替。

2)將確認邏輯移至公司模型並確認對公司的操作控制器

+0

非常感謝你幫我 – Nar

+0

更改爲= link_to''.html_safe + t('確認'),confirm_company_path(公司),確認:t('你確定'),:class =>'btn btn-small btn-success'並且已經工作 – Nar

1

您必須在controller/action/id參數和RESTful路由之間進行選擇,請檢查rails api。你可能希望這樣的:

= link_to '<i class="icon-ok icon-white"></i> '.html_safe + t('Confirm'), :controller => "users", :action => "confirm", :id => @companies, method: :post, confirm: t('Are you sure'), :class => 'btn btn-small btn-success' 

= link_to '<i class="icon-ok icon-white"></i> '.html_safe + t('Confirm'), confirm_users_path(@companies), method: :post, confirm: t('Are you sure'), :class => 'btn btn-small btn-success' 

暗示你的路線這個樣子的(REST風格):

resources :users do 
    post 'confirm' 
end 

尤里Barbashov是正確的,後做很多更有意義在這裏。

+0

您可能需要檢查耙路線。 – igwan

+0

感謝igwan,項目獲得 – Nar