Hi,
I am working on a patent database, where the user can first search the
patent database and then save the found patent with connected patents
(called familymembers). So I have a page called “patent” where the
search is done and all values that I need for the database are
available.
From that page I want to store the patent with his familymembers into
my database, to watch them. So I have the following models
class Watchedfamily < ActiveRecord::Base
has_many :watchedmembers
accepts_nested_attributes_for :watchedmembers
end
class Watchedmember < ActiveRecord::Base
belongs_to :watchedfamily
end
On my patent page I tried to save the data with a form that looks like
this (I had to define the controller that i want to use, because this
form is not in the watchedfamiles view!):
<% form_for :watchedfamily, :url => {:controller =>
“watchedfamilies”, :action => “create” } do |f| %>
<%= f.text_field :Prio_No, :value => @prio_no %>
<%= f.text_field :title, :value => @title %>
<%= f.fields_for :watchedmembers_attributes do |x| %>
<%= x.text_field :Pub_No, :value => “Test” %>
<%end%>
<%= submit_tag “Save hidden form” %>
<% end %>
That is not working! Error is: “can’t convert Symbol into Integer”
If I create the nested data directly in the watchedfamilies by
clicking new and using the form below it is working:
<%= form_for(@watchedfamily) do |f| %>
<%= f.text_field :Prio_No %>
<%= f.text_field :title %>
<%= x.text_field :Pub_No, :value => "Test" %>
<%end%>The problem is that my first approach is generating the following
request:
Parameters: {“watchedfamily”=>{“Prio_No”=>“123456”,
“title”=>“Whatever”, “watchedmembers_attributes”=>{“Pub_No”=>“Test”}},
“commit”=>“Create Watchedfamily”}
Where my second approach, directly from the watchedfamilies is
generating the following request:
Parameters: {“watchedfamily”=>{“Prio_No”=>“123456”,
“title”=>“Whatever”,
“watchedmembers_attributes”=>{“0”=>{“Pub_No”=>“Test”}}},
“commit”=>“Create Watchedfamily”}
First one watchedmembers_attributes is a string, second one is an
array.
If I change the line in my form from
<%= f.fields_for :watchedmembers_attributes do |x| %>
to
<%= f.fields_for :watchedmembers do |x| %>
I got the error: “Watchedmember(#26928120) expected, got
Array(#5756196)”
Maybe I am on a wrong track. I just want to save a nested value
(patentfamily with its family members to my database!)
I already watched tutorials and asked google, but I didn’t find a
solution to my problem. I hope I get help here!!!
Cheers,
Sebastian