从select_tag发送选项到Rails中的数据库

时间:2016-06-28 01:06:47

标签: ruby-on-rails forms

我有一个组织数组的助手:

def pros
  pros = ['ASCAP', 'BMI', 'SESAC']
end

我的表格:

<%= f.label :pro, "Performing Rights Organization" %>
<%= select_tag(:pro, options_for_select(pros)) %>

我的歌曲控制器:pro包含在允许的参数列表中:

def song_params
  params.require(:song).permit(:artist, :song_name, :writer_first_name, :writer_last_name, :cleared, :pro)
end

服务器日志:

    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列,但是当我尝试从表单添加它时,它不会保存到数据库中。

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

问题可能出在您的 params 哈希中不允许使用您的专业参数。如果在提交表单时筛选出参数,则可以检入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

您可以通过将 pro 参数添加到 params 哈希中的允许参数列表中来允许 pro 参数:

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, :pro)  # <= add your pro parameter here
    end
end

由于 params 哈希中允许使用该参数,因此它将传递给Organization模型并将保留到数据库中。

编辑:看起来您的专业参数超出了歌曲的范围,因此它没有通过 params [:song] 传递给Song对象。

我建议您将select_tag更改为:

select("song", "pro", options_for_select(pros))

select_tag 没有确定歌曲中的 pro 参数值,所以它基本上是在生成:

<select name="pro">

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

<select name="sond[pro]">

由于 select_tag 未将 pro 传递给 params [:song] ,因此使用 params [:song]实例化一个Song对象不包含 pro 参数。

答案 1 :(得分:1)

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

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" =>部分所需的参数。并使用上述解决方案来解决它...... 检查服务器日志中的输出是找出导致错误的原因的好方法:)