2016-06-25 3 views
0

現在、フォームを送信して作成できます。しかし、問題は、作成された列であるnill値を持つテーブルに1つのフィールドがあることです。私はその値がどこに渡されるのかわからないので、それは私に何かエラーを投げかけません。おそらく、私の作成メソッドは、current_user.idの値をcommodity groupテーブルのcreated_byフィールドに渡すことができなかったと思います。ユーザーと商品グループの関係を正しく設定したと思います。 基本的には、ユーザーが商品グループを作成し、そのIDがテーブル商品グループの列created_byに保存され、商品グループの情報を更新する場合、そのIDは列update_byに保存されます私はこのような何かを行うことによってビューに自分の名前や電子メールを取得できるように:フォームを作成した後にパラメータ値を保存できませんでした。

<p> 
Created by: <%= @commodity_group.user.name %> 
Updated by: <%= @commodity_group.user.name %> 
</p> 

アップデート:私は何とかcurrent_user.idは今列CREATED_BYに保存することができますかわかりません。私は<% = @ commodity_group.user.name%>を呼び出すときしかし、私はこのエラーを取得する:ここで

undefined method `name' for nil:NilClass

は、すべてのファイルです:

user.rb:

class User < ActiveRecord::Base 
    extend FriendlyId 
    include Filterable 

    friendly_id :slug_candidates, use: :history 
    has_secure_password 
    acts_as_paranoid 

    has_many :activities 
    has_many :pricing_histories 

    has_many :commodity_groups_created, class_name: 'CommodityGroup', 
             foreign_key: :created_by 
    has_many :commodity_groups_updated, class_name: 'CommodityGroup', 
             foreign_key: :updated_by 
end 

commodity_group.rb:

class CommodityGroup < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :code, use: :history 

    belongs_to :created_user, 
      class_name: 'User', 
      primary_key: :id, 
      foreign_key: :created_by 
    belongs_to :updated_user, 
      class_name: 'User', 
      primary_key: :id, 
      foreign_key: :updated_by 

    validates_presence_of :code 
    validates_presence_of :name 
    # validates_presence_of :user 
end 

commodity_groups/_form.html.haml

%div.page-content-wrapper 
    %div.page-content 
    = render 'header' 
    %div.row 
     .col-md-12 
     .portlet.light.form-fit 
      .portlet-title 
      - if defined? title 
       .caption 
       %i.icon-user.font-blue-hoki 
       %span.caption-subject.font-blue-hoki.bold.uppercase 
        = title 
      .portlet-body.form 
      = form_for @commodity_group, html: {class: 'form-horizontal form-bordered form-label-stripped'} do |f| 
       .form-body 
       .form-group 
        %label.control-label.col-md-3 Code 
        .col-md-9 
        = f.text_field :code, placeholder: 'Code', class: 'form-control' 
       .form-group.last 
        %label.control-label.col-md-3 Name 
        .col-md-9 
        = f.text_field :name, placeholder: 'Name', class: 'form-control' 
       .form-actions 
       .row 
        .col-md-offset-3.col-md-9 
        %button.btn.green{:type => 'submit'} 
         %i.fa.fa-check 
         Submit 
        = link_to "Cancel", locations_path, class: 'btn default' 

commodity_groups_controller.rb:

class CommodityGroupsController < ApplicationController 
    before_action :set_commodity_group, only: [:show, :edit, :update, :destroy] 

    # GET /commodity_groups 
    def index 
    @commodity_groups = CommodityGroup.all 
    end 

    # GET /commodity_groups/1 
    def show 
    end 

    # GET /commodity_groups/new 
    def new 
    @commodity_group = current_user.commodity_groups_created.build 
    end 

    # GET /commodity_groups/1/edit 
    def edit 
    end 

    # POST /commodity_groups 
    def create 
    @commodity_group = current_user.commodity_groups_created.create(commodity_group_params) 

    if @commodity_group.save! 
     redirect_to commodity_groups_path, notice: init_message(:success, t('message.new_success', page_name: t('page_name.commodity_group'))) 
    else 
     redirect_to new_commodity_group_path, notice: init_message(:error, t('message.new_error', page_name: t('page_name.commodity_group'))) 
    end 
    end 

    # PATCH/PUT /commodity_groups/1 
    def update 
    if @commodity_group.update(commodity_group_params) 
     @commodity_group.update_columns(created_by: current_user.id) 

     redirect_to @commodity_group, notice: 'Commodity group was successfully updated.' 
    else 
     render :edit 
    end 
    end 

    # DELETE /commodity_groups/1 
    def destroy 
    @commodity_group.destroy 
    redirect_to commodity_groups_url, notice: 'Commodity group was successfully destroyed.' 
    end 

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

    # Only allow a trusted parameter "white list" through. 
    def commodity_group_params 
     params.require(:commodity_group).permit(:code, :name, :created_by, :updated_by, :slug) 
    end 
end 
+0

フォームはどこですか? – Saad

+0

保存しています!エラーを消す感嘆符を削除すると、エラーの原因となったエラーが表示される場合があります。 –

答えて

0

commodity_objectは、ユーザーのidので正しく保存されている場合、最初にチェック。

noの場合、フォームから送信されたフォームとのparamsをアップロード

してください。

あなたは間違っている同じコードを持つ2つのbelongs_toの関係を与えているので、それはそれはcreated_by and updated_byのために取るんどのような関係で混乱

commodity_group.rbファイルでは、YESの場合。

変更したファイルは、次のよう

class CommodityGroup < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :code, use: :history 

    belongs_to :created_user, 
      class_name: 'User', 
      primary_key: :id, 
      foreign_key: :created_by 
    belongs_to :updated_user, 
      class_name: 'User', 
      primary_key: :id, 
      foreign_key: :updated_by 

    validates_presence_of :code 
    validates_presence_of :name 
    # validates_presence_of :user 
end 

慎重に2人のbelongs_toの名を見て。 今、あなたは

<%= @commodity_group.created_user.name %> 
<%= @commodity_group.updated_user.name %> 

、created_userを使用して呼び出すことができるとupdated_userはassosciationsに基づいて取得されます。

+0

お寄せいただきありがとうございます。私は自分のcommodity_group.rbファイルを編集しました。しかし、私はupdateアクションでcreated_byカラムをどのように更新すればいいのだろうかと思います。私はcommodity_groups_controller.rbを更新しました。どうぞご覧ください。 –

関連する問題