使用简单表单在表单上显示用户

时间:2016-08-13 02:34:00

标签: ruby-on-rails ruby-on-rails-4 devise simple-form

我在rails项目中使用设计和简单的形式。我想在表单上显示所有当前用户,然后能够输入每个相应员工的销售信息。我一直在迭代员工表并显示它们,我可以创建一个表单并输入销售信息并让它与用户相关但由于某种原因我似乎无法将两者结合起来。我也很喜欢绿色。

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

销售控制器

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

促销模式

class Sale < ActiveRecord::Base
    belongs_to :user
end

用户控制器

 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>

先谢谢

1 个答案:

答案 0 :(得分:2)

在控制器

中使用@users @user
def new
   @sale = Sale.new
   @users = User.all
end