nested-forms

Rails: validate presence of parent_id in has_many association

柔情痞子 提交于 2019-11-30 13:10:28
问题 I have a projects resource that has many tasks. I want to ensure that every task has a project_id by adding validates_presence_of :project_id to the tasks model. However, when creating a new project with tasks, the project_id won't be available until the record saves, therefore I can't use validates_presence_of :project_id . So my question is, how do I validate presence of project_id in the task model? I want to ensure every task has a parent. ... class Project < ActiveRecord::Base has_many

How to access nested params

前提是你 提交于 2019-11-30 08:56:14
I would like to get some nested params. I have an Order that has many Items and these Items each have a Type. i would like to get the type_id parameter from the controllers create method. @order = Order.new(params[:order]) @order.items.each do |f| f.item_type_id = Item_type.find_by_name(f.item_type_id).id end The reason is that i want the user to be able to create new item_types in the view. When they do that i use an AJAX call add them to the db. When they post the form i get names of the item_type in the item_type_id parameter and i want to find the correct item_type and set the id to that

Rails: validate presence of parent_id in has_many association

两盒软妹~` 提交于 2019-11-30 06:08:41
I have a projects resource that has many tasks. I want to ensure that every task has a project_id by adding validates_presence_of :project_id to the tasks model. However, when creating a new project with tasks, the project_id won't be available until the record saves, therefore I can't use validates_presence_of :project_id . So my question is, how do I validate presence of project_id in the task model? I want to ensure every task has a parent. ... class Project < ActiveRecord::Base has_many :tasks, :dependent => :destroy accepts_nested_attributes_for :tasks, :allow_destroy => true ... class

fields_for not rendering - rails 3

大兔子大兔子 提交于 2019-11-30 05:56:46
Finally moved to Rails 3 for a new project and already running into a rookie problem. Trying to do a simple nested form. 2 models: list and tasks List model class List < ActiveRecord::Base has_many :tasks, :dependent=>:destroy accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:name].blank? } end Task Model class Task < ActiveRecord::Base belongs_to :list end List Controller def new @list = List.new 3.times{ @list.tasks.build } end lists/new.html.erb <% form_for :list, :url=>{:action=>"create"} do |f| %> <%= f.text_field :name, :class=>'big' %> <%= f.label :name, "ex: Today's

Rails: multi level nested Forms (accepts nested attributes)

旧城冷巷雨未停 提交于 2019-11-30 05:43:27
问题 I am creating simple blog level application. below are my models. class User < ActiveRecord::Base attr_accessible :name,:posts_count,:posts_attributes , :comments_attributes has_many :posts has_many :comments accepts_nested_attributes_for :posts , :reject_if => proc{|post| post['name'].blank?} , :allow_destroy => true end class Post < ActiveRecord::Base attr_accessible :name, :user_id ,:comments_attributes belongs_to :user has_many :comments accepts_nested_attributes_for :comments end class

How to convert a MultiDict to nested dictionary

杀马特。学长 韩版系。学妹 提交于 2019-11-30 02:41:53
问题 I would like to convert a POST from Webob MultiDict to nested dictionary. E.g. So from a POST of: 'name=Kyle&phone.number=1234&phone.type=home&phone.number=5678&phone.type=work' to a multidict; [('name', 'Kyle'), ('phone.number', '1234'), ('phone.type', 'home'), ('phone.number', '5678'), ('phone.type', 'work')] to a nested dictionary {'name': 'Kyle', 'phone': [ { 'number': '12345', 'type': 'home', },{ 'number': '5678', 'type': 'work', }, Any ideas? EDIT I ended up extracting the variable

fields_for not rendering - rails 3

我只是一个虾纸丫 提交于 2019-11-29 06:13:53
问题 Finally moved to Rails 3 for a new project and already running into a rookie problem. Trying to do a simple nested form. 2 models: list and tasks List model class List < ActiveRecord::Base has_many :tasks, :dependent=>:destroy accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:name].blank? } end Task Model class Task < ActiveRecord::Base belongs_to :list end List Controller def new @list = List.new 3.times{ @list.tasks.build } end lists/new.html.erb <% form_for :list, :url=>{

Error in slick.js: “Uncaught TypeError: Cannot read property 'add' of null”

守給你的承諾、 提交于 2019-11-28 21:21:22
I used slick js for slider view of image. Here is my code. <div class="slider_wrap add-remove"> <%= f.fields_for :images do |image_form| %> <%#= render 'images_fields', :f => image_form %> <div> <%= image_tag image_form.object.picture.url,:class=>"drop_down_link even img_prev" %> </div> <div class="image_upload_div"> <div class="image-upload"> <label> <i class="fa fa-cloud-upload"> <%= image_form.file_field :picture ,:'data-role'=>"none",:onchange=>"readURL(this);" , :accept => 'image/jpeg , image/png' %> </i> </label> </div> </div> <% end %> <%= f.link_to_add "Add a picture", :images ,:id=>

Rails - Using form_for and fields_for, how do you access the sub-object while in the fields_for block?

做~自己de王妃 提交于 2019-11-28 17:46:20
In my first rails app I'm trying to use form_for and fields_for to create a nested object form. So far so good, but I can't figure out how to access the sub-object while in the fields_for block. I've pre-populated a field in the sub-object with data that I want to show in the user instructions. Models Garage: has_many :cars, :dependent => :destroy accepts_nested_attributes_for :cars Car: belongs_to :garage Garage Controller def new @garage = Garage.new for i in 1..5 @garage.cars.build :stall_number => i end end _form.html.erb <%= form_for @garage do |f| %> <%= f.label :title, "Garage Name" %>

Rails 3, nested multi-level forms and has_many through

北战南征 提交于 2019-11-28 17:07:24
I'm trying to get it to work but it dosen't! I have class User < ActiveRecord::Base has_many :events, :through => :event_users has_many :event_users accepts_nested_attributes_for :event_users end class Event < ActiveRecord::Base has_many :event_users has_many :users, :through => :event_users accepts_nested_attributes_for :users end class EventUser < ActiveRecord::Base set_table_name :events_users belongs_to :event belongs_to :user accepts_nested_attributes_for :events accepts_nested_attributes_for :users end And also the table-layout event_users user_id event_id user_type events id name users