Sebastian Peter
Sebastian Peter

Reputation: 195

Rails 5 multiple edit views for one update controller action

I am programming an app for people with Diabetes to calculate the insulin the person needs. That means a user profile consists of his name email and password and also of factors for different times of the day. The database schema is:

ActiveRecord::Schema.define(version: 20180115094430) do

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "password_digest"
    t.string "remember_digest"
    t.boolean "admin"
    t.string "activation_digest"
    t.boolean "activated"
    t.datetime "activated_at"
    t.string "reset_digest"
    t.datetime "reset_sent_at"
    t.float "zero_till_one"
    t.float "one_till_two"
    t.float "two_till_three"
    t.float "three_till_four"
    t.float "four_till_five"
    t.float "five_till_six"
    t.float "six_till_seven"
    t.float "seven_till_eight"
    t.float "eight_till_nine"
    t.float "nine_till_ten"
    t.float "ten_till_eleven"
    t.float "eleven_till_twelve"
    t.float "twelve_till_thirteen"
    t.float "thirteen_till_fourteen"
    t.float "fourteen_till_fifteen"
    t.float "fifteen_till_sixteen"
    t.float "sixteen_till_seventeen"
    t.float "seventeen_till_eightteen"
    t.float "eightteen_till_nineteen"
    t.float "nineteen_till_twenty"
    t.float "twenty_till_twentyone"
    t.float "twentyone_till_twentytwo"
    t.float "twentytwo_till_twentythree"
    t.float "twentythree_till_zero"
  end

end

When I create a user, he is just created with name, email and password. When signed up and logged in the user can edit his profile via RESTful routes user/1/edit. I want the user to be able to edit the still empty t.float "zero_till_one" etc. columns via user/1/edit/diabsettings and still be able to use the same update action inside the users_controller.rb to update the database:

def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user

    else
      render 'edit'
    end
  end

How can I achieve this. Any help highly appreciated.

Best Sebastian

Upvotes: 0

Views: 765

Answers (1)

Anand
Anand

Reputation: 6531

1- create a edit action

def edit
  @user = User.find(params[:id])
end

2- edit.html.erb

<%=form_for @user, :html => { id: "edit_user", class: "your_class"} do |f|%>
  <%=f.text_field :name, class: 'form-control'%>
  <%=f.text_field :email,class: 'form-control'%>
  <%=f.text_field :password,class: 'form-control'%>
  <% content_tag :button, :type => :submit, class: 'btn btn-primary' do %>
    Update User
  <% end %>
<%end%>

3 - create a edit_diabsettings action

  def edit_diabsettings
    @user = User.find(params[:id])
  end

4- in edit_diabsettings.html.erb craete form to edit other than name, email,password:-

<%=form_for @user, :html => { id: "edit_diabsettings", class: "your_class"} do |f|%>
  <%=f.text_field :zero_till_one, class: 'form-control'%>
  <%=f.text_field :one_till_two,class: 'form-control'%>
  <%=f.text_field :two_till_three,class: 'form-control'%>

  #.... and so on that should be editable here
  <% content_tag :button, :type => :submit, class: 'btn btn-primary' do %>
    Update User
  <% end %>
<%end%>

5- make update action to update all field

def update
    @user = User.find(params[:id])
    if @user.update(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

6- create a private action for user_params

private

    def user_params
      params.require(:user).permit!
    end

7- as you need to create a separate action for edit_diabsettings action in routes.rb

get 'user/:id/edit/diabsettings', to: 'users#edit_diabsettings', as: : edit_diabsettings

Upvotes: 1

Related Questions