Rails 4嵌套模型:如何使用json数据创建记录

时间:2021-11-21 14:30:23

I have nested model like following:

我有嵌套模型如下:

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

class Project::Task < ActiveRecord::Base
  attr_accessible :task_id, :name
  belongs_to :Project
end

I have json data coming from outside:

我有来自外面的json数据:

"project": {
      "name": "My Project Name",
      "tasks": [
        {"name": "Design prototype"},
        {"name": "Home page UI  prototype"},
        {"name": "Other Miscellaneous task"}
     ]
}

How can I create controller in Rails 4 which receives above json data as POST vars and store it in DB?

如何在Rails 4中创建控制器,将上面的json数据作为POST vars接收并存储在DB中?

2 个解决方案

#1


4  

from

"project": {
      "name": "My Project Name",
      "tasks": [
        {"name": "Design prototype"},
        {"name": "Home page UI  prototype"},
        {"name": "Other Miscellaneous task"}
     ]
}

to

"project": {
      "name": "My Project Name",
      "tasks_attributes": [
        {"name": "Design prototype"},
        {"name": "Home page UI  prototype"},
        {"name": "Other Miscellaneous task"}
     ]
}

in controller

在控制器中

project_params = params.require(:project).permit(:name, tasks_attributes: [:name])
Project.new(project_params)

#2


-2  

On project new form page do this:-

在项目新表单页面上执行以下操作: -

<%= form_for @project do |f| %>
    <%= f.fields_for :tasks do |task| %>
        <%= task.text_field :name %>
    <% end %>
<% end %>

Project controller:-

项目负责人: -

def new
    @project = Project.new
    @project.tasks.build
end

def create
    @project = Project.new(project_params)
    if @project.save
        redirect_to success_path
    else
        render 'new'
    end
end

private
def project_params
    params.require(:project).permit(:name, tasks_attributes: [:id, :name])
end

Also, attr_accessible is removed from Rails 4. In Rails 4 We need to permit attributes in controller like "project_params" method in project cntroller.

此外,attr_accessible从Rails 4中删除。在Rails 4中我们需要允许控制器中的属性,如项目cntroller中的“project_params”方法。

#1


4  

from

"project": {
      "name": "My Project Name",
      "tasks": [
        {"name": "Design prototype"},
        {"name": "Home page UI  prototype"},
        {"name": "Other Miscellaneous task"}
     ]
}

to

"project": {
      "name": "My Project Name",
      "tasks_attributes": [
        {"name": "Design prototype"},
        {"name": "Home page UI  prototype"},
        {"name": "Other Miscellaneous task"}
     ]
}

in controller

在控制器中

project_params = params.require(:project).permit(:name, tasks_attributes: [:name])
Project.new(project_params)

#2


-2  

On project new form page do this:-

在项目新表单页面上执行以下操作: -

<%= form_for @project do |f| %>
    <%= f.fields_for :tasks do |task| %>
        <%= task.text_field :name %>
    <% end %>
<% end %>

Project controller:-

项目负责人: -

def new
    @project = Project.new
    @project.tasks.build
end

def create
    @project = Project.new(project_params)
    if @project.save
        redirect_to success_path
    else
        render 'new'
    end
end

private
def project_params
    params.require(:project).permit(:name, tasks_attributes: [:id, :name])
end

Also, attr_accessible is removed from Rails 4. In Rails 4 We need to permit attributes in controller like "project_params" method in project cntroller.

此外,attr_accessible从Rails 4中删除。在Rails 4中我们需要允许控制器中的属性,如项目cntroller中的“project_params”方法。