First of all I'm no native speaker and just begun with rails three days ago. Sorry for my mistakes.
Formtastic is driving me crazy. I have three tables: user, note, receiver:
class User < ActiveRecord::Base
has_many :receivers
has_many :notes, :through => :receivers
attr_accessible :id, :email, :password, :password_confirmation, :remember_me
class Note < ActiveRecord::Base
has_many :receivers
has_many :users, :through => :receivers
attr_accessible :id, :text, :user_id
accepts_nested_attributes_for :receivers
class Receiver < ActiveRecord::Base
belongs_to :user
belongs_to :note
attr_accessible :user_id, :note_id, :note_attributes
accepts_nested_attributes_for :user
accepts_nested_attributes_for :note
And here my formtastic form:
<%= semantic_form_for #note do |form| %>
<%= form.inputs do %>
<%= form.input :text %>
<%= form.input :user_id, :as => :check_boxes, :collection => User.find(:all, :conditions => ["id != ?", current_user.id], :order => 'id').collect{|u| [u.email, u.id]} %>
<% end %>
<%= form.buttons %>
<% end %>
Now I want to create a new note which can have several receivers. Unfortunately only the note is created, but no entrys in the receiver table, even if I select receivers. Can someone help me please?
Here my notes_controller:
#note = Note.new(params[:note])
Print out the params[:note] using logger.info and check what all parameters are passed from form and Can you also try adding code reciever_ids code as attr_accessible in Note model
In the view model, you are using attr_accessible, it wont save any fields that are not in the attr_accessible like the receives_attributes, that comes from the form when your nested form is displayed. So you have to add receiver_attributes to the attr_accessible list.You might want to do this to the User and Receiver(if you are having nested forms for them too), which also have attr_accessible
attr_accessible :id, :text, :user_id, :receiver_attributes
In the new action of notes_controller, you need to use build method like
#note.build_receiver
then in the form, you need to write the code to display the fields in the receiver.
<%= semantic_form_for #note do |form| %>
<%= form.inputs do %>
<%= form.input :text %>
<%= form.input :user_id, :as => :check_boxes, :collection => User.find(:all, :conditions => ["id != ?", current_user.id], :order => 'id').collect{|u| [u.email, u.id]} %>
<% end %>
<%=f.semantic_fields_for :receiver_attributes, #note.receiver do|receiver| %>
<!-- Add receiver related input here using the receiver block variable like receiver.input -->
<% end %>
<%= form.buttons %>
<% end %>
Related
I'm new to rails and just cant get that problem solved.
i have 3 models. Orders, Products and LineItems.
I want to have a order form with checkboxes for each product. User selects appropriate products and submits the order.
I cannot get the form to create the correct hash.
class Order < ActiveRecord::Base
attr_accessible :account_id, :user_id
has_many :line_items, :dependent => :destroy
end
class LineItem < ActiveRecord::Base
attr_accessible :account_id, :product_id, :order_id
belongs_to :orders
belongs_to :product
end
Here the view:
<%= form_for 'line_items[]' do |f| %>
<%= f.select :account_id, options_from_collection_for_select( Account.all,
:id, :name ), :prompt => 'Select Account' %>
<% Product.all.each do |product| %>
<div>
<%= check_box_tag 'line_items[product_ids][]', product.id %>
</div>
<% end -%>
<div>
<%= f.submit 'save' %>
</div>
thanks!
You would need to use accepts_nested_attributes_for in your model to enable nested atributes from associated models. You may also want to check out this railscast and adapt to your needs.
For example in the orders model:
class Order < ActiveRecord::Base
attr_accessible :account_id, :user_id
has_many :products #This makes the association to products
has_many :line_items, :dependent => :destroy
accepts_nested_attributes_for :products #This allows the attributes from products accessible
end
Then the form could be:
<%= form_for #order do |f| %>
<%= f.select :account_id, options_from_collection_for_select( Account.all,
:id, :name ), :prompt => 'Select Account' %>
<%= f.fields_for :product do |product_form| %>
<%= product_form.check_box :id %>
<% end %>
<%= f.submit %>
<% end %>
I'm trying to get my User to save to a Rate. I was able to get the Location to be saved to the Rate by removing the presence validation but after it's created it doesn't have the current user. How would I do this for my nested form?
User.rb
attr_accessible :email, :password
has_many :locations
has_many :rates
Location.rb
attr_accessible :name, :rates_attributes
belongs_to :user
has_many :rates
accepts_nested_attributes_for :rates, :reject_if => :all_blank
# Not sure if :all_blank works anyways as it -
# still saves even when theirs no user_id, lol
Rate.rb
attr_accessible :amount, :location_id
belongs_to :location
belongs_to :user
validates_presence_of :amount
# Couldn't use these validations
# validates_presence_of :user_id
# validates_presence_of :location_id
LocationsController
def new
#location = Location.new
#location.rates.build
end
def create
#location = current_user.locations.build(params[:location])
if #location.save.....
end
locations/new.html.erb
<%= nested_form_for #location do |f| %>
<%= f.label :name, "Name *" %>
<%= f.text_field :name %>
<%= f.link_to_add "Add Rate", :rates %>
<%= f.fields_for :rates do |r| %>
<%= r.text_field :amount %>
<%= r.link_to_remove "Remove" %>
<% end %>
<%= f.submit "Add Location" %>
<% end %>
There is a great railscast on this topic; episodes 196 and 197. Even better, Ryan wrote a gem https://github.com/ryanb/nested_form.
The gem is super easy to implement. If set up correctly the nested form grabs the parent object id on create automatically.
I don't notice anything in the code you have posted that looks wrong...what does your nested form look like in the view?
I have User, which can be one of three types: Admin, Student, Teacher. Everyone has other attributes. I am trying polymorphic association one-to-one like this:
User
class User < ActiveRecord::Base
belongs_to :identity, :polymorphic => true
accepts_nested_attributes_for :identity, :allow_destroy => true
attr_accessible :email, :login, :remember_token,
:password_confirmation, :password, :role
end
Student
class Student < ActiveRecord::Base
attr_accessible :field
has_one :user, :as => :identity
end
Controller
def new
#user = User.new
end
def create
#user = User.new(params[:user]) # It fails here.
#user.identita.build
...
end
View
<%= form_for(#user) do |f| %>
<%= f.label :login %><br />
<%= f.text_field :login %>
<%= f.fields_for [:identity, Student.new] do |i| %>
<%= i.label :field %><br />
<%= i.textfield_select :field %>
<% end %>
<% end %>
When I submit this view (more complex, but this is the core), it sends hash like this:
{"utf8"=>"✓",
"authenticity_token"=>"...",
"user"=>{"login"=>"...",
"student"=> {"field"=>"..."}
}
So it fails on marked line in controller with:
ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: student
What am I doing wrong? Something like :as=>"student" or twisting the realationship?
Firstly, fix:
<%= f.fields_for [:identity, Student.new] do |i| %>
to:
<%= f.fields_for :identity, Student.new do |i| %>
Secondly, you are trying to use accepts_nested_attributes_for on a belongs_to relationship. This is not supported behavior AFAIK. Perhaps try moving that to the Student model:
class Student < ActiveRecord::Base
attr_accessible :field
has_one :user, :as => :identity
accepts_nested_attributes_for :user, :allow_destroy => true
end
and make the view like this:
<%= form_for(Student.new) do |i| %>
<%= i.fields_for :user, #user do |f| %>
<%= f.label :login %><br />
<%= f.text_field :login %>
<% end %>
<%= i.label :field %><br />
<%= i.textfield_select :field %>
<% end %>
From documentation of attr_accessible
attr_accessible will only set attributes in this list, to assign to
the rest of attributes you can use direct writer methods.
So once you've used attr_accessible, another attributes automatically will become a protected ones.
I am new to rails and using a combination of formtastic, activeadmin,sti and polymorphic associations to build a form
When I I can create a nested form with the address parent with no problem, but when i introduce STI and attempt to build_origin_address instead of build_address, that is when I get the error below when loading the edit view
NameError in Admin/leads#edit
Showing .../app/views/admin/leads/_form.erb where line #3 raised:
uninitialized constant Lead::OriginAddress
Models:
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
belongs_to :lead
validates :line1, :presence => true, :length => {:minimum => 2}
attr_accessible :line1, :line2, :city, :state, :zip, :country
end
class OriginAddress < Address
end
class DestinationAddress < Address
end
class Lead < ActiveRecord::Base
has_one :origin_address, :dependent => :destroy, :as => :addressable
accepts_nested_attributes_for :origin_address, :allow_destroy => true
end
partial used in edit view:
<%= semantic_form_for [:admin, #lead] do |f| %>
<% #lead.build_origin_address unless #lead.origin_address %>
<%= f.inputs :name => "Lead Info" do %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<% end %>
<%= f.semantic_fields_for :origin_address do |origin| %>
<%= origin.inputs :name => "Origin Address" do %>
<%= origin.input :line1 %>
....
<% end %>
<% end %>
<%= f.buttons do %>
<%= f.commit_button %>
<% end %>
<% end %>
I think you must define #lead before your form.
I'm new to Rails development. I have two models, Decision and Choice. Every decision has two choices, which should be added to the Choices table when the Decision is saved. I'm trying to figure out how to do this in Rails using Formtastic but I've hit a wall.
I've watched the Railscast about nested forms and followed the documentation at the Formtastic GitHub site, but I'm at a loss. Here's what I have.
The models:
class Decision < ActiveRecord::Base
attr_accessible :title, :description, :user_id, :choices_attributes
belongs_to :user
has_many :choices, :dependent => :destroy
accepts_nested_attributes_for :choices
end
class Choice < ActiveRecord::Base
belongs_to :decision
end
In the Decisions_Controller:
def new
#decision = Decision.new
2.times do
#decision.choices.build
end
end
The decisions/new view:
<% semantic_form_for #decision do |form| %>
<%= form.inputs :title, :description %>
<%= form.inputs :summary, :for => :choice %>
<%= form.buttons %>
<% end %>
What I get is the form fields for title, description and one summary (for choice). How do I get the second choice to appear and get both fields to save?
Use :for => :choices instead of :for => :choice since that the name of the relation you want to reference.
<%= semantic_form_for(#decision) do |form| %>
<%= form.inputs :title, :description %>
<%= form.inputs :summary, :for => :choices %>
<%= form.buttons %>
<% end %>