params从form_for传递但未插入/提交到数据库

时间:2016-08-19 23:24:02

标签: ruby-on-rails ruby

我有一个表单,我的创建控制器提交了一个值。在服务器日志中,我看到传递的参数(:style),但没有插入到数据库中。有人知道这会怎么样吗?

服务器日志

Started POST "/users/25/cupboards" for ::1 at 2016-08-19 19:11:39 -0400
Processing by CupboardsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"asWeoSFSisr62oMOPXZnb//KJ8LnAhT1h07NOm1Yn2O8t06N4yrDGugkd01AMQcujYzSahH+O7bEA/jpH+L9fQ==", "style"=>"WEEKEND", "commit"=>"Make a new closet", "user_id"=>"25"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 25]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 25]]
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "cupboards" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["user_id", 25], ["created_at", "2016-08-19 23:11:39.219003"], ["updated_at", "2016-08-19 23:11:39.219003"]]
   (0.7ms)  commit transaction
Redirected to http://localhost:3000/users/25/cupboards/35/edit
Completed 302 Found in 46ms (ActiveRecord: 1.4ms)

表格

<%= form_for @cupboard, url: { action: "create" }, html: { class: "listform"} do |f| %>
  <%= f.label "ATHLEISURE", class: "welcomelistitem" %>
  <%= radio_button_tag(:style, "ATHLEISURE") %>

  <%= f.label "CASUAL", class: "welcomelistitem" %>
  <%= radio_button_tag(:style, "CASUAL") %>

  <%= f.label "PROFESISONAL", class: "welcomelistitem" %>
  <%= radio_button_tag(:style, "PROFESSIONAL") %>

  <%= f.label "WEEKEND", class: "welcomelistitem" %>
  <%= radio_button_tag(:style, "WEEKEND") %>

  <%= f.label "FESTIVAL", class: "welcomelistitem" %>
  <%= radio_button_tag(:style, "FESTIVAL") %>

  <%= f.label "DATENIGHT", class: "welcomelistitem" %>
  <%= radio_button_tag(:style, "DATENIGHT") %>

<%= f.submit 'Make a new closet' %>
<% end %>

控制器

def create
  @user = User.find(params[:user_id])
  @cupboard = @user.cupboards.new(cupboard_params)
 if @cupboard.save
   redirect_to edit_user_cupboard_path(@user, @cupboard)
 else
   render :new
 end
end

PARAMS

private
  def cupboard_params
    # I used .fetch because .permit wasnt working. workaround found at http://stackoverflow.com/questions/24944871/actioncontrollerparametermissing-param-is-missing-or-the-value-is-empty-film
    params.fetch(:cupboard, {}).permit(:style, :season, :neutral1, :neutral2, :accent1, :accent2)
  end
end

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

参数“style”未在params[:cupboard]内传递。它直接在params

我没有在你的表格中看到其他参数(除了风格)所以我不确定那里发生了什么,但试试这个

def cupboard_params
  params.permit(:style)
end

或者将视图中的单选按钮更改为此

radio_button_tag('cupboard[style]', "ATHLEISURE")

答案 1 :(得分:1)

这是因为您的表单提交了style属性值,如style而不是cupboard[style]。您似乎正在将form_for方法与form_tag方法混合,您需要将radio_button_tag替换为radio_button。另请注意,有两种形式的表示法:函数调用radio_button(:model_name, :col_name, 'value')或表单对象本身f.radio_button(:col_name, 'value')上的方法调用。您的表单的另一个问题是,您为每个收音机使用标签,而它的设计是为每组收音机按钮使用一次。因此,表单应如下所示:

<%= form_for @cupboard, html: { class: 'listform' } do |f| %>
  <%= f.label :style, class: 'welcomelistitem' %>
  <%= f.radio_button :style, 'ATHLEISURE' %>
  <%= f.radio_button :style, 'CASUAL' %>
  <%= f.radio_button :style, 'PROFESSIONAL' %>
  <%= f.radio_button :style, 'WEEKEND' %>
  <%= f.radio_button :style, 'FESTIVAL' %>
  <%= f.radio_button :style, 'DATENIGHT' %>

  <%= f.submit 'Make a new closet' %>
<% end %>

然后你必须像这样重写cupboard_params方法:

def cupboard_params
  params.require(:cupboard).permit(:style)
end

没有时间检查这是否有效,但似乎应该这样。