Reputation: 1948
shop.rb
class Shop < ActiveRecord::Base
has_many :earning_rules
end
earning_rules.rb
class EarningRule < ApplicationRecord
belongs_to :user
belongs_to :shop
scope :order_rule, lambda {
where(status: 'true').where(name: 'Order').where(shop_id: shop.id)
}
validates :name, presence: true
validates :point, presence: true
def set_shop
shop = Shop.find_by(shopify_domain: current_shopify_domain)
self.shop = shop.id
end
end
earning_rules_controller.rb
def create
@earning_rule = current_user.earning_rules.new(earning_rule_params)
@earning_rule.set_shop
respond_to do |format|
if @earning_rule.save
format.html { redirect_to root_path, notice: 'Earning rule was successfully created.' }
format.json { render :show, status: :created, location: @earning_rule }
else
format.html { render :new }
format.json { render json: @earning_rule.errors, status: :unprocessable_entity }
end
end
end
def earning_rule_params
params.require(:earning_rule).permit(:user_id, :shop_id, :name, :point, :status)
end
form.html.erb
<%= simple_form_for earning_rule do |f| %>
<%= f.input :name %>
<%= f.input :point %>
<%= f.check_box :status, as: :boolean, checked_value: true, unchecked_value: false %>
<%= f.button :submit, class: 'btn btn-info btn-sm float-right ml-auto' %>
<% end %>
Log
I, [2019-11-20T09:42:01.490569 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Processing by EarningRulesController#create as HTML
I, [2019-11-20T09:42:01.490699 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Parameters: {"authenticity_token"=>"/BrJVX8bilaCc7yw/Hs0ngLh8O0DixA5lePv8bbLR1vpOBZ2MxBlk+qq7BCcM2xEh/XU8UjF6rWXUmZ1HQMf1Q==", "earning_rule"=>{"name"=>"nn", "point"=>"5", "status"=>"0"}, "commit"=>"Create Earning rule"}
D, [2019-11-20T09:42:01.492421 #92642] DEBUG -- : [96fe8592-245a-4537-8771-69a8261859e9] Shop Load (0.4ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
D, [2019-11-20T09:42:01.493549 #92642] DEBUG -- : [96fe8592-245a-4537-8771-69a8261859e9] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 3], ["LIMIT", 1]]
I, [2019-11-20T09:42:01.499620 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendering earning_rules/new.html.erb within layouts/embedded_app
I, [2019-11-20T09:42:01.507452 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered earning_rules/_form.html.erb (Duration: 7.1ms | Allocations: 2097)
I, [2019-11-20T09:42:01.507994 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered earning_rules/new.html.erb within layouts/embedded_app (Duration: 8.2ms | Allocations: 2223)
I, [2019-11-20T09:42:01.508949 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered shared/_navbar.html.erb (Duration: 0.3ms | Allocations: 179)
I, [2019-11-20T09:42:01.509206 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered layouts/_flash_messages.html.erb (Duration: 0.1ms | Allocations: 87)
I, [2019-11-20T09:42:01.509606 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Completed 200 OK in 19ms (Views: 10.2ms | ActiveRecord: 0.6ms | Allocations: 4864)
I dont know what I am missing. shop_id data is not in the log parameter and this works before I added shop associations. Any help will be appreciated.
Upvotes: 0
Views: 41
Reputation: 1259
shop = Shop.find_by(shopify_domain: current_shopify_domain)
self.shop = shop.id
I bet current_shopify_domain is not defined, so that the shop cannot be found. Also, your column is probably called shop_id? Add validation rules for the presence of user_id and shop_id in order to have Rails inform you of what's missing.
If that doesn't help, try creating an EarningRule in the console in order to narrow down the problem.
Upvotes: 1