尝试按照此处显示的示例,我正在尝试创建一个链接到父记录的记录。
就我而言,我有两个类:传感器和读数。我可以毫无问题地创建传感器,但无论我如何尝试创建读数,我似乎都失败了:(
我已经旋转轮子足够长的时间了,我认输了,希望有人能发现我的愚蠢错误。
还有一件事 - 我想使用 JSON 发布数据。但是通过调试过程,我什至没有查看 JSON 值,我已经对它们进行了硬编码,但它仍然无法正常工作。
ReadingsController.groovy
package grailshelloworld
import grails.converters.JSON
import groovy.json.JsonSlurper
class ReadingsController {
def scaffold=Readings
def save = {
def slurper = new JsonSlurper()
def result = slurper.parseText(request.reader.text)
def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")
.addToReadings(reading: "blah")
.save()
render ([ok: false] as JSON)
}
}
传感器.groovy
package grailshelloworld
class Sensor {
String sensorid
String sensorname
String sensordescription
static hasMany = [readings: Readings]
static constraints = {
sensorid blank:false, nullable: false
sensorname blank:false, nullable: false
}
}
Readings.grooovy
package grailshelloworld
import java.util.Formatter.DateTime;
class Readings {
String reading
static belongsTo = [sensor: Sensor]
}
当前错误:参数类型不匹配...
<dt>Class</dt><dd>java.lang.IllegalArgumentException</dd><dt>Message</dt><dd>argument type mismatch</dd></dl><h2>Around line 15 of <span class="filename">grails-app/controllers/grailshelloworld/ReadingsController.groovy</span></h2>
<pre class="snippet"><code class="line"><span class="lineNumber">12:</span> def slurper = new JsonSlurper()</code><code class="line"><span class="lineNumber">13:</span> def result = slurper.parseText(request.reader.text)</code><code class="line"><span class="lineNumber">14:</span></code><code class="line error"><span class="lineNumber">15:</span> def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")</code><code class="line"><span class="lineNumber">16:</span> .addToReadings(reading: "blah")</code><code class="line"><span class="lineNumber">17:</span> .save()</code><code class="line"><span class="lineNumber">18:</span></code></pre><h2>Around line 195 of <span class="filename">PageFragmentCachingFilter.java</span></h2>