Jump to content

The ultimate community for Ruby on Rails developers.


Photo

wanting "create" action over and over, but an update pops in??

rails creae action update

  • Please log in to reply
2 replies to this topic

#1 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 08 November 2013 - 02:05 PM

Well, I've got a puzzle again...  In summary, I have a very simple scaffolding and controller relating to job_sub_categories.. I type  http://localhost:300..._categories/new (see image below) and get into the "new" view. In this view I enter the job_sub_category and the job_category_id, then click the submit button.  Everything works great, row inserted and my "new" view is rendered upon completion for another insert.

 

Now in the attempt of creating a second row, I type in a new job_sub_category, enter the same job_category_id,  click submit, and to my surprise, the Update action is called not the create???  I noticed that in the parm list in the debugger, the id of the previous row's insertion was added to the parm list.

 

Processing by JobSubCategoriesController#update as HTML
  Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"Ltrw20ZAhZxd8o99b8W+g+weHPxzyFPpc/annCOePAo=", "job_sub_category"=>{"name"=>"test8", "j
job_category_id"=>"12"}, "commit"=>"Create job category", "id"=>"62"}
  JobSubCategory Load (15.6ms)  SELECT `job_sub_categories`.* FROM `job_sub_categories` WHERE `job_sub_categories`.`id` = 62 LIMIT 1
   (0.0ms)  BEGIN
  SQL (0.0ms)  UPDATE `job_sub_categories` SET `name` = 'test8', `updated_at` = '2013-11-08 13:40:19' WHERE `job_sub_categories`.`id` = 62
   (46.8ms)  COMMIT

 

This is very puzzling and frustrating as my code can't get any simpler than this...

 

Here is my controller:

 

 def create
    @job_sub_category = JobSubCategory.new(job_sub_category_params)
    @job_categories = JobCategory.all

      if @job_sub_category.save
        flash[:success] = "Welcome to the Sample App!"
        render 'new'    
      else
        flash[:success] = "Error in save!"
        render 'new'
      end
  end

 

Since I have no links yet to get to the page pragmatically, I initially enter http://localhost:300..._categories/new  Then I enter my data, click submit.  The first time it works and row is inserted.  My page after successful save renders back to the same "new" page, even my url again shows http://localhost:300..._categories/new  but no matter what I enter in the two input fields,  it will update the previous row that I inserted.  booooo! 

 

Any ideas, im not sure what else to show you other than the create action...  There;'s not much else.  Note the @job_categories = JobCategory.all line in the create action is just so I can show the list of categories and their id's for the user to see as he's creating a job_sub_category.  All this is just test code for me to learn, production will be much better, if I can get it to work..

 

 

 



#2 rayray

rayray

    Signalman

  • Members
  • 15 posts

Posted 08 November 2013 - 02:31 PM

You have to make a redirect instead of "render 'new'" if the record get's saved:

if @job_sub_category.save
 flash[:success] = ...
 redirect_to new_job_sub_category_path
else
 ...
end

Render will not call the controller action, so the record that you have created in the create action will be taken to the new view.

 

And by the way, the @job_categories = JobCategory.all is only necessary in the new action.


I'm Jack's complete lack of ruby knowledge.


#3 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 08 November 2013 - 02:51 PM

You have to make a redirect instead of "render 'new'" if the record get's saved:

if @job_sub_category.save
 flash[:success] = ...
 redirect_to new_job_sub_category_path
else
 ...
end

Render will not call the controller action, so the record that you have created in the create action will be taken to the new view.

 

And by the way, the @job_categories = JobCategory.all is only necessary in the new action.

 

Yes, thank you that worked...  ok, now I need to figure out what the differences are, googled some good articles on that, ugg there goes the next hour,,,






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users