fields-for

Rails 4: fields_for in fields_for

落花浮王杯 提交于 2019-12-02 21:59:09
I am learning RoR and i am trying to find how to set a fields_for in another one with has_one models like this: class Child < ActiveRecord::Base belongs_to :father accepts_nested_attributes_for :father end class Father < ActiveRecord::Base has_one :child belongs_to :grandfather accepts_nested_attributes_for :grandfather end class Grandfather < ActiveRecord::Base has_one :father end I used Nested Model Form Part 1 on Railscasts to get these: In children_controller.rb: def new @child = Child.new father=@child.build_father father.build_grandfather end def child_params params.require(:child)

using an array with fields_for

痴心易碎 提交于 2019-12-02 20:41:07
How can I iterate through an array of objects (all the same model) using fields_for ? The array contains objects created by the current_user. I currently have: <%= f.fields_for :descriptionsbyuser do |description_form| %> <p class="fields"> <%= description_form.text_area :entry, :rows => 3 %> <%= description_form.link_to_remove "Remove this description" %> <%= description_form.hidden_field :user_id, :value => current_user.id %> </p> <% end %> But I want to replace the :descriptionsbyuser with an array I created in my controller - @descriptionsFromCurrentUser This is also inside Ryan Bate's

fields_for in rails view

点点圈 提交于 2019-12-02 11:36:06
When I attempt to use fields_for in the view code below, the company_name is not showing up in the view. What am I doing wrong? = form_for @company do |f| -if @company.errors.any? #error_explanation %h2= "#{pluralize(@company.errors.count, "error")} prohibited this company from being saved:" %ul - @company.errors.full_messages.each do |msg| %li= msg =f.fields_for :showing do |t| .field = t.label :company_name = t.text_field :company_name .field = f.label :geography = f.text_area :geography Are you not building a showing object in your controller? @company.build_showing If you've defined

Rails 3.2 Create Parent Model from Child View

↘锁芯ラ 提交于 2019-12-02 04:22:23
问题 I'm having a difficult time understanding how to do this. I have two models, a project, and a course. #project.rb belongs_to :course attr_accessible :course_id, :course accepts_nested_attributes_for :course, reject_if: lambda { |a| a[:course_id] == 0 } #course.rb has_many :projects On the Projects#new page (child object), I want to type in the name of a new course and have it create the parent object. Here's my attempt in the view, but it doesn't seem to be working correctly. = form_for [

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

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=>{

How should I use rails and simple_form for nested resources?

纵然是瞬间 提交于 2019-11-27 23:34:04
I'm trying to create one resource with another nested resource at the same time. I'm using Rails4 and simple_form 3.0.0rc. Here is my code. Models: class User < ActiveRecord::Base has_one :profile accepts_nested_attributes_for :profile end class Profile < ActiveRecord::Base belongs_to :user end Controller: class UsersController < ApplicationController def new @user = User.new @user.build_profile end def create user = User.new user_params user.save redirect_to root_url # @par =params end private def user_params params.require(:user).permit(:email, profile_attributes: [:name]) end end View (form

How should I use rails and simple_form for nested resources?

你离开我真会死。 提交于 2019-11-26 21:48:37
问题 I'm trying to create one resource with another nested resource at the same time. I'm using Rails4 and simple_form 3.0.0rc. Here is my code. Models: class User < ActiveRecord::Base has_one :profile accepts_nested_attributes_for :profile end class Profile < ActiveRecord::Base belongs_to :user end Controller: class UsersController < ApplicationController def new @user = User.new @user.build_profile end def create user = User.new user_params user.save redirect_to root_url # @par =params end

Rails: fields_for with index?

淺唱寂寞╮ 提交于 2019-11-26 18:29:52
Is there a method (or way to pull off similar functionality) to do a fields_for_with_index ? Example: <% f.fields_for_with_index :questions do |builder, index| %> <%= render 'some_form', :f => builder, :i => index %> <% end %> That partial being rendered needs to know what the current index is in the fields_for loop. This would actually be a better approach, following Rails documentation more closely: <% @questions.each.with_index do |question,index| %> <% f.fields_for :questions, question do |fq| %> # here you have both the 'question' object and the current 'index' <% end %> <% end %> From: