Re: Creating Two Models in One Form
That worked a treat. Thank-you!
You are not logged in. Please login or register.
Ryan - first off, thanks for all of your great advice that I've found all over the web. I seem to be doing something wrong with this tutorial though. I have pretty much this exact project/task scenario - only I'm working with projecttemplates and templatetasks (these are my tables). The problem I'm having is my projecttemplateID is not carrying over and posting to my templatetasks table to tie the task and project together. Both projecttemplate and templatetask are being saved to the database but I'm missing whatever it takes to get the id in the templatetask table....any help would be greatly appreciated - nikikelly
projecttemplate_controller.rb has the following code:
def new
@projecttemplate = Projecttemplate.new
@templatetask = Templatetask.new
end
def create
@projecttemplate = Projecttemplate.new(params[:projecttemplate])
@templatetask = @projecttemplate.templatetasks.build(params[:templatetask])
if @projecttemplate.save
flash[:notice] = 'Project template was successfully created.'
redirect_to :action => 'edit', :id => @projecttemplate
else
render :action => 'new'
end
end
Is the column called "projecttemplate_id" exactly? Try creating the two models in script/console and see if you get the same results.
oops - is that it? Actually, it's projecttemplateID - doh!
Ryan,
You really are incredible with your support of us newbies - really do appreciate it - can't believe you responded so quickly - you must be glued to this stuff! All fixed!
Can't get past this issue - I know it's another dumb question - but I have to ask....trying to delete the templatetask from the edit view of the projecttemplate and then return to the edit view of the projecttemplate (or never leave it)....
I keep getting this error:
Couldn't find Projecttemplate without an ID
in templatetask_controller.rb:
def destroy
Templatetask.find(params[:id]).destroy
redirect_to :controller => 'projecttemplate', :action => 'edit', :id => @projectID
end
in projecttemplate/view/edit.rhtml:
<%= link_to 'delete', { :controller => 'templatetask', :action => 'destroy', :id => templatetask, :projectID => @projecttemplate.id }, :confirm => 'Are you sure?' %>
Thanks in advance....
Try this:
# view
<%= link_to 'delete', { :controller => 'templatetask', :action => 'destroy', :id => templatetask }, :confirm => 'Are you sure?' %># controller
def destroy
@task = Templatetask.find(params[:id]).destroy
redirect_to :controller => 'projecttemplate', :action => 'edit', :id => @task.projecttemplate_id
end
Oh I really hate it when I ask dumb questions....thanks again ryanb.
Any experience working with the RoR sublist plugin for making these types of forms more live? Words of advice greatly appreciated...
http://rorsublist.rubyforge.org/
Last edited by nikikelly74 (2007-07-15 12:40:10)
I'm having some issues with this and I can't figure out where I am going wrong.
I have an images model created using attachment_fu. I have a stone model that I want to create with an image. Here's what I have done.
I gave the images table a stone_id column. Stone has_one :image and Image belongs_to :stone.
I can create an image by itself and that works great. I've got the following in my StoneController.
def new
@stone = Stone.new
@image = Image.new
end
def create
@stone = Stone.new(params[:stone])
@image = @stone.build_image(params[:image])
if @stone.save
flash[:notice] = 'Stone was successfully created.'
redirect_to :action => 'index'
else
render :action => 'new'
end
end
<%= error_messages_for :stone, :image %><%= start_form_tag :action => 'create', :multipart => true %>
<p>
Name<br />
<%= text_field :stone, :name %>
</p>
<p>
Description<br />
<%= text_area :stone, :description %>
</p>
<p>
Price Per m<sup>3</sup><br />
<%= text_field :stone, :price_per_m3 %>
</p>
<p>
Image:
<%= file_field :image, :uploaded_data %>
</p>
<p>
<%= submit_tag 'Create' %>
</p>
<%= end_form_tag %>
Hooray I've fixed my problem. In case anyone is wondering the following worked.
def create
@stone = Stone.new(params[:stone])
@image = @stone.build_image(params[:image])
if @stone.save
flash[:notice] = 'Stone was successfully created.'
redirect_to :action => 'index'
else
render :action => 'new'
end
end
<%= error_messages_for :stone, :image %><% form_for(:stone, @stone, :url => {:action => 'create'}, :html => { :multipart => true }) do |f| %>
<p>
Name<br />
<%= f.text_field :name %>
</p>
<p>
Description<br />
<%= f.text_area :description %>
</p>
<p>
Price Per m<sup>3</sup><br />
<%= f.text_field :price_per_m3 %>
</p>
<% fields_for(:image, @stone.image) do |i| %>
<p>
Upload An Image
<%= i.file_field :uploaded_data %>
</p>
<% end %>
<p>
<%= submit_tag 'Create'%>
</p>
<% end %>
This seems like a logical question for those of you who have implemented the two models in one form. I'm trying to duplicate the parent and the child(ren) - Project and associated Tasks. So, I have the project piece of it working....
In projecttemplate_controller.rb:
def duplicate
@copy_from_projecttemplate = Projecttemplate.find(@params[:id])
@newprojecttemplate = Projecttemplate.new(@copy_from_projecttemplate.attributes)
@newprojecttemplate.templateName = @newprojecttemplate.templateName + " copy"
@newprojecttemplate.save
if @newprojecttemplate.save
redirect_to :controller => 'templatetask', :action => 'duplicate', :id => @copy_from_projecttemplate.id
end
end
Any advice greatly appreciated....
Does using the "build" also handle foreign key building?
My primary table (i.e. Projects) record id is used as a foreign key in my child table (i.e. tasks). Does BUILD handle the generation and insertion of the foreign key?
yes. Thats the whole purpose of the build methods of associations ![]()
@task = @project.build_task(params[:task])
#this will insert the id of @project into the foreign key column of the new created task.
# it's the same as doing
@task = Task.new(params[:task]
@task.project_id = @project.id
Thanks for a great tutorial. I have a slight problem with my script. Everything works fine but I want to change the formatting of the error messages when validation catches something. Right now I have three models/databases being affected by 1 form. Users, Profiles, PersonalInfo. While PersonalInfo is not sending anything to the db I want it to create a field so Users can reference it by it's ID.
My problem is I get two seperate validation errors that looks like this.
3 errors prohibited this user from being saved
There were problems with the following fields:
* Profile is invalid
* User name can't be blank
* Password can't be blank
2 errors prohibited this profile from being saved
There were problems with the following fields:
* City can't be blank
* Full name can't be blank
Here is my code
users_controller.rb
def new
@user = User.new
@profile = Profile.new
enddef create
@user = User.new(params[:user])
@profile = @user.build_profile(params[:profile])
@personal_info = @user.build_personal_info()
@user.personal_info_id = @personal_info.id
@user.profile_id = @profile.id;
if @user.save && @profile.save && @personal_info.save
flash[:notice] = 'User was successfully created.'
redirect_to :action => 'welcome'
else
render :action => 'new'
end
end
profile.rb
class Profile < ActiveRecord::Base
has_one :uservalidates_presence_of :full_name, :city, :state
end
user.rb
class User < ActiveRecord::Base
belongs_to :profile
belongs_to :personal_info
validates_associated :profile
validates_presence_of :user_name, :password
validates_uniqueness_of :user_name
end
form
<%= error_messages_for 'user' %>
<%= error_messages_for 'profile' %><!--[form:user]-->
<p><label for="profile_full_name">Full name</label><br/>
<%= text_field 'profile', 'full_name' %></p><p><label for="profile_location">Location</label><br/>
<%= text_field 'profile', 'city' %>, <%= state_select('profile', 'state', country='US', options = {}, html_options = {}) %></p><p><label for="user_user_name">User name</label><br/>
<%= text_field 'user', 'user_name' %></p><p><label for="user_password">Password</label><br/>
<%= password_field 'user', 'password' %></p>
<!--[eoform:user]-->
This tutorial was very usefull to me too.
Thanks very much
Hi, this tutorial is helping me a lot.But i am developing a web application in ruby on rails.My application is as i have a signup page to enroll the user with stores the data in two tables one in user and one in contacts.Now i am not getting how to update the user profile after login can anybody suggest me.
can i give the id with different name .i have given but it is giving error as couldnt find without id.
Great tutorial; I've managed to get it to work, however,
It is difficult for me to understand how to add a second task to a project.
after adding a task I would have taks.id=1 and added task.id =2, iow two rows in 'tasks';
both of them should have project_id set to '1'.
Its probably quite easy once you know it, but I am having a really hard time to get the view and the controller right for this.
I greatly appreciate any help.
It is difficult for me to understand how to add a second task to a project.
This tutorial just shows how to create one task with the project. See the this other tutorial on creating many tasks with a project.
Hosting provided by aTech Media