this is index.html.erb
<p>
<%= number_to_currency(product.price) %>
<%= button_to 'Add to Cart', cart_items_path(:product_id => product) %>
</p>
cart.rb
class Cart
include Mongoid::Document
include Mongoid::Timestamps
# references_many :cart_items, :dependent => :destroy
has_many :cart_items, :dependent => :destroy , :autosave => true
# embeds_many :cart_items
# accepts_nested_attributes_for :cart_items
# attr_accessible :cart_items_attributes
end
cart_item.rb
class CartItem
include Mongoid::Document
include Mongoid::Timestamps
# referenced_in :cart
# embedded_in :cart
belongs_to :cart
belongs_to :product
# embedded_in :product
field :product_id, :type => Integer
field :cart_id, :type => Integer
field :quantity, :type => Integer
field :price, :type => Float
end
application_controller.rb
...
private
current cart call anywhere in this app
def current_cart
cart = if session[:cart_id]
Cart.find(session[:cart_id])
else
Cart.create!
end
session[:cart_id] = cart.id
end
cart_items.rb
...
def create
#cart = current_cart
product = Product.find(params[:product_id])
#cart_item = #cart.cart_items.build(:product => product)
# #cart_item = CartItem.new(params[:product => product])
# #cart_item = CartItem.new(params[:cart_item])
respond_to do |format|
if #cart_item.save
format.html { redirect_to(#cart_item, notice: 'Cart item was successfully created.') }
format.json { render json: #cart_item, status: :created, location: #cart_item }
else
format.html { render action: "new" }
format.json { render json: #cart_item.errors, status: :unprocessable_entity }
end
end
end
....
product.rb
require 'carrierwave/orm/mongoid'
class Product
include Mongoid::Document
include Mongoid::Timestamps
# include Mongoid::Observing
# include Mongoid::Tree
has_many :line_items
# before_destroy :ensure_not_referenced_by_any_line_item
field :title, :type => String
field :description, :type => String
# field :image, :type => String
mount_uploader :image, ImageUploader
field :price, :type => Float
field :category_id, :type => Integer
field :stock_level, :type => Integer
set_callback(:destroy, :before) do |document|
document.ensure_not_referenced_by_any_line_item
end
protected
def ensure_not_referenced_by_any_line_item
if cart_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
end
and the result is this:
NoMethodError in CartItemsController#create
undefined method `cart_items' for BSON::ObjectId('4e156c78421aa918eb000005'):BSON::ObjectId
Rails.root: /Users/user/railsworks/rcart
Application Trace | Framework Trace | Full Trace
app/controllers/cart_items_controller.rb:46:in create'
actionpack (3.1.0.rc4) lib/action_controller/metal/implicit_render.rb:4:insend_action'
actionpack (3.1.0.rc4) lib/abstract_controller/base.rb:167:in process_action'
actionpack (3.1.0.rc4) lib/action_controller/metal/rendering.rb:10:inprocess_action'
actionpack (3.1.0.rc4) lib/abstract_controller/callbacks.rb:18:in block in process_action'
activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:416:inrun_1145394839032970144__process_action_2623355606222143076_callbacks'
activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:386:in _run_process_action_callbacks'
activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:inrun_callbacks'
actionpack (3.1.0.rc4) lib/abstract_controller/callbacks.rb:17:in process_action'
actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:30:inblock in process_action'
activesupport (3.1.0.rc4) lib/active_support/notifications.rb:55:in block in instrument'
activesupport (3.1.0.rc4) lib/active_support/notifications/instrumenter.rb:21:ininstrument'
activesupport (3.1.0.rc4) lib/active_support/notifications.rb:55:in instrument'
actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:29:inprocess_action'
actionpack (3.1.0.rc4) lib/action_controller/metal/params_wrapper.rb:202:in process_action'
actionpack (3.1.0.rc4) lib/action_controller/metal/rescue.rb:17:inprocess_action'
activerecord (3.1.0.rc4) lib/active_record/railties/controller_runtime.rb:18:in process_action'
actionpack (3.1.0.rc4) lib/abstract_controller/base.rb:121:inprocess'
actionpack (3.1.0.rc4) lib/abstract_controller/rendering.rb:45:in process'
actionpack (3.1.0.rc4) lib/action_controller/metal.rb:193:indispatch'
actionpack (3.1.0.rc4) lib/action_controller/metal/rack_delegation.rb:14:in dispatch'
actionpack (3.1.0.rc4) lib/action_controller/metal.rb:236:inblock in action'
actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:65:in call'
actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:65:indispatch'
actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:29:in call'
rack-mount (0.8.1) lib/rack/mount/route_set.rb:152:inblock in call'
rack-mount (0.8.1) lib/rack/mount/code_generation.rb:93:in block in recognize'
rack-mount (0.8.1) lib/rack/mount/code_generation.rb:75:inoptimized_each'
rack-mount (0.8.1) lib/rack/mount/code_generation.rb:92:in recognize'
rack-mount (0.8.1) lib/rack/mount/route_set.rb:141:incall'
actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:531:in call'
warden (1.0.4) lib/warden/manager.rb:35:inblock in call'
warden (1.0.4) lib/warden/manager.rb:34:in catch'
warden (1.0.4) lib/warden/manager.rb:34:incall'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/best_standards_support.rb:17:in call'
rack (1.3.0) lib/rack/etag.rb:23:incall'
rack (1.3.0) lib/rack/conditionalget.rb:35:in call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/head.rb:14:incall'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/params_parser.rb:21:in call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/flash.rb:243:incall'
rack (1.3.0) lib/rack/session/abstract/id.rb:195:in context'
rack (1.3.0) lib/rack/session/abstract/id.rb:190:incall'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/cookies.rb:321:in call'
activerecord (3.1.0.rc4) lib/active_record/query_cache.rb:54:incall'
activerecord (3.1.0.rc4) lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call'
activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:392:in _run_call_callbacks'
activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:inrun_callbacks'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/callbacks.rb:28:in call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/reloader.rb:68:incall'
rack (1.3.0) lib/rack/sendfile.rb:102:in call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/remote_ip.rb:48:incall'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/show_exceptions.rb:47:in call'
railties (3.1.0.rc4) lib/rails/rack/logger.rb:13:incall'
rack (1.3.0) lib/rack/methodoverride.rb:24:in call'
rack (1.3.0) lib/rack/runtime.rb:17:incall'
activesupport (3.1.0.rc4) lib/active_support/cache/strategy/local_cache.rb:72:in call'
rack (1.3.0) lib/rack/lock.rb:34:incall'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/static.rb:53:in call'
railties (3.1.0.rc4) lib/rails/engine.rb:438:incall'
railties (3.1.0.rc4) lib/rails/rack/content_length.rb:16:in call'
railties (3.1.0.rc4) lib/rails/rack/log_tailer.rb:14:incall'
rack (1.3.0) lib/rack/handler/webrick.rb:59:in service'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:inservice'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in run'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:inblock in start_thread'
Request
Parameters:
{"authenticity_token"=>"vwNaDytLihv5ev3M2tP5uZBjwR1/t6ld0iFF1gGwWsw=",
"product_id"=>"4e126c01421aa93347000002"}
Show session dump
_csrf_token: "vwNaDytLihv5ev3M2tP5uZBjwR1/t6ld0iFF1gGwWsw="
cart_id: BSON::ObjectId('4e156c78421aa918eb000005')
session_id: "532c1ca0f48a6fca905a97afba912e05"
i did not get the problem point. why this happening
undefined method `cart_items' ???
thanks.
I would need to see the code that is throwing an error to give you a definitive answer as to why the error is being thrown (i.e. the create method), but why don't you try this for your Cart creation code:
def current_cart
cart = Cart.find_or_create_by(:id => session[:cart_id])
session[:cart_id] ||= cart.id
return cart
end
It looks as though your current implementation is not actually returning the cart item upon locating / creating it, but instead is returning the session[:cart_id] variable.
An alternative and somewhat prettier solution might be to split the helper up into two methods:
def current_cart_id
session[:cart_id] ||= Cart.find_or_create_by(:id => session[:cart_id]).id
end
def current_cart
#current_cart ||= Cart.find(current_cart_id)
end
Then you can easily access the current_cart object and it's id at any time.
Related
Not all users in LDAP are authorized to own an user account in my MediaWiki. I already have users logging in because I created their accounts before installing LDAP Plugin. Now, I need to create accounts for new employees and I always receive the message "Username entered already in use. Please choose a different name.", through Special:CreateAccount.
Obviously, If I disable all LDAP configuration in LocalSettings, I'm able to create a local user account with the same LDAP username. Then , if I enable LDAP configuration again, the user is recognized with LDAP password and he can log in. The fact is that I don't want to edit LocalSettings every time I have a new employee.
MediaWiki: 1.29.1
PHP: 5.5.21 (apache2handler)
PostgreSQL: 9LDAP
My configuration is below. Thanks in advance.
require_once ('.../extensions/LdapAuthentication/LdapAuthentication.php');
$wgAuth = new LdapAuthenticationPlugin();
$wgLDAPDomainNames = array( 'AD' );
$wgLDAPServerNames = array( 'AD' => 'url' );
$wgLDAPUseLocal = false;
$wgLDAPEncryptionType = array( 'AD' => 'clear' );
$wgLDAPPort = array( 'AD' => 389 );
$wgLDAPProxyAgent = array( 'AD' => 'CN=a,OU=b,DC=c,DC=d' );
$wgLDAPProxyAgentPassword = array( 'UFPE-AD' => 'password' );
$wgLDAPSearchAttributes = array( 'AD' => 'description' );
$wgLDAPBaseDNs = array( 'AD' => 'DC=c,DC=d' );
$wgLDAPDisableAutoCreate = array( 'AD' => true );
$wgLDAPPreferences = array( 'AD' => array( 'email' => 'mail', 'realname' => 'cn','nickname' => 'givenname') );
$wgLDAPLowerCaseUsername = array( 'AD' => true);
$wgGroupPermissions['*']['createaccount'] = false;
new to laravel framework, I am running multiple database connections for a project I am working on. Just a simple test against one of the mysql instance,
<p>{{ $data2 = DB::connection('mysql2')->table('test')->get() }}</p>
gives a simple dump but when run simmilar test against the ms sql server 2012, it errors out
<p>{{ $data3 = DB::connection('sqlsrv')->table('test1')->get() }}</p>
The error is:
ErrorException in helpers.php line 519:
Method Illuminate\Support\Collection::__toString() must return a string value
my db config is similar to other examples I have seen posted online.
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'SQL3.company.ca'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'test'),
'password' => env('DB_PASSWORD', '#######'),
'collation' => 'SQL_Latin1_General_CP1_CI_AS',
'charset' => 'latin1',
'prefix' => '',
],
Did I set up something wrong? any direction I should be looking at?
Hello Everyone,
I am trying to send email through cakephp framework,but unfortunately email are not getting delivered. But when i use social email service provider such as google, outlook and rediffmail e-mail gets delivered.
My SMTP port is 465. I am using below cakephp mail()function:
public $gmail = array(
'host' => 'ssl://smtp.techphant.com',
'port' => 465,
'username' => 'xyz#abc.com',
'password' => 'xxxxxx',
'transport' => 'smtp',
);
Also i have tried port number 2525 and 25 but to no avail.
Please let me know your suggestions.
Thanks in advance.
Try to send eamil with cake Email component not with mail() function.
$this->set(
'content',
'<h1>Hi </h1>
<p>Thanks for contact us. We will respond you soon</p>
'
);
$this->Email->smtpOptions = array(
'host' => 'ssl://smtp.techphant.com',
'port' => 465,
'username' => 'xyz#abc.com',
'password' => 'xxxxxx',
'timeout' => 30,
'client' => null
);
$this->Email->to = 'abc#gmail.com';
$this->Email->subject = 'Thanks for contact us';
$this->Email->from = 'no_reply#abc.com';
$this->Email->template = 'default';
$this->Email->sendAs = 'html';
$this->Email->send();
Thanks..!
I've been searching for hours trying to find a up-to-date example of a mutual has_many :through between 2 models that actually worked, so I finally decided I'd just ask.
My database:
schema.rb
#...
create_table "groups", force: true do |t|
t.string "group_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "memberships", force: true do |t|
t.integer "student_id"
t.integer "group_id"
end
create_table "students", force: true do |t|
t.string "user_name"
t.string "first_name"
t.string "last_name"
t.string "password_digest"
t.datetime "created_at"
t.datetime "updated_at"
end
#...
My models:
student.rb
class Student < ActiveRecord::Base
has_secure_password
validates :password, length: { in: 6..20 }
validates :user_name, format: {with:/\w{2,7}\.\d{1,4}/}, on: :create
validates_uniqueness_of :user_name, on: :create
validates_presence_of :first_name, on: :create
validates_presence_of :last_name, on: :create
has_many :memberships, :dependent => :destroy
has_many :groups, through: :memberships
has_many :ratings, :dependent => :destroy
end
group.rb
class Group < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :students, through: :memberships
validates_uniqueness_of :group_name
end
membership.rb
class Membership < ActiveRecord::Base
belongs_to :students
belongs_to :groups
end
If my authorization correctly puts the logged-in student's :user_name into session[:user_name] if I wanted to get all of the groups to which that student belonged, what would be the proper line(s)? I'm also interested in finding the students within a given group.
I tried:
#current_student = Student.find_by_user_name(session[:user_name])
#current_groups = #current_student.groups
But I was given:
uninitialized constant Student::Groups
What did I do wrong?
Change membership.rb to :
class Membership < ActiveRecord::Base
belongs_to :student
belongs_to :group
end
I using rails 4.0.3 and am trying to set up many to many checkboxes in Active-Admin. The checkbox selections are not being saved. This is what i have
class Product < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
accepts_nested_attributes_for :categorizations
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :products, :through => :categorizations
accepts_nested_attributes_for :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :product
end
ActiveAdmin.register Product do
permit_params :title, :price, category_ids:[:id]
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Product" do
f.input :title
f.input :price
f.input :categories, :as => :check_boxes
end
f.actions
end
end
I have also tried using has_and_belongs_to_many but still cant not get the selections to save.
Any guidance would be highly appreciated.
Cheers
I found that adding the following to your active_admin file, product.rb, fixes it.
ActiveAdmin.register Product do
permit_params category_ids: []
end
try add
permit_params :title, :price, category_ids:[:id],
categories_attributes: [:id, :your_fields, :_update,:_create]