coderwannabe2
coderwannabe2

Reputation: 221

user_id = nil on a non-nested active record association

I am having trouble getting my reference key to update when I build from my controller. I am not sure what the rails convention for controller code is for this kind of thing and can't find the info anywhere.

My Tool's user_id column is always nil after I create a Tool and not sure what is missing.

I want to do this without nesting my routes, how can I do this without nesting my routes?

This is my Models(I am using Devise for users):

class Tool < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :tools
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

This is my controller:

class ToolsController < ApplicationController
before_action :set_tool, only:[:show, :edit, :update, :destroy]
before_action :authenticate_user!, only:[:new, :destroy, :edit], notice: 'you must be logged in to proceed'

    def index
        @tools = Tool.all
    end

    def show
    end

    def new
        @user = current_user
        @tool = @user.tools.build
    end

    def create
        @tool = Tool.new(tool_params)
        @tool.save
        redirect_to @tool
    end

    def edit
    end

    def update
        @tool.update(tool_params)
        redirect_to @tool
    end

    def destroy
        @tool.destroy
        redirect_to tools_path
    end

    private

    def set_tool
        @tool = Tool.find(params[:id])
    end

    def tool_params
        params.require(:tool).permit(:name, :description)
    end

end

This is my form:

<%= form_for [@tool], :html => { :multipart => true } do |f| %>
  <% if @tool.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@tool.errors.count, "error") %> prohibited this tool from being saved:</h2>

      <ul>
      <% @tool.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

These are my routes:

Rails.application.routes.draw do
  devise_for :users

   root 'tools#index'

   resources :tools
end

This is my schema:

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

  create_table "tools", force: :cascade do |t|
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
    t.string   "name"
    t.text     "description"
  end

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

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

end

Upvotes: 0

Views: 52

Answers (1)

Paweł Dawczak
Paweł Dawczak

Reputation: 9639

In create method, you should change

@tool = Tool.new(tool_params)

To similar one from new

@tool = current_user.tools.build(tool_params)

Good luck!

Upvotes: 1

Related Questions