I’m a noob to Rails, Ruby, and REST architecture, so please forgive me
if this is a obvious question.
I’ve got a model (“Team”) that aggregates some objects (“Resources”).
In the Team view
/teams/show/3
I display the info for the team, along with the aggregated Resource
objects. So far so good. I want a form to add a resource, so I put
in a form element into the views/teams/show.rhtml file:
<% form_for :resource,
:url => {:controller => “resources” } ,
:html => { :multipart => true } do |form| %>
… fields
<% end %>
Again, so far so good: my resource_controller.rb script gets the form
data in its create method.
@resource = Resource.new(params[:resource])
if @resource.save
flash[:notice] = 'Resource was successfully created.'
redirect_to :back
else
flash[:notice] = 'Error creating resource'
logger.info "Save failed" # I see this in the logs
redirect_to :back # @resource is nil in the rhtml page!
end
But how do I get back to the /teams/show page? If I redirect, it
seems all info for the model is lost: @resource is nil again. I’m
blaming that on the redirect_to, but I can’t figure out how to make
the render method use the teams controller. I’ve event tried parsing
the originating path out of request.env[‘HTTP_REFERER’], but besides
being butt-ugly code I can’t even render that path:
render :template => ‘teams/show/3’
tries to render “3.rhtml” while
render:template => ‘teams/show’
loses the ID so I get an error. Can I do cross-controller UI?
Thanks for any help,
Rod