Ruby on rails 将选项从select_标记发送到Rails中的数据库

Ruby on rails 将选项从select_标记发送到Rails中的数据库,ruby-on-rails,forms,Ruby On Rails,Forms,我有一个助手,他拥有一系列组织: def pros pros = ['ASCAP', 'BMI', 'SESAC'] end 我的表格: <%= f.label :pro, "Performing Rights Organization" %> <%= select_tag(:pro, options_for_select(pros)) %> 服务器日志: Started POST "/songs" for ::1 at 2016-06-27 21:39:

我有一个助手,他拥有一系列组织:

def pros
  pros = ['ASCAP', 'BMI', 'SESAC']
end
我的表格:

<%= f.label :pro, "Performing Rights Organization" %>
<%= select_tag(:pro, options_for_select(pros)) %>
服务器日志:

    Started POST "/songs" for ::1 at 2016-06-27 21:39:06 -0400
Processing by SongsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"L70xGWxlf+eeQc2hbP/hywKdy4+TBsaKgVP7sP5edG4TSnC6Jf3wFyBjZn9/nANnoKnm6NGj0Hw6DyjsyJazug==", "song"=>{"artist"=>"The High Fives", "song_name"=>"Off Track", "writer_first_name"=>"Josh", "writer_last_name"=>"Zandman", "cleared"=>"1"}, "pro"=>"ASCAP", "commit"=>"Create Song"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "songs" ("artist", "song_name", "writer_first_name", "writer_last_name", "cleared", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["artist", "The High Fives"], ["song_name", "Off Track"], ["writer_first_name", "Josh"], ["writer_last_name", "Zandman"], ["cleared", true], ["created_at", 2016-06-28 01:39:06 UTC], ["updated_at", 2016-06-28 01:39:06 UTC], ["user_id", 1]]
   (0.8ms)  commit transaction
  Song Store (17.6ms)  {"id":2}
Redirected to http://localhost:3000/songs/2
Completed 302 Found in 47ms (Searchkick: 17.6ms | ActiveRecord: 1.5ms)


Started GET "/songs/2" for ::1 at 2016-06-27 21:39:06 -0400
Processing by SongsController#show as HTML
  Parameters: {"id"=>"2"}
  Song Load (0.1ms)  SELECT  "songs".* FROM "songs" WHERE "songs"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Rendering songs/show.html.erb within layouts/application
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Rendered songs/_song.html.erb (1.7ms)
  Rendered songs/show.html.erb within layouts/application (2.8ms)
Completed 200 OK in 277ms (Views: 275.6ms | ActiveRecord: 0.3ms)
我可以在Rails控制台中成功地将一个组织分配给:pro列,但当我尝试从表单中添加它时,它不会保存到数据库中


谢谢你的帮助

问题可能是参数哈希中不允许使用pro参数。您可以在web服务器控制台中检查提交表单时是否过滤掉了参数

假设控制器中有如下内容:

class SongsController < ApplicationController
    # Some public methods [...]
    private
    # Some private methods [...]

    # Never trust parameters from the scary internet, only allow the white list through.
    def organization_params
        params.require(:organization)
            .permit(:param1, :param2)
    end
end
select_标记没有在song中确定pro参数值的范围,因此它基本上生成:

<select name="pro">

为了实际将pro参数传递给params[:song],它需要生成:

<select name="sond[pro]">


由于select_标记未将pro传递给params[:song],因此使用params[:song]实例化song对象将不包括pro参数。

问题可能是在params哈希中不允许使用pro参数。您可以在web服务器控制台中检查提交表单时是否过滤掉了参数

假设控制器中有如下内容:

class SongsController < ApplicationController
    # Some public methods [...]
    private
    # Some private methods [...]

    # Never trust parameters from the scary internet, only allow the white list through.
    def organization_params
        params.require(:organization)
            .permit(:param1, :param2)
    end
