- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从我的 Android 应用程序中获取以下格式的 JSON
{"list":[{"itemno":"w01","name":"t01","amount":"120","status":"done"},{"itemno":"w02","name":"t02","amount":"120","status":"done"},{"itemno":"w03","name":"t03,"amount":"120","status":"done""}]}
我需要解析它以插入到 mysql 表“列表”中。我修改了 Controller 中的“创建”代码,如下所示
def create
lists = params[:list].collect{|key,list_attributes| List.new(list_attributes) }
all_list_valid = true
lists.each_with_index do |list,index|
unless list.valid?
all_list_valid = false
invalid_list = lists[index]
end
end
if all_list_valid
@lists = []
lists.each do |list|
list.save
@lists << list
end
format.html { redirect_to @list, notice: 'List was successfully created.' }
format.json { render json: @list, status: :created, location: @list }
else
format.html { render action: "new" }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
但是无法解析JSON,所以没有调用create方法。我对 ROR 很陌生,也从网上尝试过上面的代码。不确定上面的整篇文章是否不正确,或者我遗漏了什么。请指教。谢谢。
最佳答案
此外,您可以稍微重构一下代码:
# This part of code
@lists = []
lists.each do |list|
list.save
@lists << list
end
# Can be shortened:
lists.map(&:save)
@lists = lists
整个 Action 可以这样重构:
def create
lists = params[:list].each{ |list_attributes| List.new(list_attributes) }
valid, not_valid = lists.partition{ |list| list.valid? }
if not_valid.blank?
lists.map(&:save)
@lists = lists
format.html { redirect_to @list, notice: 'List was successfully created.' }
format.json { render json: @list, status: :created, location: @list }
else
format.html { render action: "new" }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
关于ruby-on-rails - ROR 中的 JSON 数组解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18188043/
我已经使用 RailsInstaller 安装了 Ruby on Rails,还在 Windows 8 中安装了 postgresql。我正在尝试使用文件运行 rails server,但我收到错误消
我正在尝试关注 Ryan Bates RailsCast #196: Nested model form part 1 。 Ryans 版本有两个明显的区别:1)我使用的是内置脚手架,不像他使用的那么
你好,我通过内部连接获取了重复的数据行。我编写了带有 OR 条件的内部连接。 Controller userscontroller.rb @users = User.search(params[:se
我已经创建了一个表,现在我想给表添加一个关联(has_many 关系)。如果我已经创建了一个表并且在创建模型后已经完成了 rake db:migrate,我该怎么办? 在ror中创建表后是否可以添加关
我正在尝试将数据插入 ROR 中的数据库,对于电话号码,我采用了字段整数..但它只采用了 9 位数字..如果我尝试输入 10 位数字。它提供错误作为。 Mysql2::Error: Out of ra
如何获得事件项目菜单?循环生成菜单 我想为这个项目使用其他 css 属性。有任何想法吗?最好是js,jquery... 最佳答案 link_to_unless_
我正在编辑设计生成的用户模型,为用户添加一个语言字段,他可以在其中添加他从组合框中所说的一种或多种语言。 我的问题是在 mysql 中表示这一点的最佳方式是什么。我不知道他会说多少种语言,所以可能最多
我对 Rails 很陌生,并且对这个问题的逻辑有点困惑。 我有一张员工表(使用mysql),每个员工都有一个manager_id键,该键指的是他们向其汇报的员工。例如,标题为“CEO”且 id 为 1
那里的例子很少,但没有一个是非常清楚的(或在旧版本上)。 我想调用 MySQL 过程并检查返回状态(在 rails 4.2 中)。我看到的最常见的方法是调用 result = ActiveRecord
我在 /public 下有一些静态错误页面,我在其中按以下方式链接了一个名为 errors.min.css 的样式表: 但是,当我在除根路径 (/abc/non-existent-url) 之外的路
我想在 RoR 中将一张图片叠加在另一张图片上。 在纯 html 中这样做很容易(来自 https://stackoverflow.com/a/1997397/1760830 ): 当
我正在努力将静态站点引入 ruby on rails 应用程序,以便我们可以构建功能等。 我已经使用页面 Controller 设置了所有静态页面,并将所有静态页面存储在 View /页面下...
我在表格中有一个单元格,看起来像 我想编写一个 CSS 类,根据 play_result.status 的值更改此单元格的字体颜色,绿色代表字符串“OK”,橙色代表“CHANGED”,蓝色代表“
我正在尝试绑定(bind)帖子搜索页面上的类别下拉列表。下拉菜单应将类别图像显示为下拉菜单中类别名称左侧的图标。 下一行绑定(bind)所有类别并在下拉列表中显示它们的名称: 'Any') %> 我
在 Ruby 代码的 TextField 中,我想在模糊事件中检查指定的类是否被应用。请建议一些技术或方法。下面给出了我使用的示例代码。 TextField 的 Ruby 代码:: 4, :t
试图弄清楚 Ruby On Rails 中如何处理并发。 如何获取一段代码来锁定数据库中的行并在需要时强制回滚? 更具体地说,有没有办法强制某一段代码完全完成,如果不回滚?我想在我的项目中为交易添加历
我们正在开发一个 Rails 应用程序,其中一类用户可以编辑和创建文章。有时这些文章包含指向网站上其他文章的链接。执行以下操作的最佳方法是什么? 将链接存储在数据库中 在页面上呈现链接 目前我们有绑定
我有以下 ROR 代码 def func (point,c,r) res=isPointInCircle(point,c,r) #returns true/ false if
如何在 Rails 3.0 中使用 MD5 加密字符串?模型中的 pass = MD5.hexdigest(pass) 产生 未初始化的常量 MyModel::MD5 最佳答案 为此,您可以使用 Ru
我正在寻找有关监控每天大约有 250,000 次插入的特定表的建议。规模不大,但增长迅速,我们不能像现在这样继续工作。 主要应用程序依赖于带有 MySQL 数据库的 RoR4。数据从多个 Radius
我是一名优秀的程序员,十分优秀!