Hi all. When I open /courses/new (or /courses/some_id/edit), browser returns this error:
Showing /app/views/dashboard/courses/_price.html.erb where line #1 raised:
undefined method `label' for nil:NilClass
Here are codes, _form.html.erb:
<%= simple_form_for [:dashboard, #course], html: { multipart: true } do |f| %>
//////
<%= f.fields_for :prices do |p|%>
<%= render 'price', :f => 'prices' %>
<% end %>
<%= link_to_add_association 'Add', f, :prices %>
////////
_price.html.erb:
<%= p.label :price %>
<%= p.text_field :price %>
<%= p.label :desc %>
<%= p.text_field :description %>
<%= link_to_remove_association "remove", f %>
Models:
class Price < ActiveRecord::Base
belongs_to :course
end
class Course < ActiveRecord::Base
has_many :prices
accepts_nested_attributes_for :prices, :reject_if => :all_blank, :allow_destroy => true
end
How resolve this error? And why it has arisen?
You are using simple_form_for,so i guess this line
<%= f.fields_for :prices do |p|%>
should be
<%= f.simple_fields_for :prices do |p|%>
Have a look at the Git for more Info.
In your _price.html.erb partial view, your are using a form builder that does not exists (is nil) because you did not pass it as argument:
# _price.html.erb
<%= p.label :price %>
#^ the variable `p` is the form builder here
To solve this problem, you have to pass the form builder to the partial view, like this:
<%= f.fields_for :prices do |p| %>
<%= render 'price', :f => 'prices', p: p %>
#^^^^ We pass the variable `p` (form builder) to the partial
<% end %>
Hope this helps!
Related
Order has_many jobs
Job belongs to order
And I want to edit attributes of #job.order:
<% order = #job.order %>
<%= simple_form_for [#job, order],
url: job_path(#job),
method: :put,
remote: true do |f| %>
<%= f.input :order_status, input_html: {class: 'form-control'} %>
(...)
<% end %>
any way to do it by just using input in simple form?
in job.rb
accepts_nested_attributes_for :order
in form.html.erb
simple_form_for #job do |f|
f.simple_fields_for #job.order do |order_form|
order_form.input :status
end
end
in jobs_controller.rb
params.require(:job).permit(:something, :something_else, :order_attributes => [:status])
You can use the excellent Cocoon gem https://github.com/nathanvda/cocoon to manage nested relationships, including the ability to easily add new nested relationships.
class Job < ActiveRecord::Base
has_many :orders
accepts_nested_attributes_for :orders, reject_if: :all_blank, allow_destroy: true
end
class Order < ActiveRecord::Base
belongs_to :job
end
Note the pluralization.
_form.html.erb*
<%= form_for #job do |f| %>
<%= f.label :job_name %>
<%= f.text_field :name %>
<div id='order'>
<%= f.fields_for :orders do |order| %>
<%= render 'order_fields', f: order %>
<% end %>
<div class='links'>
<%= link_to_add_association 'add order', f, :orders %>
</div>
<%= f.submit %>
<% end %>
_order_fields.html.erb partial
<div class='nested-fields'>
<%= f.label :order_name %>
<%= f.text_field :order_name %>
</div>
<%= link_to_remove_association "remove order", f %>
How do you correctly fill in the following syntax to create a dropdown select tag with each option being the data of another table?
<%= form_for(#celebrity, :html => { :multipart => true }) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.label :character %>
<%= f.collection_select(:character, :celebrity_id, #characters, :id, :name) %> #this line is the question
<%= f.submit 'Save' %>
<% end %>
I followed the API documentation here, but it doesn't seem to work.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
class Celebrity < ActiveRecord::Base
has_many :characters, through: :char_celeb_joins
has_many :char_celeb_joins
has_many :universes, through: :characters
end
class Character < ActiveRecord::Base
has_many :universes, through: :univ_char_joins
has_many :univ_char_joins
has_many :celebrities, through: :char_celeb_joins
has_many :char_celeb_joins
end
class Universe < ActiveRecord::Base
has_many :characters, through: :char_univ_joins
has_many :char_univ_joins
has_many :celebrities, through: :characters
end
But I get a
undefined method 'merge' for :name:Symbol
in the browser, when I go to a view that brings up this code.
NoMethodError in Celebrities#edit
The error is due to this line
<%= f.collection_select(:character, :celebrity_id, #characters, :id, :name) %>
When using with form_for,you have to set it like this
<%= f.collection_select(:celebrity_id, #characters, :id, :name) %>
And also your form_for object is #celebrity and you are giving :character as object to collection_select.It should be :celebrity in the collection_select too.Ofcourse you have worry about this when using collection_select without form_for.In your case it would be
<%= collection_select(:celebrity, :celebrity_id, #characters, :id, :name) %>
You can use helper:
Writer this code in any of the helper:
def character_for_select
Character.all.collect { |m| [m.id, m.name] }
end
Update your form
<%= form_for(#celebrity, :html => { :multipart => true }) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.label :character %>
<%= f.select(:character, character_for_select, :prompt => 'Select character') %>
<%= f.submit 'Save' %>
<% end %>
You will get your answer :)
Have you tried to use options_for_select. In your case, your code should be something like this:
app/helpers...
def options_for_characters( selected=nil )
options = Character.all.map { |c| [d.id, c.name] }
options_for_select( options, selected )
end
app/views...
...
<%= f.select(:character, options_for_characters, :prompt => 'Select character') %>
...
I have a little problem with gem nested_form. I have:
class Factura < ActiveRecord::Base
attr_accessible :itemfacturas_attributes
has_many :itemfacturas
has_many :productos, :through => :itemfacturas
accepts_nested_attributes_for :itemfacturas, :reject_if => lambda { |a| a[:descripcion].blank? }, :allow_destroy => true
and the ItemFactura class
class Itemfactura < ActiveRecord::Base
attr_accessor :vu, :vt, :descripcion
belongs_to :factura
belongs_to :producto
I used the gem in the facturas/new view to add itemfacturas.
<%= f.fields_for :itemfacturas do |b| %>
<%= render 'itemfacturas/itemfacturas', f: b %>
<% end -%>
<%= f.link_to_add "Agregar item", :itemfacturas %>
And the partial is:
<%= f.number_field :cantidad, :min => 0, :value => 1 %>
<%= f.text_field :descripcion, :class => "desc_autocomplete" %>
<%= f.text_field :vu %>
<%= f.text_field :vt %>
<%= f.hidden_field :producto_id%>
<%= f.link_to_remove "Eliminar" %>
But I have this error:
NoMethodError in Facturas#new
Showing
/Users/fabricioflores/desarrollo/facturacion/app/views/itemfacturas/_itemfacturas.html.erb
where line #7 raised:
undefined method `link_to_remove' for
If I comment line that contains link_to_remove I have another error about link_to_add
I followed steps from https://github.com/ryanb/nested_form but it did't work. I'm using Rails 3.2.9 and nested_form (0.3.1)
Ok I solved it. In the form of facturas/new I need to put
<%= nested_form_for #factura do |f| %>
This was the reason that can't find the link_to_add and link_to_remove, because is a different helper.
I am working on a dynamically nested form using the cocoon gem. I have two models
class CrossTable < ActiveRecord::Base
attr_accessible :title, :table_name, :database, :folder_label_id, :foreign_fields_attributes
belongs_to :folder_label
has_many :foreign_fields
accepts_nested_attributes_for :foreign_fields
validates :title, :table_name, :database, :folder_label_id, presence: true
end
class ForeignField < ActiveRecord::Base
attr_accessible :cross_table_id, :column_name, :description
belongs_to :cross_table
has_many :filter_sets
end
I have cocoon and jquery-rails in the gemfile
I added //=require cocoon to the application.js file
And here is my form partial
<%= simple_form_for #table do |f| %>
<%= f.input :title %>
<%= f.input :folder_label_id, :collection => #folders, :label_method => :title, :value_method => :id %>
<br><br>
<%= f.input :table_name %>
<%= f.input :database %>
<%= f.simple_fields_for :foreign_fields do |fields| %>
<%= render 'foreign_field_fields', :f => fields %>
<div id='links'>
<%= link_to_add_association 'Add Field', f, :foreign_fields %>
</div>
<% end %>
<%= f.button :submit %>
<% end %>
#table is an instance of the cross table model. Nothing in the foreign_field_fields partial shows up and link_to_add_association does nothing, and I get no errors. How can I start debugging this? Does anyone spot an error?
You wrote the link_to_add_association inside the simple_fields_for, which will loop over all :foreign_fields and execute the given block. So if there are no foreign-fields yet, the link_to_add_association is never shown.
You should write your view as follows (as documented):
<%= simple_form_for #table do |f| %>
<%= f.input :title %>
<%= f.input :folder_label_id, :collection => #folders, :label_method => :title, :value_method => :id %>
<br><br>
<%= f.input :table_name %>
<%= f.input :database %>
<%= f.simple_fields_for :foreign_fields do |fields| %>
<%= render 'foreign_field_fields', :f => fields %>
<% end %>
<div id='links'>
<%= link_to_add_association 'Add Field', f, :foreign_fields %>
</div>
<%= f.button :submit %>
<% end %>
Hope this helps.
Hi i am trying to do the railcasts nested form, part 1 and part 2. It seem i am having several issues and I don't understand why.
Question 1: The add and remove links don't works, they show up but don't actually execute anything
Question 2: I get the following error code and don't understand why? The error is regarding the f.error_messages is not recognized
Question 3: When trying to create a surveys i get: Can't mass-assign protected attributes: answer
Thank you here my code similar to the railcasts 196,197
model question
class Question < ActiveRecord::Base
belongs_to :survey
attr_accessible :content, :question_id, :name, :answers_attributes
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
model answer
class Answer < ActiveRecord::Base
belongs_to :question
attr_accessible :content, :question_id
end
model surveys
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
attr_accessible :name, :questions_attributes
end
views
form
<%= form_for(#survey) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<p><%= link_to_add_fields "Add Question", f, :questions %></p>
<%= f.fields_for :questions do |bf|%>
<% render 'question_fields', :f => bf %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
question_fields
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows=> 3 %>
<%= f.check_box :_destroy %>
<%= link_to_remove_fields "remove", f %>
</p>
<p><%= link_to_add_fields "Add Answer", f, :answers %></p>
<%= f.fields_for :answer do |form| %>
<%= render 'answer_fields', :f => form %>
<% end %>
answer_fields
<p class="fields">
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= link_to_remove_fields "remove", f %>
</p>
controller
javascript
application.js
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).up().insert({
before: content.replace(regexp, new_id)
});
}
application_jquery.js
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
Any help is appreciated.
Here the link to the tutorial
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
And the route not sure what you mean.
Surveysays::Application.routes.draw do
resources :surveys
end
Question1: Those functions are not being called when you press the button. In the railscast he adds the link_to_remove and link_to_add to the application helper.
module ApplicationHelper
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end
end
Question 2: Not sure, but it might clear up with some of the other fixes
Question 3: You have a type here:
<%= f.fields_for :answer do |form| %>
<%= render 'answer_fields', :f => form %>
<% end %>
Should be
<%= f.fields_for :answers do |form| %>
<%= render 'answer_fields', :f => form %>
<% end %>
It's answers because you have a has_many relations ship. The attribute answer doesn't exist and is also whitelisted by default. That's why you get mass assignment errors.
Also there is a simpler version of this in his revised episode:
http://railscasts.com/episodes/196-nested-model-form-revised