2013-02-10 69 views
0

好日子,我有這種形式的看法/ startseites/index.html.erb,我指定了我的people_controller方法,她沒有去。我準備了一些用於理解的幽靈代碼。我想通過button_to標記將下拉列表中的條目賦予控制器操作checkValid。在那裏我想驗證對數據庫的條目。我必須從桌子上讀書寫字。我希望它清楚。Rails:DropDown選擇到控制器行動

class PeopleController < ApplicationController 


    def checkValid 
     @trainerName = params[:trainer_name] 
     @sportlerName = params[:sportler_name] 
     @trainerPID = params[:trainer_pid] 
     @sportlerPID = params[:sportler_pid] 

     #checks if sportlerID is null 
     @person = Person.find(params[:sportler_pid]) 
     id = Person.sportler_id 
     if id = nil then 
      Person.sportler_id = params[:trainerPID] 
     else 
      puts "Sportler can have only one Trainer!" 
     end 

    end 
    ... 

視圖/ startseites/index.html.erb:此代碼犯規去

這應該送下拉選擇控制器動作checkValid()。我怎樣才能使用參數?

<%=button_to("Zuordnung erstellen", :action => "checkValid", :controller =>"people" %>** 

<table> 
    <tr> 
    <td colspan="3"> 
     Jeder Sportler kann ein Trainer haben. </br> 
    </td> 
    </tr> 
    <tr> 
     <td>Trainer</td> 
     <td> 
      <%= collection_select(:trainer, :trainer_id, Trainer.all, :id, :name) %> 

     </td> 

     <td> 
     <%= link_to 'Neuer Trainer', new_person_path(:type => "Trainer") %> 
     </td> 

    <tr> 
    <tr> 
     <td>Sportler</td> 
     <td> 
      <%= collection_select(:sportler, :sportler_id, Sportler.all, :id, :name) %> 

     </td> 
     <td> 
     <%= link_to 'Neuer Sportler', new_person_path(:type => "Sportler") %> 
     </td> 
    <tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td> 
     **<%=button_to("Zuordnung erstellen", :action => "checkValid", :controller => "people") %>** 
      </td> 
     <tr> 


    </table> 

我增加這一行到我的路由

match '/people/checkValid', :controller => 'people', :action => 'checkValid' 

但:沒有路由匹配{:控制器=> 「人/ checkValid」:方法=>:checkValid}

不,我認爲它不過

模板是缺失

Missing template people/checkValid, application/checkValid with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/Users/jord/testmood/app/views" 

回答

1

Missing template錯誤是指缺少的視圖。您應該在app/views/people/目錄中有check_valid.html.erb視圖文件。

另外,在應用程序目錄中的任何位置的命令行上運行rake routes。您將收到由您的routes.rb文件生成的列表路由。如果存在people#checkValid,您可以仔細檢查。

順便說一下,您可能需要將checkValid更改爲check_valid,以便您遵循Rails中操作的命名約定。

+0

好的謝謝。現在它走了,但我想我需要button_tag而不是button_to,因爲我想驗證一些值。那裏因爲我不需要一個單獨的形式。我做了這個<%= button_tag「Zuordnung」,:controller =>: :action =>'check_valid', :trainer_id = :trainer_name =>「TEST」, :method => post%>和我的控制器操作 – ubuseral 2013-02-10 15:22:24

+0

def check_valid flash.now [:notice] ='發送消息!' respond_to do | format | format.html#index.html.erb format.json {render json:@people} end end但無響應 – ubuseral 2013-02-10 15:23:08

+0

這是正確的。你需要'button_tag'。 Rails中沒有'button_to'。當你不確定時,你可能想引用[link](http://api.rubyonrails.org)。 我猜你想要做的是檢查是否params正在通過?爲此,您可以檢查開發日誌,或在控制器將重定向到的視圖中包含「<%= params debug%>」。 您可以將'redirect_to root_path'添加到您的'people#check_valid'動作中。將我前面提到的'params debug'行添加到根路由的視圖中。 – Gjaldon 2013-02-11 05:40:20