I have got a problem with :on => create validation
validates_uniqueness_of :nip, :regon, on => :create
Doesn''t work properly when I call it via xhr
In my log
Processing AdminController#doajaxing_for_updateuser (for 127.0.0.1 at
2010-03-22 17:37:13) [PUT]
Parameters:
{"account"=>{"company_attributes"=>{"nip"=>"634-119-69-92"}},
"named"=>"company|nip",
"locale"=>"pl"}
Processing AdminController#doajaxing_for_createuser (for 127.0.0.1 at
2010-03-22 17:31:33) [POST]
Parameters:
{"account"=>{"company_attributes"=>{"nip"=>"634-119-69-95"}},
"named"=>"company|nip",
"locale"=>"pl"}
It looks fine, Put and Post is given by my jquery request...
But still validation is always invoking validates_uniqueness_of ....
why /.??
I check validation by:
@model.valid?
and later bringing messages using something like this
@model.errors[current_given]
regards
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 22, 4:59 pm, Piotr <inpr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Mąsior <inpr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have got a problem with :on => create validation > > validates_uniqueness_of :nip, :regon, on => :createThe :on there doesn''t refer to the controller action - it refers to whether the save is an update to an existing record or not. Fred> > Doesn''t work properly when I call it via xhr > > In my log > > Processing AdminController#doajaxing_for_updateuser (for 127.0.0.1 at > 2010-03-22 17:37:13) [PUT] > Parameters: > {"account"=>{"company_attributes"=>{"nip"=>"634-119-69-92"}}, > "named"=>"company|nip", "locale"=>"pl"} > > Processing AdminController#doajaxing_for_createuser (for 127.0.0.1 at > 2010-03-22 17:31:33) [POST] > Parameters: > {"account"=>{"company_attributes"=>{"nip"=>"634-119-69-95"}}, > "named"=>"company|nip", "locale"=>"pl"} > > It looks fine, Put and Post is given by my jquery request... > > But still validation is always invoking validates_uniqueness_of .... > why /.?? > > I check validation by: > > @model.valid? > > and later bringing messages using something like this > > @model.errors[current_given] > > regards-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
So painful, I was wondering is there any dry_save or something like this ? On 22 Mar, 18:13, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 22, 4:59 pm, Piotr <inpr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Mąsior > > <inpr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have got a problem with :on => create validation > > > validates_uniqueness_of :nip, :regon, on => :create > > The :on there doesn''t refer to the controller action - it refers to > whether the save is an update to an existing record or not. > > Fred > > > > > > > Doesn''t work properly when I call it via xhr > > > In my log > > > Processing AdminController#doajaxing_for_updateuser (for 127.0.0.1 at > > 2010-03-22 17:37:13) [PUT] > > Parameters: > > {"account"=>{"company_attributes"=>{"nip"=>"634-119-69-92"}}, > > "named"=>"company|nip", "locale"=>"pl"} > > > Processing AdminController#doajaxing_for_createuser (for 127.0.0.1 at > > 2010-03-22 17:31:33) [POST] > > Parameters: > > {"account"=>{"company_attributes"=>{"nip"=>"634-119-69-95"}}, > > "named"=>"company|nip", "locale"=>"pl"} > > > It looks fine, Put and Post is given by my jquery request... > > > But still validation is always invoking validates_uniqueness_of .... > > why /.?? > > > I check validation by: > > > @model.valid? > > > and later bringing messages using something like this > > > @model.errors[current_given] > > > regards-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Piotr Mąsior wrote:> So painful, I was wondering is there any dry_save or something like > this ?How do you populate your @model? Stephan -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Model is passed by @model variable with nested attributes so it is
like:
@model = model.new
@model.build_another
at form it is looped through like
- form_for @account, :url => { :action => @given_action , :controller
=> ''admin''} do |f|
- f.fields_for :company do |o|
%label{:for =>
"company|name"}Company.human_attribute_name(:name)
= o.text_field :name, :named => "company|name"
- o.fields_for :city do |x|
%label{:for =>
"company|city|name"}City.human_attribute_name(:name)
= x.text_field :name, :named => "company|city|name"
jquery is responsible for communication and requesting my particular
methods so it calls appropriate model basing on something like
params[:model], doajaxing method knows how to deal with nested
attributes
so it request from controller some action like
doajaxing_for_createmodel and it looks like:
def doajaxing_for_createmodel
doajaxing("account")
end
and doajaxing itself looks like
def doajaxing(object_name)
if request.xhr?
sended = params[object_name.to_sym]
object_name = object_name.camelize
@process = object_name.constantize
@process = @process.new(sended)
@process.valid?
##some other processing
if @process.errors[current_given]
#process message and field
#some @response is given in json
render :json => @response
end
end
that it work... I wonder if I can use instead of @process.valid?
something else
On 22 Mar, 19:57, Stephan Wehner
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> Piotr Mąsior wrote:
> > So painful, I was wondering is there any dry_save or something like
> > this ?
>
> How do you populate your @model?
>
> Stephan
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Piotr Mąsior wrote:> Model is passed by @model variable with nested attributes so it is > like: > > @model = model.new> @model.build_another > > at form it is looped through like > > - form_for @account, :url => { :action => @given_action , :controller > => ''admin''} do |f| > - f.fields_for :company do |o| > %label{:for => "company|name"}> Company.human_attribute_name(:name) > = o.text_field :name, :named => "company|name" > - o.fields_for :city do |x| > %label{:for => "company|city|name"}> City.human_attribute_name(:name) > = x.text_field :name, :named => "company|city|name" > > > jquery is responsible for communication and requesting my particular > methods so it calls appropriate model basing on something like > params[:model], doajaxing method knows how to deal with nested > attributes > > so it request from controller some action like > doajaxing_for_createmodel and it looks like: > > def doajaxing_for_createmodel > doajaxing("account") > end > > and doajaxing itself looks like > > def doajaxing(object_name) > if request.xhr? > sended = params[object_name.to_sym] > object_name = object_name.camelize > @process = object_name.constantize > @process = @process.new(sended)Because you have "@process.new", the "on_create" validations will be triggered. If you are updating an existing record, use @process = @process.find(...model-id...) where model-id is usually taken from "params" (params[:id]). Then assign the new values to @process. Then the "on_create" validations will not be triggered.> @process.valid? > ##some other processing > if @process.errors[current_given] > #process message and field > #some @response is given in json > render :json => @response > end > end > > > > that it work... I wonder if I can use instead of @process.valid? > something elseHope that helps. I think your "doajaxing" method needs to know about the two cases, create/update. Stephan -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I just realized it too... so bad for me :) Ok, I thought there would be another more convenient way but as I can now see there is not Regards, thanks everyone On 22 Mar, 21:21, Stephan Wehner <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Piotr Mąsior wrote: > > Model is passed by @model variable with nested attributes so it is > > like: > > > @model = model.new > > @model.build_another > > > at form it is looped through like > > > - form_for @account, :url => { :action => @given_action , :controller > > => ''admin''} do |f| > > - f.fields_for :company do |o| > > %label{:for => "company|name"}> > Company.human_attribute_name(:name) > > = o.text_field :name, :named => "company|name" > > - o.fields_for :city do |x| > > %label{:for => "company|city|name"}> > City.human_attribute_name(:name) > > = x.text_field :name, :named => "company|city|name" > > > jquery is responsible for communication and requesting my particular > > methods so it calls appropriate model basing on something like > > params[:model], doajaxing method knows how to deal with nested > > attributes > > > so it request from controller some action like > > doajaxing_for_createmodel and it looks like: > > > def doajaxing_for_createmodel > > doajaxing("account") > > end > > > and doajaxing itself looks like > > > def doajaxing(object_name) > > if request.xhr? > > sended = params[object_name.to_sym] > > object_name = object_name.camelize > > @process = object_name.constantize > > @process = @process.new(sended) > > Because you have "@process.new", the "on_create" validations will be > triggered. > > If you are updating an existing record, use > @process = @process.find(...model-id...) > where model-id is usually taken from "params" (params[:id]). > Then assign the new values to @process. > > Then the "on_create" validations will not be triggered. > > > @process.valid? > > ##some other processing > > if @process.errors[current_given] > > #process message and field > > #some @response is given in json > > render :json => @response > > end > > end > > > that it work... I wonder if I can use instead of @process.valid? > > something else > > Hope that helps. I think your "doajaxing" method needs to know about the > two cases, create/update. > > Stephan > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.