On Apr 27, 2011, at 9:44 AM, Sebastian wrote:
OK, I don't know why but it happens all the time that after posting a
problem in a forum I am able to solve the problem, nearly!!!
I think I was really on the wrong track!
Now I added the following code in my patent view:
<%params = {:watchedfamily => {:Prio_No => "666666", :title =>
"Please", :watchedmembers_attributes => [{:Pub_No => "Test1"},
{:Pub_No => "Test2"}]}} %>
<%Watchedfamily.create!(params[:watchedfamily]) %>
This is creating a new record with two nested records, exactlly what I
wanted. Only problem is now: HOW TO FIRE THIS WITH A BUTTON OR LINK???
You need to re-think this entirely. The fact that you are calling a
Model method directly in a view should be a strong whiff of Code
Smell, and you need to consider how to make this a link to a REST
(create) method in your WatchedfamilyController rather than what you
have here. Getting the result back into your Patent view is an
exercise in routing and maybe Ajax, but what you have done in this
view shouldn't work, or maybe shouldn't be tried, even if you can get
it to work part-way.
Walter
Cheers
On 27 Apr., 15:18, Sebastian <[email protected]> wrote:
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| %>
<p>
<%= x.text_field :Pub_No, :value => "Test" %>
</p>
<%end%>
<p><%= submit_tag "Save hidden form" %></p>
<% 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| %>
<div class="field">
<%= f.label :Prio_No %><br />
<%= f.text_field :Prio_No %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<%= f.fields_for :watchedmembers do |x| %>
<p>
<%= x.text_field :Pub_No, :value => "Test" %>
</p>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% 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
--
You received this message because you are subscribed to the Google
Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-
[email protected].
To unsubscribe from this group, send email to [email protected]
.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
.
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.