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

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|


So, when I click Save in that button, I expect to end up on /views/
parts/show.html, with a nice [flash] reading ‘You have successfully
defined a new part for this project’. Instead, I end up on /views/
projects/index.html, with the [flash] message defined in the update
action in the projects controller.

What’s going on and how can I fix this?

Many thanks,

Steven.

Hi –

On Sat, 12 Sep 2009, steven_noble wrote:

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

Should that be @part.save? If so, I’m not sure why it doesn’t blow up
entirely, but anyway, that was the first thing that struck my eye.

David


David A. Black, Director
Ruby Power and Light, LLC (http://www.rubypal.com)
Ruby/Rails training, consulting, mentoring, code review
Book: The Well-Grounded Rubyist (The Well-Grounded Rubyist)

Hi –

On Sat, 12 Sep 2009, steven_noble wrote:

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


David A. Black, Director
Ruby Power and Light, LLC (http://www.rubypal.com)
Ruby/Rails training, consulting, mentoring, code review
Book: The Well-Grounded Rubyist (The Well-Grounded Rubyist)

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’

2009/9/12 steven_noble [email protected]:


     format.xml  { render :xml => @part, :status
/views/parts/new.html

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

What does the submit code look like?

Colin

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.