Jump to content

The ultimate community for Ruby on Rails developers.


How can i show the new form and edit form in the same page using jquery?

ruby-on-rails jquery

  • Please log in to reply
2 replies to this topic

#1 Logesh



  • Members
  • 14 posts

Posted 16 August 2013 - 05:05 AM

I have a requirement where i need to show the new form and edit form in the same page where i list the items. Consider i have run "rails generate scaffold Item name:string" which has created all that is generated by default and now i used jquery to show the new form in the index page by making it slide down on clicking the text but how can i make edit form to appear as we have to pass the id and populate the fields with the content it has. I am stuck with this please help me.

#2 Kelli Shaver

Kelli Shaver


  • Administrators
  • 76 posts
  • LocationKentucky

Posted 16 August 2013 - 04:25 PM

It should be as simple as having your edit action render the form view as normal, except that you would add layout: false to the render call so your application layout doesn't render around it, and then fetch it via jQuery's $.ajax() (or $.get() or $.load()). You can then post the data the same way and render the response back into the page, etc. However you want to set it up.

#3 Vell



  • Members
  • 100 posts
  • LocationWashington, DC

Posted 23 August 2013 - 04:10 AM

If you are going to use the existing templates I would do the following:


  • change the edit.html.erb to a partial
  • add 2 divs to the view. 1 for viewing the edit page and 1 for displaying errors
  • create or edit the url to pass the id in params to the edit action
  • edit action should have your instance variable that you set with Model.find(params[:id]) or something to that effect.
  • create the edit.js.erb file with my jquery code to check checks if the instance variable exists. If so show the edit page else render an error. (each should be set to the appropriate div)

Also with layout: false since you don't want the layout to render as Kelli suggested.


hope this helps.

Also tagged with one or more of these keywords: ruby-on-rails, jquery

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users