【问题标题】:Getting error "No signature of method is applicable for argument types: (com.vproc.market.SubCategory)" in grails在 grails 中出现错误“没有方法签名适用于参数类型:(com.vproc.market.SubCategory)”
【发布时间】:2013-11-05 13:34:40
【问题描述】:

我正在尝试将类别和子类别添加到组织(当前登录用户)。我可以添加类别但未能将子类别添加到组织。当我尝试时,收到以下消息:

No signature of method: com.vproc.market.Follower.addToSubCategories() is applicable for argument types: (com.vproc.market.SubCategory) values: [com.vproc.market.SubCategory : 4]. 

我尝试按照以下组织控制器的方法将类别和子类别添加到组织。

OrganizationController.groovy

   def follow() {
      Subscriber loggedinSubscriber = subscriberService.getLoggedinSubscriber()
      Party organization = loggedinSubscriber?.customer?.party
      def marketInstance = Category.get(params.abc)
      def follower = new Follower()
      follower.followedBy = organization
      follower.category = marketInstance
      def sub = params.list('subcategories')
      sub.each { id ->
      follower.addToSubCategories(SubCategory.get(id))
      }
      follower.save(failOnError: true);
      flash.msg = "Okay. This market is now on your watchlist."
      redirect (action: "profile")
    }

在这种方法中,我在以下行中出现错误:

follower.addToSubCategories(SubCategory.get(id))

问题标题中提到。

Organization.groovy

package com.vproc.member

import java.util.Date;
import com.vproc.market.SubCategory;

class Organization extends Party{

    String orgName
    Person contact
    String orgSize
    boolean isVendor  = false

  static hasMany = [follows: SubCategory]

  static constraints = {
    orgName blank: false
    orgSize blank: false
  }
}

Follower.groovy

package com.vproc.market
import com.vproc.member.Organization;

class Follower {

    Category category
    Organization followedBy
  SubCategory subCategory
  static constraints = {
    }
}

Follower 是将 Category 和子类别添加到 Organization 并存储的域。

Category.groovy

package com.vproc.market

import com.vproc.enquiry.Enquiry;

class Category {

    String name
    String description
    static constraints = {
    }
    static hasMany = [ subCategories: SubCategory ]
}

SubCategory.groovy

package com.vproc.market

import com.vproc.enquiry.Enquiry;

class SubCategory {
    String name 

    static hasMany = [requirements: Enquiry]
    static belongsTo = [ category: Category]
    static constraints = {
        requirements nullable:true
    }
}

gsp 文件

<g:form  controller="organization" params="[temp : marketInstance?.id]" action="follow" method="post">
                <g:hiddenField  name= "abc" value="${marketInstance?.id}"  />
                <g:hiddenField name="id" value="${subcategory?.id}" />
                  <div style="margin-left:200px">
                    <input type="button" class="button-inner" id="check1" value="Check All" />
                    <input type="hidden" id="isChkd" value="true" />
                    <g:each var="subcategory" in="${subCategroyInstanceList}">
                    <div>
                    <g:checkBox class="cb1-element" name="subcategories" value="${subcategory.id}"/>
                    <label for="subcategories"> ${subcategory.name}</label>
                    </div>
                  </g:each>
                  <button class="btn btn-inverse">Submit</button>
                </div>
              </g:form>

总结:我想向组织添加类别和子类别。我可以成功添加类别,但未能将子类别添加到组织。

以下几行出现错误:

def sub = params.list('subcategories')
      sub.each { id ->
      follower.addToSubCategories(SubCategory.get(id))
      }

无方法签名:com.vproc.market.Follower.addToSubCategories() 适用于参数类型:

【问题讨论】:

    标签: grails


    【解决方案1】:

    快速浏览一下您的域,Follower 域与 SubCategory 直接关联。您可以简单地将 subCategoty 分配给不需要follower.addToSubCategories(SubCategory.get(id))

    可能是这样的:

    follower.subCategory = SubCategory.get(id)
    

    【讨论】:

    • 嗨@alidad,虽然我选择了2个子类别,但它只添加了一个子类别。任何解决方案。
    • 也许您需要将其定义为 hasMany。类似于您在Category 中的内容
    • 嗨@alidad,你的意思是静态hasMany = [ subCategories: SubCategory ]应该添加到Follower域,对吧?
    • 嗨@alidad,我尝试了你的建议,但没有奏效。
    • 根据您所说的,您的要求似乎要求您为追随者拥有多个子类别,如果是这种情况,那么您需要根据您的模型定义一对多或多对多关系.您如何定义它允许您只有一个子类别
    【解决方案2】:

    我目前正在研究 Grails,也发现了类似的问题。我正在使用 Grails 2.3.7,我发现了这个 https://github.com/grails/grails-datastore-test-support/issues/1

    即使从这个链接它提到这是一个错误,但我仍然不确定。到目前为止,我只是执行以下操作,它对我有用:

    if(follower.subCategories == null){
        follower.subCategories = [] 
    }
    def sub = params.list('subcategories')
      sub.each { id ->
      follower.subCategories.add(SubCategory.get(id))
    }
    

    如果您对此问题有更好的解决方案,请告诉我。我也很想知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2019-06-28
      • 2021-07-31
      • 1970-01-01
      相关资源
      最近更新 更多