【问题标题】:Rails: DropDown Selection to Controller ActionRails:下拉选择到控制器动作
【发布时间】:2013-02-10 14:23:58
【问题描述】:

美好的一天,我有这个表单 view/startseites/index.html.erb 并且我在我的 people_controller 中指定了一个方法,她没有去。我准备了一些幽灵代码以供理解。我想将带有 button_to 标记的下拉列表中的条目提供给控制器操作 checkValid。我想在数据库中验证条目。我必须在桌子上读写。我希望它足够清楚。

 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
   ...

view/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'

但是:没有路由匹配 {:controller=>"people/checkValid", :method=>:checkValid}

不,我认为是这样,但是

模板丢失

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

【问题讨论】:

    标签: ruby-on-rails forms controller interaction


    【解决方案1】:

    Missing template 错误是指缺少视图。您应该在app/views/people/ 目录中有check_valid.html.erb 查看文件。

    此外,在应用目录中的任意位置的命令行上运行 rake routes。您将收到由您的 routes.rb 文件生成的路由列表。如果people#checkValid 存在,您可以在那里仔细检查。

    顺便说一句,您可能希望将 checkValid 更改为 check_valid,以便遵循 Rails 中操作的命名约定。

    【讨论】:

    • 好的,谢谢。现在它走了,但我认为我需要 button_tag 而不是 button_to,因为我想验证一些值。因为我不需要单独的表格。我这样做了 :people, :action => 'check_valid', :trainer_id => 2, :sportler_id => 2, :sportler_name => "TEST", :trainer_name => "TEST", :method => :post %> 和我的控制器操作
    • def check_valid flash.now[:notice] = '消息已发送!' respond_to 做 |格式| format.html # index.html.erb format.json { render json: @people } end end but no response
    • 没错。您需要button_tag。 Rails 中没有button_to。当您不确定时,您可能想参考link。我猜你想要做的是检查参数是否被传递?为此,您可以检查开发日志或在控制器将重定向到的视图中使用&lt;%= params debug %&gt;。您可以将 redirect_to root_path 添加到您的 people#check_valid 操作中。将我之前提到的params debug 行添加到路由根的视图中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-08-25
    • 2023-03-05
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多