After creating an instance of an object, Rails redirects to the index of the object that it :belongs to

When I click the Save in /views/parts/new.html, I expect to end up on /
views/parts/show.html, but instead I end up in /views/projects/
index.html, with the wrong [flash] message.

I have:

Hi --

Hi --

Theory number two:

form_for(@project, @part) do |f|

Try this:

   form_for([@project, @part]) do |f|

(i.e., put them in an array).

Theory number one still applies, though, in case you run into further
trouble :slight_smile:

David

When I click the Save in /views/parts/new.html, I expect to end up on /
views/parts/show.html, but instead I end up in /views/projects/
index.html, with the wrong [flash] message.

I have:

++++++++++++++++
/models/project.rb
----------------------------

has\_many :parts

++++++++++++++++
/models/parts.rb
----------------------------

belongs\_to :project

++++++++++++++++
/controllers/parts_controller.rb
----------------------------

def new
  @project = Project\.find\(params\[:project\_id\]\)
  @part = @project\.parts\.build

  respond\_to do |format|
    format\.html \# new\.html\.erb
    format\.xml  \{ render :xml => @part \}
  end
end

def create
  @project = Project\.find\(params\[:project\_id\]\)
  @part = @project\.parts\.build\(params\[:concept\]\)
  @part\.user\_id = @current\_user

  respond\_to do |format|
    if @concept\.save
      flash\[:notice\] = 'You have successfully defined a new part

for this project'
format.html { redirect_to part_path(@part) }
format.xml { render :xml => @part, :status
=> :created, :location => @part }
else
format.html { render :action => "new" }
format.xml { render :xml => @part.errors, :status
=> :unprocessable_entity }
end
end
end

++++++++++++++++
/views/parts/new.html
----------------------------

form_for(@project, @part) do |f|

What does the submit code look like?

Colin

I'm an idiot. Perils of copy-pasting controllers and then starting at
them cross-eyed at 11pm (local time).

I made the two changes above (@concept and []) and it took the process
forward. But now when I save, it seems to be trying to create a
project as well as a part, as it runs my new project validations and
complains about there being no project name, etc.

Colin, the submit code is:

submit_tag 'Save', :name => 'save'

And with fresh morning eyes, I solved that too.

(T'was another concept where it should have been part -- another
victim of copy-pasting controller code.)

s.