Fields_for多态:没有将符号隐式转换为整数

时间:2019-02-18 15:38:36

标签: ruby-on-rails ruby-on-rails-4

我正试图创建一个 Group 和一个嵌套关联(Wathlist,多态),但出现此错误:

没有将符号隐式转换为整数

模型

package com.socket;
import lombok.Getter;
import javax.faces.push.Push;
import javax.faces.push.PushContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

@ViewScoped
@Named
public class IndexBackingBean implements Serializable {

    @Getter
    private String someString = "Hello";

    @Inject
    @Push(channel = "test-channel")
    private PushContext pushContext;

    private int counter = 0;

    public void clickMe() {
        counter += 1;
        final String msg = "Click #" + counter;
        System.out.println(msg + "!!");
        pushContext.send(msg);
    }
}

控制器

class Group < ApplicationRecord
  belongs_to :user
  has_many :watchlists
  accepts_nested_attributes_for :watchlists, allow_destroy: true

end

class Watchlist < ApplicationRecord
  belongs_to :group
  belongs_to :watchable, polymorphic: true
end

查看

def create
    @group = Group.new(group_params)  <---- ERROR HERE
    @group.user = current_user

  end


# Never trust parameters from the scary internet, only allow the white list through.
    def group_params
      params.require(:group).permit(:name,
        watchlists_attributes: [:watchable_type, :watchable_id]
      )
    end

enter image description here

1 个答案:

答案 0 :(得分:0)

解决了添加:watchlists,Watchlist.new

<%= f.fields_for :watchlists, Watchlist.new do |w| %>
   <%=w.hidden_field :watchable_type, value: @watchable.class %>
   <%=w.hidden_field :watchable_id, value: @watchable.id %>
<% end %>