title: How to use the same form for creating and editing in ruby on rails
body: <p>I am new to ruby. I have the same form but i need it to perform creation/updation as and when required. The problem i have now is that whenever i call edit instead of editing the details of an existing user, i am getting a new user with the edited details. To put it simply, i think whenever i perform edit, the create method is being called.</p>
  
  <p>So is there any way to use single form for both new and edit instead of using separate forms.</p>
  
  <p><strong>The following code is for editing user details:</strong></p>
  
  <pre><code>         &lt;%= link_to 'Home', root_path %&gt;
          &lt;h2&gt;Edit user&lt;/h2&gt;
          &lt;%= form_for :user, url: user_index_path do |f| %&gt;
            &lt;p&gt;
              &lt;%= f.label :name %&gt;&lt;br&gt;
              &lt;%= f.text_field :name %&gt;
            &lt;/p&gt;
  
            &lt;p&gt;
              &lt;%= f.label :address %&gt;&lt;br&gt;
              &lt;%= f.text_area :address %&gt;
            &lt;/p&gt;
           &lt;p&gt;
              &lt;%= f.label :email %&gt;&lt;br&gt;
              &lt;%= f.text_field :email %&gt;
            &lt;/p&gt; 
           &lt;p&gt;
              &lt;%= f.label :phone %&gt;&lt;br&gt;
              &lt;%= f.text_field :phone %&gt;
            &lt;/p&gt;
            &lt;p&gt;
              &lt;%= f.label :state %&gt;&lt;br&gt;
              &lt;%= f.text_field :state %&gt;
            &lt;/p&gt;
            &lt;p&gt;
              &lt;%= f.label :country %&gt; 
              &lt;%= f.collection_select(:country, Country.all, :name, :name) %&gt;
            &lt;/p&gt;
            &lt;p&gt;
              &lt;%= f.submit %&gt;
            &lt;/p&gt;
          &lt;% end %&gt;
  </code></pre>
  
  <p><strong>The following code is for creating user:</strong></p>
  
  <pre><code>        &lt;%= link_to 'Home', root_path %&gt;
      &lt;h2&gt;Create new user&lt;/h2&gt;
      &lt;%= form_for :user, url: user_index_path do |f| %&gt;
        &lt;p&gt;
          &lt;%= f.label :name %&gt;&lt;br&gt;
          &lt;%= f.text_field :name %&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
          &lt;%= f.label :address %&gt;&lt;br&gt;
          &lt;%= f.text_area :address %&gt;
        &lt;/p&gt;
       &lt;p&gt;
          &lt;%= f.label :email %&gt;&lt;br&gt;
          &lt;%= f.text_field :email %&gt;
        &lt;/p&gt; 
       &lt;p&gt;
          &lt;%= f.label :phone %&gt;&lt;br&gt;
          &lt;%= f.text_field :phone %&gt;
        &lt;/p&gt;
        &lt;p&gt;
          &lt;%= f.label :state %&gt;&lt;br&gt;
          &lt;%= f.text_field :state %&gt;
        &lt;/p&gt;
        &lt;p&gt;
          &lt;%= f.label :country %&gt; 
          &lt;%= f.collection_select(:country, Country.all, :name, :name) %&gt;
        &lt;/p&gt;
        &lt;p&gt;
          &lt;%= f.submit %&gt;
        &lt;/p&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p><strong>And this is my controller:</strong></p>
  
  <pre><code>      class UserController &lt; ApplicationController
      def index
          @users = User.all
  
      end
  
      def show
      @user = User.find(params[:id])
    end
  
      def new
          @user = User.new
      end
  
  
      def create
      @user = User.new(user_params)
          if @user.save
              redirect_to @user
          else
              render 'new'
          end 
      end
  
    def edit
  
      @user = User.find(params[:id])
  
  
    end
  
      def update
      #@user = User.find(params[:id])
          if @user.update(user_params)
          redirect_to @user
      else
        render 'edit'
      end
      end
  
      def destroy
      @user = User.find(params[:id])
      @user.destroy
        redirect_to user_index_path
    end
  
      private
      def user_params
          params.require(:user).permit(:name, :address, :email, :phone, :state, :country)
      end
  
  end
  </code></pre>
  
  <p>Since i am new to ruby, i did not get the exact details of how http requests function in ruby.</p>