Job belongs_to :order
Order has_many :jobs
Job accepts_nested_attributes_for :order
Form to edit notes doesn't show up. Why?
<% order = #job.order %>
<%= simple_form_for #job,
url: admin_job_path(#job),
method: :put,
remote: true do |f| %>
# (...) fields for #job do show up normally
# can't see the field below:
<% f.simple_fields_for order do |form| %>
<%= form.input(
:notes,
input_html:
{
value: (order.notes),
rows: 7,
class: 'form-control'
}
)
%>
<% end %>
<br/>
<%= f.button :submit, :class => "btn btn-success btn-sm" %>
<% end %>
Adding some text because "it's mostly code". I hope the answer is clear without too much elaboration, but can always add more datails if you need. Thank you for any help!
Form to edit notes doesn't show up. Why?
You are missing = here <% f.simple_fields_for order do |form| %>. It should be
<%= f.simple_fields_for order do |form| %>
Related
I have fields_for form for update like below:
<%= form_for #cuisine, url: {action: "update" } do |f| %>
<%= f.text_area :name %>
<% #cuisine.ingredients.each do |ing| %>
<%= f.fields_for ing do |ingredient_fields|%>
<%= ingredient_fields.text_area :name, :value=> ing.name %>
<%= ingredient_fields.hidden_field :_destroy%>
<%= link_to_remove_fields 'Remove this ingredient', f %>
<% end %>
<% end %>
<% end %>
my cuisines_controller:
def update
#cuisine = Cuisine.find(params[:id])
if #cuisine.update_attributes(cuisine_params)
redirect_to root_path
else
redirect_to edit_cuisine_path
end
end
Though this shows the form correctly (showing forms filled with #cuisine and its ingredients' info), after reloading the page the #cuisine object gets deleted even when I don't push the submit button.
Any idea what's going on or how to update nested attributes using fields_for?
Thanks in advance.
First you should just use fields_for like this:
<%= f.fields_for :ingredients do |ingredient_fields|%>
<%= ingredient_fields.text_area :name %>
<%= ingredient_fields.hidden_field :_destroy %>
<%= link_to_remove_fields 'Remove this ingredient', f %>
<% end %>
Second you should first check to see if the cuisine_params in your controller permit those attributes:
params.require(:cuisine).permit(:name, ingredients_attributes:[:id, :name, :_destroy])
Lastly, make sure your model has this accepts_nested_attributes_for :ingredients, allow_destroy: true
I have a new action in the marks controller. When the user clicks on 'new marks', I need to display a form which contains list of papers for that student. And each paper has a list of options which the user needs to select.
The association between the models are:
Mark.rb
belongs_to :paper
paper.rb
has_many :options
Option.rb
belongs_to :paper
In the form for #mark, I need to display all the papers and the list of options using 'formtastic'.
I tried doing,
<% #array_papers.each do |paper| %>
<% options = paper.options %>
<%= semantic_form_for paper, url:thinking_marks_path(student_id: #student.id) do |form| %>
<li class="each-question">
<%= form.input :paper, label: "{paper[:name]}" %>
<%= semantic_fields_for :options, paper.object.options do |option| %>
<%= option.input :option, as: :check_boxes %>
<% end %>
</li>
<% end %>
</ul>
<p> <%= link_to 'Save',thinking_marks_path( student_id: #student.id ), :class => 'simple-button course-type' %>
</p>
<% end %>
But it is throwing error:
undefined method `option' for #<Paper:0x0000000fe6d3e0>
What should I do ?
The form variable is the form of paper object.
I advise you to better naming your form variables, eg. paper_form and option_form.
This code should work:
<%= semantic_fields_for :options, paper.object.options do |option_form| %>
<%= option_form.input :option, as: :check_boxes %>
<% end %>
I am getting the error
ActionView::Template::Error (undefined method `values_at' for nil:NilClass):
when using nested_form gem
Here is my code
Models
class Poll < ActiveRecord::Base
attr_accessible :question, :poll_answers_attributes
has_many :poll_answers
accepts_nested_attributes_for :poll_answers
end
class PollAnswer < ActiveRecord::Base
belongs_to :poll
attr_accessible :answer
end
View
=nested_form_for [:admin, #poll], mutipart: true, class: "form-horizontal" do |f|
.span6
.control-group
=f.label :question, class: "control-label"
.controls
=f.text_field :question, rows: "5", class: "span5"
= f.link_to_add "Add a Answer", :poll_answers
=f.submit "Create Post", class: "btn btn-primary"
StackTrace
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/builder_mixin.rb:41:in `block in link_to_add'
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:53:in `call'
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:53:in `after_nested_form_callbacks'
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:8:in `block in nested_form_for'
Any Ideas ?
As mentioned in the documentation for the nested_form gem, you have to mention the field_for of the nested object before link_to_load button. I haven't use this gem previously but after going through the documentation, i am guessing this.
Here the form looks like
<%= nested_form_for [:admin, #poll], mutipart: true, class: "form-horizontal" do |f| %>
<%= f.text_field :question %>
<%= f.fields_for :poll_answers do |poll_ans_form| %>
<%= poll_ans_form.text_field :name %>
<%= poll_ans_form.link_to_remove "Remove this task" %>
<% end %>
<p><%= f.link_to_add "Add a Answer", :poll_answers %></p>
<% end %>
You should check out simple_form for doing this type of nested form. It makes it pretty easy. Based on your models you posted, something like this should work:
# controller
def new
#poll = Poll.new
end
# new.html.erb
<%= simple_form_for #poll do |f| %>
<%= f.simple_fields_for :poll_answer do |l| %>
<%= l.input :answer, autofocus: true %>
<%= l.submit "Add", class: 'small round button' %>
<% end %>
<% end %>
Then that post should get sent to the poll#create controller if you have the route open. From there, you can work your logic on it.
Hope that helps.
I have a model
class Rcomment < ActiveRecord::Base
attr_accessible :comment, :rating
belongs_to :recipe
belongs_to :user
end
I'm trying to add comment via the show view of recipe. I populated rcomment table with dummy data and it's showing fine via:
#recipe = Recipe.find(params[:id])
#comments = #recipe.rcomments
So I tried
#newcomments = #recipe.rcomments.build(params[:recipe])
But it doesn't work at all with an error:
undefined method `rcomments_path' for #<#:0x25ccd10>
How do I get it to display a usable form_for?
%= form_for(#newcomments) do |f| %>
<%= f.label :Comments %>
<%= f.text_field :comment%>
<%= f.label :ratings %>
<%= f.text_field :rating %>
<%= f.submit "Submit Comment", class: "btn btn-large btn-primary" %>
<% end %>
You've created a form for Rcomment creation, but you don't have an rcomments entry in your routes.rb. However, I think what best fits your problem is creating rcomments through a recipe. You can do this with accepts_nested_attributes_for and fields_for.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
So, something like this--
In your Recipe model, add:
attr_accessible :rcomments_attributes
accepts_nested_attributes_for :rcomments
And in your recipes#show view:
<%= form_for(#recipe) do |f| %>
<% f.fields_for :rcomments do |cf| %>
<%= cf.label "Comments" %>
<%= cf.text_field :comment%>
<%= cf.label "Ratings" %>
<%= cf.text_field :rating %>
<% end %>
<%= f.submit "Submit Comment", class: "btn btn-large btn-primary" %>
<% end %>
I've got this tripbuilder which i want to assign categories to. So I've set up the models as where a trip can have any(or more) categories that are in the category table in my database. However; i have no idea how i can set up the form allowing a user to select categories via checkbox. Since fields_for doesn't sound like a solid way to go in this case (Because i want to see all the categories with a checkbox and select as many categories as i want). Can anyone help me out?
I've tried this form:
<%= form_for #trip, :html => {:multipart => true} do |a| %>
<%= a.label :title, "Routetitel" %>
<%= a.text_field :title %>
<%= a.label :description, "Omschrijving" %>
<%= a.text_area :description %>
<%= a.fields_for :categories do |cat| %>
<%= cat.check_box :name %>
<% end %>
<%= a.submit 'Verstuur' %>
<% end %>
At first, you need to setup the relationship between trip and category like this:
class Trip < ActiveRecord::Base
has_and_belongs_to_many :categories
end
Then you can build the form like this:
<%= form_for #trip, :html => {:multipart => true} do |a| %>
<%= a.label :title, "Routetitel" %>
<%= a.text_field :title %>
<%= a.label :description, "Omschrijving" %>
<%= a.text_area :description %>
<% Category.all.each do |cat| %>
<%= check_box_tag "trip[category_ids][]", cat.id, #trip.catergory_ids.include?(cat.id)
<% end %>
<%= a.submit 'Verstuur' %>
<% end %>
Yes, it can be done by using select tag and multiple attribute of select tag.
<% = a.select :categories, Category.all.collect {|c| [c.name, c.id]}, :include_blank => true', :multiple => "multiple" %>
Please Modify your fields_for as described below and check !!!!
<%= a.fields_for "categories[]" do |cat| %>
<%= cat.check_box :name %>
<% end %>