Rails集合在表单中选择不保存

时间:2015-06-03 08:07:40

标签: ruby-on-rails simple-form

无论出于何种原因,这都不能节省。它会给我一个成功并重定向到它应该的页面,但是group项上的page_id是nil。

<%= simple_form_for group do |f| %>
    <%= f.input :page_id, 
        collection: @pages,
        selected: group.page,
        label: false
        %>
    <%= f.button :submit, "Assign Page" %>
<% end %>

控制器

def update
  group = Group.find(params[:id])
  group.update_attributes(group_params)
  redirect_to groups_path, notice: "Did it"
end

并且params(我已经尝试了所有的变化)。这里奇怪的是,表格似乎是在创建一个嵌套的参数。

def group_params
  params.require(:group).permit(:name, :page, :group => [:page_id])
end

返回的请求参数:

{"utf8"=>"✓", "_method"=>"patch",
"authenticity_token"=>"Cccccii383838=",
"group"=>{"page_id"=>"2"}, "commit"=>"Assign Page",
"action"=>"update", "controller"=>"groups", "id"=>"3"}

组has_one页面。页面has_one组。相关的ID位于每个模型上。

有什么想法?我把头撞到墙上是因为这可能是一个愚蠢的问题。

3 个答案:

答案 0 :(得分:0)

你尝试过这个吗?我认为问题出在# MySQL. Versions 4.1 and 5.0 are recommended. # # Install the MYSQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # http://dev.mysql.com/doc/refman/5.0/en/old-client.html development: adapter: mysql2 encoding: utf8 reconnect: false database: mrdelivery_development pool: 5 username: hd_user password: host: localhost # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: mysql2 encoding: utf8 reconnect: false database: mrdelivery_test pool: 5 username: hd_user password: host: 127.0.0.1 port: 3306 production: adapter: mysql2 encoding: utf8 reconnect: false database: mrdelivery_production pool: 5 username: hd_user password: host: localhost 方法

 group_params

答案 1 :(得分:0)

<%= simple_form_for @group do |f| %>
    <%= f.input :page_id, 
        collection: @pages,
        selected: group.page,
        label: false
        %>
    <%= f.button :submit, "Assign Page" %>
<% end %>

groups_controller.rb

def update
  @group = Group.find(params[:id])
  @group.update_attributes(group_params)
  redirect_to groups_path, notice: "Did it"
end

def group_params
  params.require(:group).permit(:name, :page_id)
end

,您的关联应该是page has_one group和group belongs_to page

答案 2 :(得分:0)

替换控制器中的强参数

Picasso.with(ctx).load(uri)
    .transform(new YourCropTransform(params...))
    .into(target)