Rails form for nested model - ruby-on-rails

I'm trying to figure out how to change the standard rails scaffolding form so that it works for a nested model.
So here's the deal: checklists have many checks. But when I do the standard scaffolded form:
<%= form_for(#check) do |f| %>
<% if #check.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#check.errors.count, "error") %> prohibited this check from being saved:</h2>
<ul>
<% #check.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I get this error (accessing /checklists/1/checks/new):
undefined method `checks_path' for #<#<Class:0x00000101795070>:0x000001017913a8>
Extracted source (around line #1):
1: <%= form_for(#check) do |f| %>
2: <% if #check.errors.any? %>
3: <div id="error_explanation">
What should I change in this form?

Railscasts' Nested Model Form would be a good tutorial for you.

Related

Rails Added column to a scaffold

I successfully added a column "name" to "posts", When I check the post in the rails console it says name is nil even after I added a post to the form.html, this is the form file.
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post
from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :description %>
<%= form.text_field :description, id: :post_description %>
</div>
<div class="field">
<%= form.label :Name %>
<%= form.text_field :name, id: :post_name %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Make sure you have added that attribute in your permitted param list in your controller.

Try to generate a dynamic field in Rails

I want to generate a dinamic field in my form on Rails. This is the code:
<%= form_for(#item) do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :vehicle %><br>
<%= f.text_field :vehicle %>
</div>
<div class="field">
<%= f.label :part %><br>
<%= f.text_field :part %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My idea is put a script into the form where i can generate a field part dinamicaly with a button with a text like "add" for another part field. Any idea?? Thanks everyone.

undefined method `model_name', Rails

I am trying to implement simple form in home#index page using:
<%= render "forms/form"%>
Form look like this:
<%= form_for(#form) do |f| %>
<% if #form.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#form.errors.count, "error") %> prohibited this form from being saved:</h2>
<ul>
<% #form.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :defect %><br />
<%= f.text_field :defect %>
</div>
<div class="field">
<%= f.label :region %><br />
<%= f.text_field :region %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
But when I access that page, I got this error message:
undefined method `model_name' for NilClass:Class
First of all I though it is because my model name is also Form, but then I checked Rails 3 reserved words, but there wasn't "form" !
Thanks in advance!
In home controller, set the instance variable #form in the index action as:-
def index
#form = Form.new
end
Your #form instance variable's value is nil. You should set it in controller.

Rails 3, mysql join tables and habtm

I created 2 tables called products and brands and created a join table called brands_products via migration.
In each of the models I wrote the corresponding has_and_belongs_to_many setting.
In a form if have the following code:
<%= form_for(#product) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :brand %><br />
<%= f.text_field (what to write?) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I don't know how to add a brand to a product intuitively, like the way Rails usually works...any thoughts?
You may want to look at accepts_nested_attributes_for. You'll also want to look at fields_for. Without seeing more of your data model, it's hard to give a detailed answer.
Generally, it would look something like:
<%- f.fields_for :brands do |m| -%>
<%= m.text_field :name %>
<%- end -%>

Rails Screencast - Completely stuck on "fields_for"

i am following this simple tutorial and i followed all the steps... but the browser simply doesn't show me anything i put inside the fields_for tag.
<%= form_for :activity, :url => activities_path do |f| %>
<% if #activity.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
<ul>
<% #activity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Foto" %><br />
<%= f.text_field :photo %>
</div>
<div class="field">
<%= f.label "Foto per la home" %><br />
<%= f.text_field :photoHome %>
</div>
<% for info in #activity.infos %>
This is rendered<br>
<% fields_for "activity[info_attributes][]", info do |info_form| %>
This is not rendered<br>
<p>
Titolo: <%= info_form.text_field :title %>
</p>
<p>
Descrizione: <%= info_form.text_field :description %>
</p>
<% end %>
<% end %>
<p><%= submit_tag "Create activity" %></p>
<% end %>
The above code result is this:
What am i missing?
Rails is sometimes (well ok, often) a bit confusing in which helpers want a <% vs which helpers want <%=. Try
<%= fields_for "activity[info_attributes][]", info do |info_form| %>
The tutorial you're following doesn't, but that's from 2007, and in Rails 3 this was changed. Current documentation on fields_for is here.

Resources