ruby - Active Record Association not saving in database -
i have 2 models:
class tool < activerecord::base belongs_to :user end class user < activerecord::base has_many :tools, dependent: :destroy end
and created migration provide foreign key of model:
class adduseridtotool < activerecord::migration def change add_column :tools, :user_id, :integer end end
i have following form:
<%= form_for @tool, :html => { :multipart => true } |f| %> <% if @tool.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@tool.errors.count, "error") %> prohibited tool being saved:</h2> <ul> <% @tool.errors.full_messages.each |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 submit form foreign key not updated(user_id). see following while in console:
irb(main):002:0> tool.last tool load (0.0ms) select "tools".* "tools" order "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, ol_image_updated_at: "2015-04-09 21:38:43", user_id: nil> irb(main):003:0>
as can see user_id
nil
here 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] # /tools # /tools.json def index @tools = tool.all end # /tools/1 # /tools/1.json def show end # /tools/new def new @tool = tool.new end # /tools/1/edit def edit end # post /tools # post /tools.json def create @tool = tool.new(tool_params) respond_to |format| if @tool.save format.html { redirect_to @tool, notice: 'tool 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 |format| if @tool.update(tool_params) format.html { redirect_to @tool, notice: 'tool 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 |format| format.html { redirect_to tools_url, notice: 'tool destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_tool @tool = tool.find(params[:id]) end # never trust parameters scary internet, allow white list through. def tool_params params.require(:tool).permit(:name, :description, :tool_image, :user_id) end end
how user_id updated id of logged in user? have include hidden field in form or that?
also have permit user_id variable within strong parameters in controller? have tried has not solved problem...
how work?
currently building tool on own. associate user model need create tool model in following way:
@user.tools.build
it create new activerecord tool model proper user_id of belonged model. parent need fetch id params. done in such way, in action:
@user = user.find(params[:user_id])
note: make possible routes should nested correspondingly (read more here: http://guides.rubyonrails.org/routing.html)
so, in all, new action should like:
def new @user = user.find(params[:user_id]) @tool = @user.tools.build end
and in routes.rb
resources :users resources :tools end