Ajax enables you to retrieve data for a web page without having to refresh the contents of the entire page. In the basic web architecture, the user clicks a link or submits a form. The form is submitted to the server, which then sends back a response. The response is then displayed for the user on a new page.
Rails has a simple, consistent model for how it implements Ajax operations. Once the browser has rendered and displayed the initial web page, different user actions cause it to display a new web page (like any traditional web application) or trigger an Ajax operation −
This example works based on scaffold, Destroy concept works based on ajax.
Here, we will provide, list, show and create operations on ponies table.
First, let us create an application.
The above command creates an application, now we need to call the app directory using with cd command. It will enter in to an application directory then we need to call a scaffold command. It will be done as follows −
Above command generates the scaffold with name and profession column. We need to migrate the data base as follows command
Now Run the Rails application as follows command
Now open the web browser and call a url as http://localhost:3000/ponies/new, The output will be as follows
Now open app/views/ponies/index.html.erb with suitable text editors. Update your destroy line with :remote => true, :class => 'delete_pony'.At finally, it looks like as follows.
Create a file, destroy.js.erb, put it next to your other .erb files (under app/views/ponies). It should look like this −
Now enter the code as shown below in destroy.js.erb
Now Open your controller file which is placed at app/controllers/ponies_controller.rb and add the following code in destroy method as shown below −
This is the final image of controller page.
Now run an application, Output called from http://localhost:3000/ponies/new, it will looks like as following image
Press on create pony button, it will generate the result as follows
Now click on back button, it will show all pony created information as shown image
Till now, we are working on scaffold, now click on destroy button, it will call a pop-up as shown below image, the pop-up works based on Ajax.
If Click on ok button, it will delete the record from pony and the final output will be as follows −
Ruby on Rails Related Interview Questions
|Python Interview Questions||HTML 5 Interview Questions|
|PHP Interview Questions||PostgreSQL Interview Questions|
|Ruby Interview Questions||Django Interview Questions|
|wxPython Interview Questions||MongoDB Interview Questions|
|Python Automation Testing Interview Questions||GitHub Interview Questions|
|Ruby Sinatra Interview Questions|
Ruby On Rails Tutorial
Ruby On Rails
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.