ruby-on-railsruby-on-rails-4devisesimple-form

Displaying users on a form using simple form


I am using devise and simple form in my rails project. I want to display all the current users on the form and then be able to enter in sales info for each corresponding employee. I have been able to iterate through the employee table and display them, I can create a form and enter in the sales info and have it be related to the user, but how can I combine the two?

Schema.rb

ActiveRecord::Schema.define(version: 20160812021027) do
  create_table "sales", force: :cascade do |t|
  t.datetime "created_at",                                       null:     false
  t.datetime "updated_at",                                       null: false
  t.integer  "user_id",       limit: 4
  t.decimal  "service_sales",           precision: 10, scale: 2
  t.decimal  "retail_sales",            precision: 10, scale: 2
end   

create_table "users", force: :cascade do |t|
  t.string   "email",                  limit: 255, default: "", null: false
  t.string   "encrypted_password",     limit: 255, default: "", null: false
  t.string   "reset_password_token",   limit: 255
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          limit: 4,   default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip",     limit: 255
  t.string   "last_sign_in_ip",        limit: 255
  t.datetime "created_at",                                      null: false
  t.datetime "updated_at",                                      null: false
  t.string   "first_name",             limit: 255
  t.string   "last_name",              limit: 255
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

end

Sales Controller

class SalesController < ApplicationController
  before_action :set_sale, only: [:show, :edit, :update, :destroy]

  # GET /sales
  # GET /sales.json
  def index
    @sales = Sale.all
    @users = User.order(last_name: :desc)
  end

  # GET /sales/1
  # GET /sales/1.json
  def show
     @sales = Sale.find(params[:id])
  end

  # GET /sales/new
  def new
    @sale = Sale.new
    @user = User.all
  end

  # GET /sales/1/edit
  def edit
  end

  # POST /sales
  # POST /sales.json
  def create
    @sale = Sale.new(sale_params[:user_id])
    @sale.user = current_user

    respond_to do |format|
      if @sale.save
        format.html { redirect_to @sale, notice: 'Sale was successfully created.' }
        format.json { render :show, status: :created, location: @sale }
      else
        format.html { render :new }
        format.json { render json: @sale.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /sales/1
  # PATCH/PUT /sales/1.json
  def update
    respond_to do |format|
      if @sale.update(sale_params)
        format.html { redirect_to @sale, notice: 'Sale was successfully updated.' }
        format.json { render :show, status: :ok, location: @sale }
      else
        format.html { render :edit }
        format.json { render json: @sale.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /sales/1
  # DELETE /sales/1.json
  def destroy
    @sale.destroy
    respond_to do |format|
      format.html { redirect_to sales_url, notice: 'Sale was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_sale
      @sale = Sale.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def sale_params
      params.require(:sale).permit(:service_sale, :retail_sale, :user, :user_id)
    end
end

Sale Model

class Sale < ActiveRecord::Base
    belongs_to :user
end

User Controller

 class User < ActiveRecord::Base
   # Include default devise modules. Others available are:
   # :confirmable, :lockable, :timeoutable and :omniauthable
   devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable
  has_many :sales
end

form.html.erb

<div class="row">
  <div class="small-6 large-centered columns">
    <table>
      <tr>
        <th>Name</th>
        <th>Service Sales</th>
        <th>Retail Sales</th>
      </tr>
      <tr>
        <% @users.each do |user| %>
          <td>
            <%= user.last_name %>,
              <%= user.first_name %>
          </td>
          <% end  %>
            <%= simple_form_for(@sale) do |f| %>
              <%= f.error_notification %>
                <div class="form-inputs">
                  <td>
                    <%= f.input :service_sales %>
                  </td>
                  <td>
                    <%= f.input :retail_sales %>
                  </td>
                </div>
      </tr>
    </table>
    <div class="form-actions">
      <%= f.button :submit %>
    </div>
    <% end %>
  </div>
</div>

Solution

  • Use @users insted of @user in controller

    def new
       @sale = Sale.new
       @users = User.all
    end