Please how can I save more than one field ? I have Product has many colors by quantity. This works #controller: products action: new file:_form.erb <% for color in Color.all %> <div> <%= check_box_tag "product[color_ids][]", color.id, @product.colors.include?(color) %> <%= color.name %> </div> <% end %> but if I add one more field the value will not work. <%= text_field_tag "stock["+color.id.to_s+"][qtd]" %> Please anyone can help me? My structure: class Product < ActiveRecord::Base has_many :stocks, :dependent => :destroy has_many :colors, :through => :stocks end class Color < ActiveRecord::Base has_many :stocks has_many :product, :through => :stocks end class Stock < ActiveRecord::Base belongs_to :product belongs_to :color end -- 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.
People I figure out this problem The problem was solved with this # models/product.rb accepts_nested_attributes_for :stocks # views/product/_form.erb <% for color in Color.all %> <div> <%= check_box_tag "product[stocks_attributes][][color_id]", color.id, @product.colors.include?(color) %> <%= color.name %> <%= text_field_tag "product[stocks_attributes][][qtd]" %> </div> <% end %> On 11 out, 23:35, Farah <elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Please how can I save more than one field ? > > I have Product has many colors by quantity. > > This works > #controller: products action: new file:_form.erb > <% for color in Color.all %> > <div> > <%= check_box_tag "product[color_ids][]", color.id, > @product.colors.include?(color) %> > <%= color.name %> > </div> > <% end %> > > but if I add one more field the value will not work. > > <%= text_field_tag "stock["+color.id.to_s+"][qtd]" %> > > Please anyone can help me? > > My structure: > > class Product < ActiveRecord::Base > has_many :stocks, :dependent => :destroy > has_many :colors, :through => :stocks > end > > class Color < ActiveRecord::Base > has_many :stocks > has_many :product, :through => :stocks > end > > class Stock < ActiveRecord::Base > belongs_to :product > belongs_to :color > end-- 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.
Now I have another problem, How can I get the values from database to show in the fields for edit action ? Any one can help me ? On 12 out, 08:55, Farah <elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> People I figure out this problem > > The problem was solved with this > > # models/product.rb > accepts_nested_attributes_for :stocks > > # views/product/_form.erb > > <% for color in Color.all %> > <div> > <%= check_box_tag "product[stocks_attributes][][color_id]", > color.id, @product.colors.include?(color) %> > <%= color.name %> > <%= text_field_tag "product[stocks_attributes][][qtd]" %> > </div> > <% end %> > > On 11 out, 23:35, Farah <elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Please how can I save more than one field ? > > > I have Product has many colors by quantity. > > > This works > > #controller: products action: new file:_form.erb > > <% for color in Color.all %> > > <div> > > <%= check_box_tag "product[color_ids][]", color.id, > > @product.colors.include?(color) %> > > <%= color.name %> > > </div> > > <% end %> > > > but if I add one more field the value will not work. > > > <%= text_field_tag "stock["+color.id.to_s+"][qtd]" %> > > > Please anyone can help me? > > > My structure: > > > class Product < ActiveRecord::Base > > has_many :stocks, :dependent => :destroy > > has_many :colors, :through => :stocks > > end > > > class Color < ActiveRecord::Base > > has_many :stocks > > has_many :product, :through => :stocks > > end > > > class Stock < ActiveRecord::Base > > belongs_to :product > > belongs_to :color > > end-- 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.
On 12 October 2011 13:24, Farah <eliasfa-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Now I have another problem, How can I get the values from database to > show in the fields for edit action ?Asking a question so vague - how can I get values from database - is very difficult to answer. Please give more details of what you are trying to do. Since this seems fairly basic stuff have you worked through the Rails Guides and some tutorials? railstutorial.org is good and free to use online. Colin -- 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.
Sorry about that,
I have 3 tables/models products, colors, stock, the problem is I need
to save product with N colors and each color I need to tell what the
quantity each color.
The form is gonna be like this:
http://postimage.org/image/2cjyus59g/
How can I save this information together ?
More especifications
Product model
class Product < ActiveRecord::Base
has_many :stocks, :dependent => :destroy
has_many :colors, :through => :stocks
belongs_to :manufacturer
accepts_nested_attributes_for :stocks, :reject_if => lambda { |a|
a[:qtd].present? }
end
Stock Model
class Stock < ActiveRecord::Base
belongs_to :product
belongs_to :color
end
Color Model
class Color < ActiveRecord::Base
has_many :stocks
has_many :product, :through => :stocks
end
From view _form.erb
Just the associations:
<%= fields_for :stocks do |stock_form| %>
<% for color in Color.all %>
<%= stock_form.check_box :color_id %> <%= color.name %>
<%= stock_form.text_field :qtd %>
<% end %>
<% end %>
that''s it, anyone can help me ?
On Oct 12, 9:37 am, Colin Law
<clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
wrote:> On 12 October 2011 13:24, Farah
<elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > Now I have another problem, How can I get the values from database to
> > show in the fields for edit action ?
>
> Asking a question so vague - how can I get values from database - is
> very difficult to answer. Please give more details of what you are
> trying to do.
> Since this seems fairly basic stuff have you worked through the Rails
> Guides and some tutorials? railstutorial.org is good and free to use
> online.
>
> Colin
--
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.
Existe alguma forma que você conheça que posso preencher o valor do text_field como preencho o checkbox, algo como: <%= text_field_tag "product[stocks_attributes][] [qtd]", :value=>@product.stocks.include?(color) %> mas o include? retorna boolean, eu queria que ele retornasse o valor da posição encontrada da cor. Se existisse esse método já resolveria meu problema. On 12 out, 15:11, Farah <elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry about that, > > I have 3 tables/models products, colors, stock, the problem is I need > to save product with N colors and each color I need to tell what the > quantity each color. > > The form is gonna be like this: > > http://postimage.org/image/2cjyus59g/ > > How can I save this information together ? > > More especifications > > Product model > > class Product < ActiveRecord::Base > has_many :stocks, :dependent => :destroy > has_many :colors, :through => :stocks > belongs_to :manufacturer > > accepts_nested_attributes_for :stocks, :reject_if => lambda { |a| > a[:qtd].present? } > > end > > Stock Model > > class Stock < ActiveRecord::Base > belongs_to :product > belongs_to :color > end > > Color Model > > class Color < ActiveRecord::Base > has_many :stocks > has_many :product, :through => :stocks > end > > From view _form.erb > > Just the associations: > > <%= fields_for :stocks do |stock_form| %> > <% for color in Color.all %> > <%= stock_form.check_box :color_id %> <%= color.name %> > <%= stock_form.text_field :qtd %> > <% end %> > <% end %> > > that''s it, anyone can help me ? > > On Oct 12, 9:37 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > > > > > On 12 October 2011 13:24, Farah <elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Now I have another problem, How can I get the values from database to > > > show in the fields for edit action ? > > > Asking a question so vague - how can I get values from database - is > > very difficult to answer. Please give more details of what you are > > trying to do. > > Since this seems fairly basic stuff have you worked through the Rails > > Guides and some tutorials? railstutorial.org is good and free to use > > online. > > > Colin-- 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.
Anyone can Help me ? On Oct 13, 9:35 am, Farah <elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Existe alguma forma que você conheça que posso preencher o valor do > text_field como preencho o checkbox, algo como: > > <%= text_field_tag "product[stocks_attributes][] > [qtd]", :value=>@product.stocks.include?(color) %> > > mas o include? retorna boolean, eu queria que ele retornasse o valor > da posição encontrada da cor. > > Se existisse esse método já resolveria meu problema. > > On 12 out, 15:11,Farah<elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Sorry about that, > > > I have 3 tables/models products, colors, stock, the problem is I need > > to save product with N colors and each color I need to tell what the > > quantity each color. > > > The form is gonna be like this: > > >http://postimage.org/image/2cjyus59g/ > > > How can I save this information together ? > > > More especifications > > > Product model > > > class Product < ActiveRecord::Base > > has_many :stocks, :dependent => :destroy > > has_many :colors, :through => :stocks > > belongs_to :manufacturer > > > accepts_nested_attributes_for :stocks, :reject_if => lambda { |a| > > a[:qtd].present? } > > > end > > > Stock Model > > > class Stock < ActiveRecord::Base > > belongs_to :product > > belongs_to :color > > end > > > Color Model > > > class Color < ActiveRecord::Base > > has_many :stocks > > has_many :product, :through => :stocks > > end > > > From view _form.erb > > > Just the associations: > > > <%= fields_for :stocks do |stock_form| %> > > <% for color in Color.all %> > > <%= stock_form.check_box :color_id %> <%= color.name %> > > <%= stock_form.text_field :qtd %> > > <% end %> > > <% end %> > > > that''s it, anyone can help me ? > > > On Oct 12, 9:37 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > On 12 October 2011 13:24,Farah<elia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Now I have another problem, How can I get the values from database to > > > > show in the fields for edit action ? > > > > Asking a question so vague - how can I get values from database - is > > > very difficult to answer. Please give more details of what you are > > > trying to do. > > > Since this seems fairly basic stuff have you worked through the Rails > > > Guides and some tutorials? railstutorial.org is good and free to use > > > online. > > > > Colin-- 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.