In the previous article I showed you how to create a project and five tasks all in one form. Here I will show you how to add/remove tasks in that same form using JavaScript and RJS.
Let's start off with the code we created in the last article. But, we'll just start with one task instead of five since they can be added dynamically.
Last edited by ryanb (2006-11-30 19:25:56)
Offline
I have problem
http://pastie.caboo.se/21015
Strange
Offline
Hi innu, would you mind posting your User model (user.rb)? I think the problem might be in there. I'm guessing the login method might call itself, but I'm not sure.
Offline
http://pastie.caboo.se/21106
I think that its not user.rb error.
If i dont use partial, just use code in the content, then there is no error. Only if render partial, the error will appear.
Offline
I've tried to duplicate the problem, but haven't succeeded (it is working fine for me).
I have also researched the stack trace, and the problem appears to occur when passing the user as a local variable into the partial. It calls "hash" on the user which results in an infinite loop over method_missing. I'm wondering if the problem is in the users table (database schema). Perhaps there is a column name which is overriding a method in active_record? I'm not sure.
Offline
I have really stupid feeling.
Today, I turned my pc on and its all working. Maybe it just needed server restart, but i dont know why. Anyway, thanks for help.
Edit:
Btw.
Last edited by innu (2006-11-04 09:39:05)
Offline
innu wrote:
Almous good, except "character "[" is not allowed in the value of attribute "id"." ( XHTML 1.0 Transitional ). Should i change all fields :id-s by hand or ?
Yeah, if you need to reference it I would do it by hand. It is just important that we have [0] in the name so we can loop through the fields in the resulting action.
Offline
Man Ryan--you are posting the exact tutorials that I need! Thank you so much dude.
Offline
Alright--as last tutorial I had trouble passing a third model within the whole scheme of things.
Heres the error:
"NoMethodError in MemoriesController#create
You have a nil object when you didn't expect it!
The error occured while evaluating nil.each_value
Parameters: {"memory"=>{"memory_date(2i)"=>"11", "memory_date(3i)"=>"8", "title"=>"weeee", "memory_date(1i)"=>"2006"}, "commit"=>"Create", "conversation_1"=>{"message"=>"test 2", "author_id"=>"2"}, "conversation_"=>{"message"=>"test 1", "author_id"=>"1"}}
Show session dump
---
flash: !map:ActionController::Flash::FlashHash {}"
and my code:
Offline
On the fields_for conversation line, try this:
Offline
Kk. Tried that--but now I can't get:
" NoMethodError in Memories#new
Showing app/views/memories/_conversation_fields.rhtml where line #3 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occured while evaluating nil.id_before_type_cast
Extracted source (around line #3):
1: <div id="conversation_<%= index %>">
2: <% fields_for "conversations[#{index}]", conversation do |f| %>
3: <%= f.collection_select(:author_id, @authors, :id, :name) %>
4: <%= f.text_field :message %> <%= link_to_remote 'remove', :url => { :action => 'remove_conversation', :index => index } %>
5: <% end %>
6: </div>"
Offline
This happens when the value of the "index" variable is nil. The problem is it isn't getting set in the loop in new.rhtml:
Offline
Ryan--thanks for the reply i'll try it out later.
The reason i was passing the :authors variable is because I thought it was necessary to edit the fields and all that, since i'm trying to pass another model in this equation.
Offline
darrenemo wrote:
The reason i was passing the :authors variable is because I thought it was necessary to edit the fields and all that, since i'm trying to pass another model in this equation.
Here you are referencing the authors as an instance variable (@authors) so it's not necessary to pass it as a local variable. However, you will need to set up this @authors instance variable in every action that renders this partial (this including the add_conversation action).
Offline
ryanb wrote:
...However, you will need to set up this @authors instance variable in every action that renders this partial (this including the add_conversation action).
How would I do this in the add_conversation action?
@authors = Author.find_all (?)
Or would it be different since the authors elements are being passed through all the methods?
Offline
Yeah, however you are doing it in the other action. Probably find(:all)
Offline
Alright i was successfully able to get the authors saved from the new.rhtml template.
Now my next question is how to do I successfully add a new conversation, under the edit.rhtml template?
I added the snippet from new.rhtml to add a new convo (in edit.rhtml):
Offline
darrenemo wrote:
And my third question is, how can I successfully delete the memory AND all the attributes with it? I've toyed around with adding variables (@memory.conversations.each.delete) and such to no avail. The memory successfully gets deleted, but the accompanying conversations don't
Look into the :dependent parameter in the has_many association.
Offline
ryanb wrote:
As for your other questions, I'm afraid you'll have to wait for another tutorial as this can get rather tricky.
Alright--thanks man for your help!
Offline