steven_noble
2009-Sep-12 13:11 UTC
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: ++++++++++++++++ /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.
David A. Black
2009-Sep-12 13:38 UTC
Re: After creating an instance of an object, Rails redirects to the index of the object that it :belongs to
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.saveShould 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 (http://www.manning.com/black2)
David A. Black
2009-Sep-12 13:46 UTC
Re: After creating an instance of an object, Rails redirects to the index of the object that it :belongs to
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) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2)
Colin Law
2009-Sep-12 14:10 UTC
Re: After creating an instance of an object, Rails redirects to the index of the object that it :belongs to
2009/9/12 steven_noble <steven-IeilculjuCusTnJN9+BGXg@public.gmane.org>:> > 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> ---------------------------- > > 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. > > > > >
steven_noble
2009-Sep-12 21:22 UTC
Re: After creating an instance of an object, Rails redirects to the index of the object that it :belongs to
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'' On Sep 13, 12:10 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/9/12 steven_noble <ste...-IeilculjuCusTnJN9+BGXg@public.gmane.org>: > > > > > > > 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 > > > ---------------------------- > > > 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. > >
steven_noble
2009-Sep-12 22:13 UTC
Re: After creating an instance of an object, Rails redirects to the index of the object that it :belongs to
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. On Sep 13, 7:22 am, steven_noble <ste...-IeilculjuCusTnJN9+BGXg@public.gmane.org> wrote:> 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'' > > On Sep 13, 12:10 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > > > 2009/9/12 steven_noble <ste...-IeilculjuCusTnJN9+BGXg@public.gmane.org>: > > > > 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 > > > > ---------------------------- > > > > 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. > >