Allen
Allen

Reputation: 79

User Id reference issues

I am having issues with passing the user_id into the params by referencing it in a find_by. I have a pin number that a user will put in and what that does it gets checked to see if it is valid and if so then take that users id and place it in the params for saving in the database.

controller

class InventoriesController < ApplicationController
before_action :set_inventory, only: [:show, :edit, :update, :destroy]

  def index
    @inventories = Inventory.all
  end

  def show

  end

  def new
    @inventory = Inventory.new
  end

  def edit
  end

  def create
      if @user = User.find_by(pin: params[:pin])
        @inventory = Inventory.new(inventory_params)
        @inventory.user_id = @user.id
        if @inventory.valid?
          @inventory.save

        else

        end
      else

      end
  end

  def update
    respond_to do |format|
      if @inventory.update(inventory_params)
        format.html { redirect_to @inventory, notice: 'Inventory was successfully updated.' }
        format.json { render :show, status: :ok, location: @inventory }
      else
        format.html { render :edit }
        format.json { render json: @inventory.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @inventory.destroy
    respond_to do |format|
      format.html { redirect_to inventories_url, notice: 'Inventory was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def inventory_params
      params.require(:inventory).permit(:eq_type_id, :equipment_id, :chauffeur_id, :user_id)
    end

end

so this doesn't save can assume it's because user_id 1985 isn't a thing and when it gives me the error i see

"inventory"=>{"eq_type_id"=>"1", "equipment_id"=>"1", "chauffeur_id"=>"1", "user_id"=>"1985"}

which the incorrect way i want the user_id to be handled. It should be user_id: 1.

now, if i was to go into rails console and do

user = User.find_by(pin: 1985)

I get a user object:

#<User id: 1, email: "[email protected]", created_at: "2017-08-11 18:39:22", updated_at: "2017-08-11 18:40:22", admin: true, pin: "1985">

then if I initiate the new inventory

inventory = Inventory.new

I get the inventory object

#<Inventory id: nil, eq_type_id: nil, equipment_id: nil, chauffeur_id: nil, created_at: nil, updated_at: nil, user_id: nil>

I can then assign the user_id in the inventory object by

inventory.user_id = user.id

and then look at the inventory object again, i can see it was assigned.

#<Inventory id: nil, eq_type_id: nil, equipment_id: nil, chauffeur_id: nil, created_at: nil, updated_at: nil, user_id: 3>

now in the form the rest is filled in correctly and not showing "nil" but the user_id still comes up as 1985 for some reason. So I am not sure why in rails console it works but in the application it doesn't.

Upvotes: 1

Views: 40

Answers (1)

Pavan
Pavan

Reputation: 33542

As per the discussion, In your form you have

<%= form.label "Pin Number" %>
<%= form.text_field :user_id, id: :pin, class: "form-control" %>

So using params[:pin_number] is wrong as you used :user_id for text_field. It should be params[:inventory][:user_id]. Also you need to handle the failed save too. Your code should look like below.

def create
  @user = User.find_by(pin: params[:inventory][:user_id])
  @inventory = Inventory.new(inventory_params)
  @inventory.user_id = @user.id
  if @inventory.valid?
    @inventory.save
  else
    #write code to handle the failed save
  end
end

Upvotes: 1

Related Questions