Forum: Ruby on Rails Best way to call action new for a child entity?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2fba0881396c95771fd22ff200c16294?d=identicon&s=25 Petros Amiridis (amiridis)
on 2007-05-04 13:17
Hi,

I have two entities: Installation and Command. Each installation can
have many commands. In order to create a command the user has to go
through the installation. I have generated the standard scaffold code
for command and I changed the two actions New and Create in the
commands_controller as follows:

def new
  session[:installation_id] = params[:id] #I added this
  @command = Command.new
end

def create
  @command = Command.new(params[:command])
  @installation = Installation.find(session[:installation_id]) #I added
this
  @command.installation = @installation #I added this
  if @command.save
    flash[:notice] = 'Command was successfully created.'
    redirect_to :action => 'list'
  else
    render :action => 'new'
  end
end

In the installation list view I have a link for each listed installation
that calls the command new action passing the installation id. In the
new command action I save this id in the session and when create is
called I retrieve the id and assign set the command to be relation with
that installation.

Is this a good practice? Is there another maybe more proper way of doing
what I did?

Thank you,
Petros (A Ruby on Rails newbie)
This topic is locked and can not be replied to.