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

**URL:** <https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390>\
**Category:** Rails\
**Created:** [September 12, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390 "2009-09-12T15:13:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 12, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390/1 "2009-09-12T15:13:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 12, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390/2 "2009-09-12T15:40:20Z")

</div>

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](http://www.rubypal.com))  
Ruby/Rails training, consulting, mentoring, code review  
Book: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://www.manning.com/black2))

---

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 12, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390/3 "2009-09-12T15:47:56Z")

</div>

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 🙂

David

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

---

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 12, 2009, 11:23pm UTC](https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390/4 "2009-09-12T23:23:51Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 12, 2009, 4:12pm UTC](https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390/5 "2009-09-12T16:12:31Z")

</div>

2009/9/12 steven\_noble [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> * * *
> 
> ## Â Â Â Â Â format.xml Â { render :xml =\> @part, :status /views/parts/new.html
> 
> form\_for(@project, @part) do |f|

What does the submit code look like?

Colin

---

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 13, 2009, 12:15am UTC](https://www.ruby-forum.com/t/after-creating-an-instance-of-an-object-rails-redirects-to-the-index-of-the-object-that-it-belong/175390/6 "2009-09-13T00:15:21Z")

</div>

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.
