I have two models:
class Tool < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :tools, dependent: :destroy
end
And I created a migration to provide the foreign key of the model:
class AddUserIdToTool < ActiveRecord::Migration
def change
add_column :tools, :user_id, :integer
end
end
I have the following 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="field">
<%= f.file_field :tool_image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Whenever I submit the form the foreign key is not updated(user_id). I see the following while in my console:
irb(main):002:0> Tool.last
Tool Load (0.0ms) SELECT "tools".* FROM "tools" ORDER BY "tools"."id" DESC LIMIT 1
=> #<Tool id: 6, name: "wrench2", description: "another wrench", created_at: "2015-04-09 21:38:47", updated_at: "2015-04
-09 21:38:47", tool_image_file_name: "wrench.jpg", tool_image_content_type: "image/jpeg", tool_image_file_size: 3424, to
ol_image_updated_at: "2015-04-09 21:38:43", user_id: nil>
irb(main):003:0>
As you can see the user_id is nil
Here is my controller:
class ToolsController < ApplicationController
before_action :authenticate_user!, only: [:new, :destroy, :edit, :update], notice: 'you must sign in first!'
before_action :set_tool, only: [:show, :edit, :update, :destroy]
# GET /tools
# GET /tools.json
def index
@tools = Tool.all
end
# GET /tools/1
# GET /tools/1.json
def show
end
# GET /tools/new
def new
@tool = Tool.new
end
# GET /tools/1/edit
def edit
end
# POST /tools
# POST /tools.json
def create
@tool = Tool.new(tool_params)
respond_to do |format|
if @tool.save
format.html { redirect_to @tool, notice: 'Tool was successfully created.' }
format.json { render :show, status: :created, location: @tool }
else
format.html { render :new }
format.json { render json: @tool.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tools/1
# PATCH/PUT /tools/1.json
def update
respond_to do |format|
if @tool.update(tool_params)
format.html { redirect_to @tool, notice: 'Tool was successfully updated.' }
format.json { render :show, status: :ok, location: @tool }
else
format.html { render :edit }
format.json { render json: @tool.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tools/1
# DELETE /tools/1.json
def destroy
@tool.destroy
respond_to do |format|
format.html { redirect_to tools_url, notice: 'Tool was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tool
@tool = Tool.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tool_params
params.require(:tool).permit(:name, :description, :tool_image, :user_id)
end
end
How would I get the user_id updated with the id of the logged in user? Do I have to include a hidden field in my form or something like that?
Also do I have to permit the user_id variable within my strong parameters in my controller? I have tried this but it has not solved the problem...
How does this work?
Aucun commentaire:
Enregistrer un commentaire