Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 Rails从多个选择表单更新模型:没有将字符串隐式转换为整数_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 Rails从多个选择表单更新模型:没有将字符串隐式转换为整数

Ruby on rails 4 Rails从多个选择表单更新模型:没有将字符串隐式转换为整数,ruby-on-rails-4,Ruby On Rails 4,如果项目有许多类型通过:GenresProject 参数为: def project_params params.fetch(:project, {}).permit(genres_projects_attributes: [:id, {genre_id: []}) end 我的提交表格是: <%= form_for @project do |f| %> <%= f.fields_for :genres_projects_attributes do |ff| %&g

如果项目
有许多
类型
通过:
GenresProject

参数为:

def project_params
  params.fetch(:project, {}).permit(genres_projects_attributes: [:id, {genre_id: []})
end
我的提交表格是:

<%= form_for @project do |f| %> 
  <%= f.fields_for :genres_projects_attributes do |ff| %>
    <%= ff.select :genre_id, Genre.order(:id).collect{|g| [g.name, g.id]}, {}, { multiple: true } %>
  <% end %>
  <%= f.submit 'Update'%>
<% end %>
应该

project.update(project_params)

自动遍历
流派\u id
数组并创建相应的GenresProject记录?

如果没有
GenresProject
记录,则无法从
参数中获取id

其次,我认为,它不会自动为您创建新的
类型
记录或更新一个。您应该自己在
update
操作中创建新记录。例如:

params[:project][:genre_id].each do |id|
  unless GenreProject.find(id)
    # create new record here
    GenreProject.create
  end
  # other updating operations
end

希望它对您有用

谢谢,这正是我所期望的,但我不确定。
params[:project][:genre_id].each do |id|
  unless GenreProject.find(id)
    # create new record here
    GenreProject.create
  end
  # other updating operations
end