Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1052883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:09:58+00:00 2026-05-16T17:09:58+00:00

Ok, I have a model that is very simple: ServiceType(id: integer, title: string, duration:

  • 0

Ok, I have a model that is very simple:

ServiceType(id: integer, title: string, duration: integer, created_at: datetime, updated_at: datetime)

Because its so simple, It’s ridiculous that I should have a separate page for creating and updating those types of records. What I would like to do is have a form on my index page which I can use to create new records and if I click the edit link, I would like the form to load that record so that the form doubles as a create/update form.

Has anyone already done this before or does anyone know of a blog article somewhere that shows how to do this?

EDIT: Using the blog link posted below by Laheab I am at this point:
If you are using Rails 3, Laheab’s link is what you want, but BE WARNED!! The guy who wrote it left a ton of obvious errors in his code.. look for bad comment blocks like */ */ instead of /* */ and look for times where he is using jQuery but leaves off the $.

IF you are using Rails 2.3.x, I have posted my modified version of this as an answer.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:09:59+00:00Added an answer on May 16, 2026 at 5:09 pm

    If you are running Rails 2.3.x, you need to modify what the guy does in this blog link.

    This is what I did to get create/edit/destroy(this isn’t working yet, it still renders destroy.js.erb as html for some reason) it to work:

    make rails app:

    $ rails post_app
    $ cd post_app
    $ script/generate scaffold Post title:string content:text
    $ rake db:migrate
    

    And now add/replace the following files:

    public/javascripts/application.js (see Railscasts episode 136 http://railscasts.com/episodes/136-jquery):

    // Setting up ajax for sending javascript requests
    jQuery.ajaxSetup({
      'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
    });
    
    jQuery.fn.submitWithAjax = function() {
      this.submit(function() {
        $.post(this.action, $(this).serialize(), null, "script");
        return false;
      });
      return this;
    };
    
    jQuery.fn.getWithAjax = function() {
      this.click(function() {
        $.get(this.href, null, null, "script");
        return false;
      });
      return this;
    };
    
    jQuery.fn.postWithAjax = function() {
      this.click(function() {
        $.post(this.href, null, null, "script");
        return false;
      });
      return this;
    };
    

    app/controllers/posts_controller:

    class PostsController < ApplicationController
      before_filter :load
    
      def load
        @posts = Post.all
        @post = Post.new
      end
    
      def index
      end
    
      def edit
        @post = Post.find(params[:id])
        respond_to do |format|
          format.all {render :file => "#{RAILS_ROOT}/public/404.html", :status => '404 Not Found'}
          format.js {render :layout => false}
        end
      end
    
      def create
        @post = Post.new(params[:post])
    
        if @post.save
          flash[:notice] = 'Post was successfully created.'
          @posts = Post.all
        end
    
        respond_to do |format|
          format.js {render :layout => false }
        end
      end
    
      def update
        @post = Post.find(params[:id])
    
        if @post.update_attributes(params[:post])
          flash[:notice] = 'Post was successfully updated.'
          @posts = Post.all
        end
        respond_to do |format|
          format.js {render :layout => false }
        end
      end
    
      def destroy
        @post = Post.find(params[:id])
        @post.destroy
        flash[:notice] = "Successfully destroyed post."
        @posts = Post.all
        respond_to do |format|
          format.js {render :layout => false }
        end
      end
    end
    

    app/views/layouts/posts.html.erb

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
      <title>Posts: <%= controller.action_name %></title>
      <%= stylesheet_link_tag 'scaffold' %>
      <%= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', 'application' %>
      <%= javascript_include_tag 'rails' %>
      <%= yield :head %>
    </head>
    <body>
      <div id="container">
        <div id="flash_notice" style="display: none; color: green"></div>
        <%= yield %>
      </div>
    </body>
    </html>
    

    under app/views/posts delete EVERYTHING and add the following files:

    index.html.erb:

    <%- content_for :head do -%>
      <%= javascript_include_tag '/posts.js' %>
    <% end %>
    <h1>Listing posts</h1>
    <%= render :partial => "form" %>
    <div id="posts_list">
    <%= render :partial => "posts" %>
    </div>
    

    index.js.erb:

    // Setting up the ajax requests for the forms/links.
    $(document).ready(function() {
      $("#new_post").submitWithAjax();
      $("a.edit").each(function(){
        $(this).getWithAjax();
      });
      $("a.destroy").each(function(){
        $(this).postWithAjax();
      });
    });
    

    create.js.erb:

    <% if @post.errors.any? -%>
      /*Hide the flash notice div*/
      $("#flash_notice").hide(300);
    
      /*Update the html of the div post_errors with the new one*/
      $("#post_errors").html("<%= escape_javascript(error_messages_for(@post))%>");
    
      /*Show the div post_errors*/
      $("#post_errors").show(300);
    <% else -%>
      /*Hide the div post_errors*/
      $("#post_errors").hide(300);
    
      /*Update the html of the div flash_notice with the new one*/
      $("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
    
      /*Show the flash_notice div*/
      $("#flash_notice").show(300);
    
      /*Clear the entire form*/
      $(":input:not(input[type=submit])").val("");
    
      /*Replace the html of the div post_lists with the updated new one*/
      $("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
    <% end -%>
    

    destroy.js.erb:

    $("#post_errors").hide(300);
    $("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
    $("#flash_notice").show(300);
    $("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
    

    _posts.erb:

    <table>
      <tr>
        <th>Title</th>
        <th>Content</th>
      </tr>
      <% for post in @posts %>
        <tr>
          <td><%= post.title %></td>
          <td><%= post.content %></td>
          <td><%= link_to "Edit", edit_post_path(post), :class => "edit" %></td>
          <td><%= link_to "Destroy", post, :confirm => 'Are you sure?', :method => :delete, :class => 'destroy' %></td>
        </tr>
      <% end %>
    </table>
    

    edit.js.erb:

    $("#new_post").html("<%= escape_javascript(render(:partial => "form"))%>");
    $("#post_title").val('<%= escape_javascript(@post.title)%>');
    $("#post_content").val('<%= escape_javascript(@post.content)%>');
    

    _form.erb:

    <% form_for @post do |f| %>
      <div id="post_errors" style="display:none"></div>
      <p>
        <%= f.label :title %><br/>
        <%= f.text_field :title %>
      </p>
      <p>
        <%= f.label :content %><br/>
        <%= f.text_area :content, :rows => 5 %>
      </p>
      <p><%= f.submit %></p>
    <% end %>
    

    update.js.erb:

    <% if @post.errors.any? -%>
      $("#flash_notice").hide(300);
      $("#post_errors").html("<%= escape_javascript(error_messages_for(@post))%>");
      $("#post_errors").show(300);
    <% else -%>
      $("#post_errors").hide(300);
      $("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
      $("#flash_notice").show(300);
      $(":input:not(input[type=submit])").val("");
      $("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
    <% end -%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a very simple payments model with the default created_at datetime field that
I have a very simple model that includes the auto-filled field much like 'created'.
i have very simple problem. I need to create model, that represent element of
I have a very simple class that wont compile because of a default parameter
I have a very simple user model. When I first created it, it had
Consider a very simple model where we have locations and each location can have
I have a very simple setup... A route is setup that calls a modal
Hey, I have a maybe very simple problem using the PreLoadFcn in my model.
I have a very simple rails 3 program with 2 models: a user model
I have created a very simple model called Discussion and one of the columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.