I'm new to ruby. I have the same form, but I need it to create / update as needed. The problem is that whenever I call editing, and not edit information about an existing user, I get a new user with the edited data. Simply put, I think that whenever I do the editing, the create method is called.
So, is there a way to use one form for new and for editing instead of using separate forms.
The following code is for editing user information:
<%= link_to 'Home', root_path %> <h2>Edit user</h2> <%= form_for :user, url: user_index_path do |f| %> <p> <%= f.label :name %><br> <%= f.text_field :name %> </p> <p> <%= f.label :address %><br> <%= f.text_area :address %> </p> <p> <%= f.label :email %><br> <%= f.text_field :email %> </p> <p> <%= f.label :phone %><br> <%= f.text_field :phone %> </p> <p> <%= f.label :state %><br> <%= f.text_field :state %> </p> <p> <%= f.label :country %> <%= f.collection_select(:country, Country.all, :name, :name) %> </p> <p> <%= f.submit %> </p> <% end %>
The following code is for creating a user:
<%= link_to 'Home', root_path %> <h2>Create new user</h2> <%= form_for :user, url: user_index_path do |f| %> <p> <%= f.label :name %><br> <%= f.text_field :name %> </p> <p> <%= f.label :address %><br> <%= f.text_area :address %> </p> <p> <%= f.label :email %><br> <%= f.text_field :email %> </p> <p> <%= f.label :phone %><br> <%= f.text_field :phone %> </p> <p> <%= f.label :state %><br> <%= f.text_field :state %> </p> <p> <%= f.label :country %> <%= f.collection_select(:country, Country.all, :name, :name) %> </p> <p> <%= f.submit %> </p> <% end %>
And this is my controller:
class UserController < 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
Since I am new to ruby, I have not received accurate data on how http requests work in ruby.