Hi guys,
A question about creating rows and some scaffolding issues.
At the same time I could describe how I''m doing my stuff, just in case
I''m doing something wrong.
I have two models.
Users:
class User < ActiveRecord::Base
has_many :tabs
...some stuff for authentication here
end
Tabs:
class Tab < ActiveRecord::Base
belongs_to :users
end
---------
I create a user and then after authentication it gets access to the form
from where to create tabs. In that form the user enters some parameters,
but not all. So in the create method in the controller I get some of
them from the from and the rest are filled by the controller itself.
That''s why I use create instead of new for the DB row creation.
def create
@user = User.find(session[:uid])
picture_location = "C:/tabs/aname.png"
midi_location = "C:/midis/aname.mid"
if @user.tabs.create( :user_id => session[:uid],
:name => params[:tab][:name],
:album => params[:tab][:album],
:midi_file_location => midi_location,
:tab_picture_location => picture_location )
#save tab picture
File.open("C:/tabs/aname.png", "wb") do |f|
f.write(@params[''tab_picture''].read)
end
#save tab midi
File.open("C:/midis/aname.mid", "wb") do |f|
f.write(@params[''tab_audio''].read)
end
redirect_to :action => ''list''
end
end
QUESTION 1:
The idea is to put the binary objects into the file system if
@user.tabs.create goes ok. But is a save needed after using create? It
works fine but I''m am not sure I''m using it correctly though.
QUESTION 2:
def new
#@user = User.find(session[:uid])
#@tab = @user.tabs.new
@tab = Tab.new
end
The method "new" in the controller contains @tab = Tab.new and that
works fine. But if I use:
@user = User.find(session[:uid])
@tab = @user.tabs.new
Also works fine. Could someone explain what is the difference between
those (if any)? And...why is it needed if I use the create call in the
real creation of the row?
Thanks a lot!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---