I'd like to update a database record using where method.
This how I proceed:
def AjoutAuPanier
#book = Book.find(params[:id])
if #book.nbr_exemplaires > 0
#p = Panier.where(user_id: current_user, book_id: #book.id)
if #p.empty? == false
#p.update(#p.id, quantity: #p.quantity + 1)
else
#p = Panier.new(user_id: current_user.id , book_id: #book.id , price: #book.price, quantity: 1)
end
if #p.save
#book.update(nbr_exemplaires: #book.nbr_exemplaires-1)
redirect_to detail_path
else
redirect_to books_list_path
end
else
flash[:notice]='Ce livre n\'est plus disponible'
redirect_to books_list_path
end
end
This, however, gives the following error:
undefined method `id' for #
#p=Panier.where(user_id: current_user, book_id: #book.id)
You must add .first because .where always return an array
#p=Panier.where(user_id: current_user, book_id: #book.id).first
Related
#app/serializers/admin_serializer.rb
class AdminSerializer < ActiveModel::Serializer
attributes :id, :email, :access_locked?
end
#app/controllers/dashboard/admins_controller.rb
def index
#search = Admin.search(params[:q])
if params[:page]
#admins = #search.result(:distinct => true).page(params[:page][:number]).per(params[:page][:size])
else
#admins = #search.result(:distinct => true).page(1).per(10)
end
respond_to do |format|
format.html
format.json {render json: #admins}
end
end
On accessing http://dashboard.localhost.com:3000/admins.json I am getting correct links, with page[size] param as 10. The subsequent page has only one admin object to show
"links": {
"self": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"next": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=2&page%5Bsize%5D=10",
"last": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=5&page%5Bsize%5D=10"
}
But if I access the last link here i.e. http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=5&page%5Bsize%5D=10
The subsequent JSON cotains the following incorrect links, with page[size] param as 1. Since this page has only one admin object to show, all the other links thus generated contains page[size]=1
"links": {
"self": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=5&page%5Bsize%5D=10",
"first": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"prev": "http://dashboard.localhost.com:3000/admins.json?page%5Bnumber%5D=4&page%5Bsize%5D=10"
}
Fixed this by replacing the
#admins = #search.result(:distinct => true).page(params[:page][:number]).per(params[:page][:size]) by #admins = #search.result(:distinct => true).page(params[:page][:number]).per(10)
I am new to Ruby-on-Rails 4. I have created a custom validator but cannot assign value to an array. It shows error
undefined method <<' for nil:NilClass.
It highlights #msg << 1
For instance, my model is like
class User < ActiveRecord::Base
has_secure_password
validates :email,:email_format => true, :on => :create
validates :password, password_format:{with: "upercase"}
end
My custom validator
class PasswordFormatValidator < ActiveModel::EachValidator
#def initilize(options)-Fixed
def initialize(options)
#msg=[]
#password1 = options[:attributes=>[:password]]
#val=options.inspect
super
end
def validate_each(record, attribute, value)
record.errors[attribute] << #val
unless (value.nil? || value.empty?)
#msg << 1
#record.errors[attribute] << "testing"
end
end
end
#val output
{:attributes=>[:password], :complexity=>3, :length=>6, :class=>User(id: integer, email: string, password_digest: string, created_at: datetime, updated_at: datetime)}
You have a typo in your constructor name, it should be initialize and not initilize. This is why your #msg variable is undefined - your constructor has never been called!
Have a nice day.
I want to use UIPickerView to select a number and assign the selected number to a label. I worked out how to do it using the ib gem and using interface builder to create the initial interface and it works fine. However, I would like to do it purely using RubyMotion code and I can't for the life of me get it to work. The best I have managed is for the label to return True and not a number.
I'm using the following standard code for the picker view delegate methods:
def pickerView(pickerView, numberOfRowsInComponent:component)
101
end
def pickerView(pickerView, titleForRow:row, forComponent:component)
row.to_s
end
def numberOfComponentsInPickerView (pickerView)
1
end
def pickerView(pickerView, didSelectRow:row, inComponent:component)
end
def pickerView(pickerView, titleForRow:row, forComponent:component)
" #{row+1}"
end
def submit
totals.addTotals(myPicker.selectedRowInComponent(0))
end
and then the label text is populated like this:
numLabel = UILabel.new
numLabel.text = "Number Selected: #{submit}"
numLabel.font = UIFont.boldSystemFontOfSize(18)
numLabel.frame = [[20,320],[260,340]]
numLabel.numberOfLines = 2
numLabel.adjustsFontSizeToFitWidth = 'YES'
self.view.addSubview numLabel
The totals is a shared client.
Here is how to do it in RubyMotion alone. Note that the label and picker are set up in viewDidLoad. The label gets updated in pickerView:didSelectRow:inComponent:
app_delegate.rb
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
#window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
#window.rootViewController = PickerDemo.new
#window.makeKeyAndVisible
true
end
end
picker_demo.rb
class PickerDemo < UIViewController
def viewDidLoad
view.backgroundColor = UIColor.whiteColor
#numLabel = UILabel.new
#numLabel.text = "Number Selected: 0"
#numLabel.font = UIFont.boldSystemFontOfSize(18)
#numLabel.frame = [[20,100],[260,120]]
#numLabel.numberOfLines = 2
#numLabel.adjustsFontSizeToFitWidth = true
view.addSubview(#numLabel)
#picker = UIPickerView.new
#picker.frame = [[0, 183], [320, 162]]
#picker.delegate = self
#picker.dataSource = self
view.addSubview(#picker)
end
def pickerView(pickerView, numberOfRowsInComponent:component)
101
end
def pickerView(pickerView, titleForRow:row, forComponent:component)
row.to_s
end
def numberOfComponentsInPickerView(pickerView)
1
end
def pickerView(pickerView, didSelectRow:row, inComponent:component)
#numLabel.text = "Number Selected: #{row}"
end
end
I have the following models set up
# task.rb
class Task << AR
# everything all task objects have in common
end
# login_request.rb
class Tasks::LoginRequest < Task
store :data, accessors: [:email, :first_name, :last_name, :expires_at]
composed_of :valid_until, class_name: 'DateTime', mapping: %w(expires_at to_s), constructor: Proc.new { |date| (date && date.to_datetime) || DateTime.now }, converter: Proc.new { |value| value.to_s.to_datetime }
end
I'm using the datetime_select helper in my form:
# _form.html.haml
= f.datetime_select :valid_until
This works quite well, but when I call update in my controller with the submitted form data I get the following error message:
1 error(s) on assignment of multiparameter attributes [error on assignment [2014, 4, 2, 9, 48] to valid_until (can't write unknown attribute 'expires_at')]
So, I'm guessing the updated method tries to manipulate the attributes hash directly, but obviously it can't find the attribute expires_at, since it's a simple accessor method of the JSON column data.
I know I could simply add this field to the DB and it would probably work - although there's no need then to have a composed_of statement. But I'd rather not go this route, because not every task has a expires_at column.
How can I overcome this error? Or did I miss something?
Currently compose_of is not supporting this scenario since it writes directly to attributes that are assumed to be in the database. I wrote a tweaked compose_of version that does (based of Rails 4.0.2 version)
Putting this in initialize folder:
#/initialize/support_store_in_composed_of.rb
module ActiveRecord
module Aggregations
extend ActiveSupport::Concern
def clear_aggregation_cache #:nodoc:
#aggregation_cache.clear if persisted?
end
module ClassMethods
def composed_of_with_store_support(part_id, options = {})
options.assert_valid_keys(:class_name, :mapping, :allow_nil, :constructor, :converter, :store)
name = part_id.id2name
class_name = options[:class_name] || name.camelize
mapping = options[:mapping] || [ name, name ]
mapping = [ mapping ] unless mapping.first.is_a?(Array)
allow_nil = options[:allow_nil] || false
constructor = options[:constructor] || :new
converter = options[:converter]
reader_method(name, class_name, mapping, allow_nil, constructor, options[:store])
writer_method(name, class_name, mapping, allow_nil, converter, options[:store])
create_reflection(:composed_of, part_id, nil, options, self)
end
private
def reader_method(name, class_name, mapping, allow_nil, constructor, store=nil)
define_method(name) do
if #aggregation_cache[name].nil? && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
if store.present?
attrs = mapping.collect {|pair| send(pair.first)}
else
attrs = mapping.collect {|pair| read_attribute(pair.first)}
end
object = constructor.respond_to?(:call) ?
constructor.call(*attrs) :
class_name.constantize.send(constructor, *attrs)
#aggregation_cache[name] = object
end
#aggregation_cache[name]
end
end
def writer_method(name, class_name, mapping, allow_nil, converter, store=nil)
define_method("#{name}=") do |part|
klass = class_name.constantize
unless part.is_a?(klass) || converter.nil? || part.nil?
part = converter.respond_to?(:call) ? converter.call(part) : klass.send(converter, part)
end
if part.nil? && allow_nil
mapping.each { |pair| self[pair.first] = nil }
#aggregation_cache[name] = nil
else
if store.present?
mapping.each { |pair| send("#{pair.first}=", part.send(pair.last)) }
else
mapping.each { |pair| self[pair.first] = part.send(pair.last) }
end
#aggregation_cache[name] = part.freeze
end
end
end
end
end
end
And using it like this would solve your problem.
class Task < ActiveRecord::Base
store :data, accessors: [:email, :first_name, :last_name, :expires_at]
composed_of_with_store_support :valid_until, class_name: 'DateTime', mapping: %w(expires_at to_s),
constructor: Proc.new { |date| (date && date.to_datetime) || DateTime.now },
converter: Proc.new { |value| value.to_s.to_datetime },
store: true
end
I'm stuck on this:
I need to populate data into my app.
I'm using Promotion for the very first time....
Without ProMotion I use to fetch the data in the init method
Now my code looks like below:
class Parties < ProMotion::TableScreen
attr_accessor :_cells
#news = []
include MyUiModules
title 'Yazarlar'
refreshable callback: :on_refresh,
pull_message: "Pull to refresh",
refreshing: "Refreshing data…",
updated_format: "Last updated at %s",
updated_time_format: "%l:%M %p"
def on_refresh
#MyItems.pull_from_server do |items|
##my_items = items
end_refreshing
#update_table_data
# end
end
def table_data
_cells = []
[{
title: nil,
cells: create_cells(_cells)
}]
end
def will_appear
Barbutton.create_bar(self)
set_attributes self.view, {
backgroundColor: hex_color("DBDBDB")
}
end
def go_to_next
App.delegate.slide_menu.show_menu
end
def create_cells(_cells)
BW::HTTP.get(URL) do |response|
json = BW::JSON.parse response.body.to_str
for line in json
_cells << { title: line["val_for_title"]}
end
end
_cells
end
end
Unfotunately this does return an empty array, and I can't figure out how to solve it.
Thx for your help
You can't do that because BW::HTTP.get is asynchronous !
Instead try something like this:
def on_init
#data = []
end
def table_data
[
{
title: nil,
cells: #data
}
]
end
def on_refresh
BW::HTTP.get(URL) do |response|
#data = []
json = BW::JSON.parse(response.body.to_str)
json.each do |hash|
#data << { title: hash["val_for_title"]}
end
update_table_data
end_refreshing
end
end
Hope it helps :-)