end
select_标记没有在song中确定pro参数值的范围,因此它基本上生成:

<select name="pro">

为了实际将pro参数传递给params[:song],它需要生成:

<select name="sond[pro]">


由于select_标记没有将pro传递给params[:song],因此使用params[:song]实例化song对象将不包括pro参数。

好的,所以我知道您有解决方案,但为了将来调试此类问题的参考,您应该始终查看输出中的这一行:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"L70xGWxlf+eeQc2hbP/hywKdy4+TBsaKgVP7sP5edG4TSnC6Jf3wFyBjZn9/nANnoKnm6NGj0Hw6DyjsyJazug==",
"song"=>{"artist"=>"The High Fives", "song_name"=>"Off Track", "writer_first_name"=>"Josh", "writer_last_name"=>"Zandman", "cleared"=>"1"},
"pro"=>"ASCAP", "commit"=>"Create Song"}
您可以清楚地看到需要
“pro”=>“ASCAP”
位于
“song”=>
部分内部的参数。并用上述解决方案来解决它。。。
检查服务器日志中的输出是找出错误原因的一个很好的方法:)

好的,所以我知道您有解决方案,但为了将来调试此类问题的参考,您应该始终查看输出中的这一行:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"L70xGWxlf+eeQc2hbP/hywKdy4+TBsaKgVP7sP5edG4TSnC6Jf3wFyBjZn9/nANnoKnm6NGj0Hw6DyjsyJazug==",
"song"=>{"artist"=>"The High Fives", "song_name"=>"Off Track", "writer_first_name"=>"Josh", "writer_last_name"=>"Zandman", "cleared"=>"1"},
"pro"=>"ASCAP", "commit"=>"Create Song"}
您可以清楚地看到需要
“pro”=>“ASCAP”
位于
“song”=>
部分内部的参数。并用上述解决方案来解决它。。。
检查服务器日志中的输出是找出错误原因的好方法:)

您好,欢迎使用堆栈溢出。您可以查看您的日志文件(例如
log/development.log
)或启动服务器的窗口中的输出,并检查输出以查看您实际收到的参数。如果有什么东西阻止你的参数被允许,你也会得到另一行告诉你哪些参数是不被允许的。编辑您的问题并将所有问题添加到原始问题中(不要将其放在注释中,因为注释中的代码格式非常糟糕):)同时向我们显示您的控制器操作以及控制器中的许可/要求方法中的内容。:)否则我们只是猜测你的问题是什么,可能猜错了。@TarynEast我更新了问题-谢谢!大家好,欢迎来到Stack overflow。您可以查看您的日志文件(例如
log/development.log
)或启动服务器的窗口中的输出,并检查输出以查看您实际收到的参数。如果有什么东西阻止你的参数被允许,你也会得到另一行告诉你哪些参数是不被允许的。编辑您的问题并将所有问题添加到原始问题中(不要将其放在注释中,因为注释中的代码格式非常糟糕):)同时向我们显示您的控制器操作以及控制器中的许可/要求方法中的内容。:)否则我们只是猜测你的问题是什么,可能猜错了。@TarynEast我更新了问题-谢谢!您好,我的参数列表中包含了它。您能按照Taryn East在您的原始帖子中提供的说明(提供更多信息)进行操作吗?通过这种方式,我们将能够更准确地看到您的问题可能在哪里,并进一步帮助您:)按照您在编辑中的建议做,成功了!非常感谢。很高兴为您提供帮助:)编辑了原始帖子并给出了解释。嗨,我的参数列表中确实包含了它。您能按照Taryn East在原始帖子中提供的说明(提供更多信息)进行操作吗?通过这种方式,我们将能够更准确地看到您的问题可能在哪里,并进一步帮助您:)按照您在编辑中的建议做,成功了!非常感谢。很高兴能帮上忙:)编辑了原始帖子并给出了解释。谢谢Taryn的帮助!我真的很感谢塔林的帮助!我真的很感激