For reference, this is using Rails 3.0.9.
I can't seem to figure out why, but I cannot update a field which was left blank at the initial creation of the object/row.
If my model looks like this:
name - string
date - date
message - string
If I put info in all of the fields upon initial creation, everything can be updated without a hitch later on. But if I do not put info in one of the fields, say the title, I cannot update that field after initial creation.
I'm not doing anything out of the ordinary, the form is pretty plain jane:
<%= form_for #event do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :date %><br />
<%= f.date_select :date %>
</p>
<p>
<%= f.label :message %><br />
<%= f.text_field :message %>
</p>
<p><%= f.submit %></p>
<% end %>
And my controller is equally as simple:
def update
if #event.update_attributes(params[:event])
redirect_to #event, :notice => "Successfully updated event."
else
render :action => 'edit'
end
end
And the model:
class Event < ActiveRecord::Base
has_many :races, :dependent => :destroy
has_many :price_groups, :dependent => :destroy
accepts_nested_attributes_for :races, :reject_if => lambda{ |a| a[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :price_groups, :reject_if => lambda{ |a| a[:name].blank? }, :allow_destroy => true
attr_accessible :name, :date, :message, :races_attributes, :price_groups_attributes
end
Related
Nesting forms always seems to mangle my brain. Any help would be appreciated.
I want to make a booking form for a course. The course has start dates associated with it. I want the form to update the course start dates once the course is selected from the dropdown selection in the form. I have this much done. I've added cocoon gem and have the forms all set up but I cannot seem to get the form to generate a list based on the course selected in the form.
Models:
class Booking < ApplicationRecord
belongs_to :course
belongs_to :user
end
class Course < ApplicationRecord
has_many :bookings
has_many :schedules
accepts_nested_attributes_for :schedules, :reject_if => :all_blank, :allow_destroy => true
end
class Schedule < ApplicationRecord
belongs_to :course
end
Form Views
Booking Form
<%= simple_form_for(#booking) do |f| %>
<%= f.error_notification %>
<div class="ice-card">
<!-- <div class="form-inputs">-->
<%= f.input :user_id, :input_html => { :value => current_user.id} %>
<%= f.input :user_email, :input_html => { :value => current_user.email}, label: "Contact Email" %>
<%= f.association :course, label_method: :header, :input_html => { :selected => :course }, :include_blank => false, label: "Course Name" %>
<%= f.simple_fields_for :schedules do |schedule| %>
<%= render 'schedule_fields', :f => schedule %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit, class: "btn btn-primary" %>
</div>
<% end %>
Schedule Form partial
<%= f.input :start_date, as: :date %>
I have the following code form my passport_visas.rb model
ActiveAdmin.register PassportVisa do
menu :label => "Visas"
form :partial => "form"
index :title => "Visas"
end
And this is my code for the partial "form"
<%= semantic_form_for [:admin, #passport_visa] do |f| %>
<%= f.inputs "Main information" do %>
<%= f.input :country %>
<%= f.input :citizenship, :as => :radio, :collection => {"US Citizen" => 0, "Foreign National" => 1} %>
<%= f.input :visa_type, :as => :select, :collection => ["Tourist", "Business", "Official"] %>
<%= f.input :visa_required, :label => "Is Visa Required?", :as => :radio, :collection => {"Required" => 0, "Not Required" => 1} %>
<%= f.input :maximum_stay, :label => "Maximum Stay" %>
<% end %>
<fieldset class="actions">
<ol>
<li class="action input_action" id="passport_visa_add_new_entry">
<input name="new_entry" type="button" value="Add New Entry">
</li>
<li class="action input_action" id="passport_visa_remove_entry">
<input name="remove_entry" type="button" value="Remove Entry">
</li>
</ol>
</fieldset>
<!-- Problem -->
<%= f.inputs "Entries" do %>
<%= f.has_many :visa_entries do |entry| %>
<%= entry.input :type_of_entry, :as => :select, :collection => ["Testing"] %>
<% end %>
<% end %>
<%= f.actions %>
<% end %>
And I'm getting this error message: "undefined method `has_many' for #Formtastic::FormBuilder:0x10c234c38"
I'm trying the entire day to setup this form, here's my model's code:
passport_visa.rb
class PassportVisa < ActiveRecord::Base
has_many :visa_entries
accepts_nested_attributes_for :visa_entries
end
visa_entry.rb
class VisaEntry < ActiveRecord::Base
belongs_to :passport_visa
attr_accessible :type_of_entry, :maximum_validity, :embassy_fees, :service_fees, :processing_time
end
Finally I found a work around for that. So here's what I did to make it work:
First I got rid of the _form partial, because for some reason the "has_many" don't work inside there.
I modified my 2 models:
passport_visa.rb
class PassportVisa < ActiveRecord::Base
has_many :visa_entries
accepts_nested_attributes_for :visa_entries
attr_accessible :visa_entries_attributes, :country, :citizenship, :visa_type, :visa_required, :maximum_stay
validates_presence_of :country, :citizenship, :visa_type, :visa_required, :maximum_stay
end
visa_entry.rb
class VisaEntry < ActiveRecord::Base
belongs_to :passport_visa
attr_accessible :type_of_entry
validates_presence_of :type_of_entry
end
And the last part, the actual form:
form do |f|
f.inputs "Entries" do
f.has_many :visa_entries do |ff|
ff.input :type_of_entry, :as => :select, :collection => ["Testing"]
end
end
f.actions
end
Bye!
I am trying to get a Project form to build the first (starting) time of several (up to 12) volunteer time blocks.
project.rb
class Project < ActiveRecord::Base
attr_accessible :title, ...
has_many :vol_times, :dependent => destroy
accepts_nested_attributes_for :vol_times, :reject_if => lambda { |a| a[:start_time].blank? }, :allow_destroy => true
...
end
vol_time.rb
class Vol_time < ActiveRecord::Base
attr_accessible :start_time, ...
belongs_to :project
end
ProjectsController
class ProjectsController < ApplicationController
before_filter :signed_in_user, only: :create
...
def new
#project = Project.new
#user = current_user
#project.vol_times.build
end
...
end
Vol_Times Controller
class Vol_TimesController < ApplicationController
def new
#reward = Reward.new
end
...
end
My view looks like this...
<%= form_for(#project) do |f| %>
<div class="form_field_block">
<p class="form_label"> Project Title</p>
<%= f.text_field :title, :size => 40, :placeholder => " Project Title...", :class => "frm" %>
</div>
<div class="form_field_block">
<p class="form_label"> Project Sub-title</p>
<%= f.text_field :sub_title, :size => 40, :placeholder => " Project Sub-title...", :class => "frm" %>
</div>
<p class="clearing"></p>
<div class="form_field_block">
<% f.fields_for :vol_times do |builder| %>
<%= render :partial => 'start_time', :f => builder %>
<% end %>
</div>
<p class="clearing"></p>
<%= button_tag "btn_start_project.png", :class => "btn_save" %>
<% end %>
And the _partial looks like this...
<%= f.label :start_time, "Starting Time" %>
<%= f.text_field :start_time %>
When I view the page, I see the containing <div>, but not the contents of the ERB, which should be parsed from the _partial.
Any ideas why this isn't working? I got the general context from Ryan Bates' RailsCast #196 - Here
you are missing a = on the fields_for. It should be
<%= f.fields_for :vol_times do |builder| %>
I'm getting the infamous error:
Can't mass-assign protected attributes: comments
As far as I can tell, I've done everything right in my model:
class Book < ActiveRecord::Base
attr_accessible :author, :edition, :issue, :title, :url, :comments_attributes
has_many :comments, :dependent => :destroy
accepts_nested_attributes_for :comments, :allow_destroy => true
end
And view:
<%= form_for #book, :remote => true do |f| %>
<%= f.label :title %>:
<%= f.text_field :title %><br />
<%= f.label :author %>:
<%= f.text_field :author %><br />
<%= f.fields_for #comment do |comment| %>
<%= comment.label :body, :Comment %>:
<%= comment.text_area :body %><br />
<% end %>
<%= f.submit %>
<% end %>
What am I missing here? I've read through over a dozen similar questions on stackoverflow, but the only advice out there seems to be to add attr_accessible and accepts_nested_attributes_for, both of which I've already got. Surely there's something else?
You have fields_for #comment do |comment|
It should be:
fields_for :comments do |comment|
Then in the next line I would probably just use a string "Comment" for the label.
i have many to many throught asociation and then i do this
fields_for :device
this displaying in good way, but i cannot save it i get unknown attribute :price
And fields_for :devices
And so on, one device makes one more repeat, if i write f.fields_for :price it gives good text field count, but it write
unknown attribute: price
Rails.root: C:/Users/Ignas/mildai/baze4
Application Trace | Framework Trace | Full Trace
app/controllers/commercial_offers_controller.rb:74:in `block in update'
app/controllers/commercial_offers_controller.rb:73:in `update'
thank you
additional information:
controller
def edit_prices
#commercial_offer = CommercialOffer.find(params[:id])
end
link to edit prices
<%= link_to "Edit prices", :controller => "CommercialOffers", :action => "edit_prices", :id => #commercial_offer %>
_edit_price.html.erb
<%= form_for #commercial_offer do |f| %>
<% CommercialOffer.find(params[:id]).devices.each do |device| %>
<%= check_box_tag "commercial_offer[device_ids][]", device.id, #commercial_offer.devices.include?(device) %>
<%= device.name %>
<%= f.fields_for :prices do |builder| %>
<%= render 'prices/new_price', :f => builder, :commercial_offer => #commercial_offer, :device => device %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit "Save"%>
</div>
<% end %>
for one device it have render only one time, but it rendering for one device such times how many devices is with same commercial_offer_id
_new_price.html.erb
Price:
<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id, :value => device.id %></br>
class CommercialOffer < ActiveRecord::Base
has_many :prices
has_many :devices, :through => :prices
accepts_nested_attributes_for :prices
accepts_nested_attributes_for :devices
end
class Device < ActiveRecord::Base
has_many :prices
accepts_nested_attributes_for :prices
has_many :commercial_offer, :through => :prices
class Price < ActiveRecord::Base
belongs_to :device
belongs_to :commercial_offer
end
There is no any field_for method.
Also your question isn't clear. What actually you want to do? Show only one device? Wich one? First, last?
UPD
commercial_offer.rb:
class CommercialOffer < ActiveRecord::Base
has_many :prices
has_many :devices, :through => :prices
accepts_nested_attributes_for :prices
accepts_nested_attributes_for :devices, :allow_destroy => true
end
Your view
<%= form_for #commercial_offer do |f| %>
<%= f.fields_for :devices do |device| %>
<p>
<%= device.check_box :_destroy %>
<%= device.label "Destroy?" %>
</p>
<p>
<%= device.text_field :name %>
</p>
<%= device.fields_for :prices do |builder| %>
<%= render 'prices/new_price', :f => device %>
<% end %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit "Save"%>
</div>
<% end %>
_new_price partial
<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id %></br>