NoMethodError undefined method 'each' fir nil:NilClass - ruby-on-rails

I'm facing NoMethodError in NewFormsController#create, undefined method `each' for nil:NilClass !
Here is my code :
Controller : new_forms_controller.rb
class NewFormsController < ApplicationController
def new
#form = NewForm.new
end
def create
#form = NewForm.new(params[:form])
if #form.valid?
flash[:notice] = "Successfully created recommendation."
redirect_to 'new_forms/new'
else
render :action => 'new'
end
end
end
Model : new_form.rb
class NewForm
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Naming
attr_accessor :titleID, :articleID, :content, :author
validates :titleID, :articleID, :content, :author, :presence => true
validates :titleID, :articleID => {:minimum => 5 }
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
New View : new.html.erb
<%= content_for :title, "New Article Form" %>
<%= form_for #form do |f| %>
<% #form.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<p>
<%= f.label :titleID %> <br/>
<%= f.text_field :titleID %><br/>
</p>
<p>
<%= f.label :articleID %><br/>
<%= f.text_field :articleID %><br/>
</p>
<p>
<%= f.label :content %><br/>
<%= f.text_field :content %><br/>
</p>
<p>
<%= f.label :author %><br/>
<%= f.text_field :author %><br/>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
Routes : routes.rb
TestBranch::Application.routes.draw do
resources :new_forms
root :to => "new_forms#new"
end
I'm trying to implement ActiveModel because I don't want my form object to be backed up by database, I will have different service for database interactions.

You can do like this:
<% if #forms.errors.any? %>
<% #form.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<% end %>

Try the below code
<% if #form.errors.any? %>
<% #form.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<% end %>

it seems your model is correct and doens't include errors, so #form.errors contains []
you could check for errors
<% #form.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end unless #form.errors.blank? %>
update from my comments:
please try params[:new_form] instead of params[:form]. your identifier :form is wrong. your model called NewForm so your param name is new_form

Related

rails: nested-form doesn't render in view

I have models like this
post.rb
has_many :items
accepts_nested_attributes_for :items
item.rb
has_one :heading, dependent: :destroy
has_one :content, dependent: :destroy
has_one :link, dependent: :destroy
has_one :movie, dependent: :destroy
has_one :photo, dependent: :destroy
has_one :quate, dependent: :destroy
accepts_nested_attributes_for :heading
accepts_nested_attributes_for :content
accepts_nested_attributes_for :link
accepts_nested_attributes_for :movie
accepts_nested_attributes_for :photo
accepts_nested_attributes_for :quate
heading,content,link,movie,photo,quate.rb
belongs_to :item
posts_controller.rb
def new
#post = current_user.posts.build
end
def create
#post = current_user.posts.build(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'created!!' }
else
format.html { render :new }
end
end
def post_params
params.require(:post).permit(:title, :description, :image,:user_id,
items_attributes:[:id, :order,
heading_attributes:[:id, :head],
photo_attributes:[:id, :image, :title, :q_url],
movie_attributes:[:id, :y_url],
quate_attributes:[:id, :quate, :q_url, :q_title, :q_comment],
content_attributes:[:id, :content],
link_attributes:[:id, :url, :l_text],
twitter_attributes:[:id, :t_url]
])
end
posts/new_and_edit.html.erb
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description%>
<%= f.text_field :description %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.hidden_field :user_id %>
<%= f.submit %>
<%= render 'posts/item_form_fields', name: 'headings' %>
<%= render 'posts/item_form_fields', name: 'contents' %>
<%= render 'posts/item_form_fields', name: 'movies' %>
<%= render 'posts/item_form_fields', name: 'quates' %>
<%= render 'posts/item_form_fields', name: 'links' %>
<%= render 'posts/item_form_fields', name: 'photos' %>
posts/_item_form_fields.html.erb
<%= form_for(#post, remote: false) do |m| %>
<%= render 'items/form_fields', m: m, name: name %>
<%= m.submit "submit" %>
<% end %>
items/_form_fields.html.erb
<%= m.fields_for :items, #item do |b| %>
<%= b.hidden_field :order, value: '0' %>
<%= render "#{name}/form_fields", b: b %>
<% end %>
headings/_form_fields.html.erb
<%= b.fields_for :heading, #item.build_heading do |h| %>
<%= h.text_field :head %>
<% end %>
"I have a "_form_fields.html.erb" for content,link,movie, photo and quate as well. "
when I run rails server, the nested-models form doesn't show up on view.
Also I pressed "submit", console said that
param is missing or the value is empty: post
I don't get why its happen.
Do you have any idea?
Thanks
You need to correct some important things
In your posts#new action
def new
#post = current_user.posts.build
#item = #post.items.build
#item.build_heading
#item.build_content
#item.build_link
#item.build_movie
#item.build_photo
#item.build_quate
end
And your form code after all the changes should look like this
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description%>
<%= f.text_field :description %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.hidden_field :user_id %>
<%= f.fields_for #item do |m| %>
<%= render 'items/form_fields', m: m %>
<%= render 'headings/form_fields', m: m %>
<%= render 'contents/form_fields', m: m %>
<%= render 'movies/form_fields', m: m %>
<%= render 'quates/form_fields', m: m %>
<%= render 'links/form_fields', m: m %>
<%= render 'potos/form_fields', m: m %>
<% end %>
<%= f.submit %>
<% end %>
And you need to change the contents of partials like below
#items/_form_fields.html.erb
<%= m.hidden_field :order, value: '0' %>
#headings/_form_fields.html.erb
<%= m.fields_for :heading do |h| %>
<%= h.text_field :head %>
<% end %>
And the same for the remaining partials as well.

fields_for duplicates during edit

I followed this railscast.
Controller: project_sub_types_controller.rb
def new
#svn_repos = ['svn_software','svn_hardware']
#project_sub_type = ProjectSubType.new
#project_sub_type.repositories.build
end
def edit
#svn_repos = ['svn_software','svn_hardware']
#project_sub_type = ProjectSubType.find(params[:id])
end
Model: project_sub_type.rb
class ProjectSubType < ActiveRecord::Base
belongs_to :project_type
has_many :repositories, :dependent => :destroy
def repositories_attributes=(repos_attributes)
repos_attributes.each do |attributes|
repositories.build(attributes)
end
end
end
View: _form.html.erb
<%= form_for #project_sub_type, :html => {:class => 'project_subtype_form'} do |f| %>
<%= f.label :name, "Project sub type name" %>
<%= f.text_field :name %>
<% for repos in #project_sub_type.repositories %>
<%= fields_for "project_sub_type[repositories_attributes][]", repos do |repos_form| %>
<% #svn_repos.each do |repos| %>
<%= repos_form.check_box :repos_name, {}, "#{repos}", nil %>
<%= h repos -%>
<% end %>
<% end %>
<% end %>
<%= f.submit "Save"%>
This works perfectly during creation of a new record. But Y does the fields_for duplicates during edit. During create I see 2 checkboxes but during edit there are 4 checkboxes which duplicates the other 2 checkboxes. What am I doing wrong?
Update : The more times I click on edit and the duplication increases by 1.
<% for repos in #project_sub_type.repositories %>
<%= fields_for "project_sub_type[repositories_attributes][]", repos do |repos_form| %>
<% #svn_repos.each do |repos| %>
<%= repos_form.check_box :repos_name, {}, "#{repos}", nil %>
<%= h repos -%>
<% end %>
<% end %>
<% end %>
Get rid of that and do:
<%= f.fields_for :repositories do |repo_form| %>
<% #svn_repos.each do |rep| %>
<%= repo_form.check_box :repos_name, {}, rep, nil %>
<%= h rep -%>
<% end %>
<% end %>
Also get rid of repositories_attributes= method in your model and add accepts_nested_attributes_for :repositories
The railscast you linked is 7 years old. :)

Rails 4 Permitted Params with nested attributes

Alright, been looking everywhere for this one. Tried all the solutions. Maybe someone can help on this.
So, I have a WebRequest model that has many WebSites. Each WebSite belongs to a WebRequest. My problem is in the nested form. Ive gone in an permitted the params (atleast, based on the documentation I have) and everything works fine until I go into the server logs. Posted below
class WebRequest < ActiveRecord::Base
has_many :web_sites
accepts_nested_attributes_for :web_sites
end
and here is the WebSite model
class WebSite < ActiveRecord::Base
belongs_to :web_request, dependent: :destroy
end
_form.html.erb
<% 1.times do %>
<%= f.fields_for :web_site do |ff| %>
<%= ff.input :url %>
<%= ff.input :description %>
<% end %>
<% end %>
WebRequests Controller
class WebRequestsController < ApplicationController
def new
#web_request = WebRequest.new
# #web_request.web_sites.build
end
def index
#web_requests = WebRequest.all
end
def create
#web_request = WebRequest.new(web_request_params)
respond_to do |format|
if #web_request.save
RequestMailer.web_request_submit(#web_request).deliver
format.html { render partial: 'success' }
format.json { render action: 'show', status: :created, location: #web_request }
else
format.html { render action: 'new' }
format.json { render json: #web_request.errors, status: :unprocessable_entity }
end
end
end
def web_request_params
params.require(:web_request).permit(:web_needs, :primary_goal, :secondary_goal, : :call_to_action, :hero_image, :image_count, :existing, :resources, :web_examples, :special_functions, :social_network, web_sites_attributes: [:id, :url, :description])
end
end
And here is the server log:
Started POST "/web_requests" for 127.0.0.1 at 2014-07-10 15:56:12 -0400
Processing by WebRequestsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0iisNgGk/AhC4jRrp1cmKWVCBsCcSx5G2dueEI/+p2A=", "web_request"=>{"web_needs"=>"", "primary_goal"=>"", "secondary_goal"=>"", "call_to_action"=>"", "hero_image"=>"", "image_count"=>"", "existing"=>"", "web_site"=>{"url"=>"TROLL", "description"=>"TROLL"}, "resources"=>"", "special_functions"=>"", "social_network"=>""}, "commit"=>"Create Web request"}
Unpermitted parameters: web_site
Notice at how the form fields get passed but they get restricted out of making it to the DB.
THANKS!
Update::
Here is the full form path:
<%= simple_form_for(#web_request) do |f| %>
<% if #web_request.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#web_request.errors.count, "error") %>
prohibited this Web Request from being saved:</h2>
<ul>
<% #web_request.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="container">
<div class="field">
<%= f.input :web_needs, label: 'What web needs do you have?' %>
</div>
<div class="container"><h5>Please list below 5 URL's and explanations of why you like them.</h5></div>
<% 1.times do %>
<%= f.fields_for :web_sites do |ff| %>
<div class="field">
<%= ff.input :url, label: "URL of example site" %>
</div>
<div class="field">
<%= ff.input :description, label: "Description of example site" %>
</div>
<% end %>
<% end %>
<div class="field">
<%= f.input :resources, label: 'Will you be providing any kind of resource on this page? e.g. chord chart download.' %></br>
</div>
</div>
<div class="actions">
<%= f.button :submit, :class => "button" %>
</div>
</div>
<% end %>
Update::Full error log
undefined method `url' for #<ActiveRecord::Associations::CollectionProxy []>
The line is here
<% 1.times do %>
<%= f.simple_fields_for :web_site, #web_request.web_sites do |ff| %>
<div class="field">
<%= ff.input :url, label: "URL of example site" %> <--ERROR HERE on ':url'
</div>
<div class="field">
<%= ff.input :description, label: "Description of example site" %>
</div>
<% end %>
Add to controller in new action:
def new
#web_request = WebRequest.new
#web_site = #web_request.web_sites.build
end
and form:
<%= f.simple_fields_for #web_site do |ff| %>
<%= ff.input :url %>
<%= ff.input :description %>
<% end %>
<% end %>
and web_request_params:
def web_request_params
params.require(:web_request).permit(:web_needs,
:primary_goal,
:secondary_goal,
:call_to_action,
:hero_image,
:image_count,
:existing,
:resources,
:web_examples,
:special_functions,
:social_network,
{ web_sites: [:id, :url, :description] })
end
In your controller in web_request_params change web_sites_attributes for web_site_attributes
In your controller remove the comment from #web_request.web_sites.build
In your view remove 1.times do
In yor form change f.fields_for :web_sites do |ff| with f.simple_fields_for :web_sites do |ff|

Trying to follow a book but my form isn't being generated, why is this?

Trying to follow a book but my form isn't being generated, why is this?
Here is my new.html.erb
<h1>New ad</h1>
<% form_for :ad,:url=>{:action=>'create'} do |f| %>
<p><b>Name</b><br /><%= f.text_field :name %></p>
<p><b>Description</b><br /><%= f.text_area :description %></p>
<p><b>Price</b><br /><%= f.text_field :price %></p>
<p><b>Seller</b><br /><%= f.text_field :seller_id %></p>
<p><b>Email</b><br /><%= f.text_field :email %></p>
<p><b>Img url</b><br /><%= f.text_field :img_url %></p>
<p><%= f.submit "Create" %></p>
<% end %>
This is my ads_controller
class AdsController < ApplicationController
def show
#ad = Ad.find(params[:id])
end
def stats
#seller = Seller.find(params[:id])
end
def index
#ads = Ad.find(:all)
end
def new
#ad = Ad.new
end
end
and my routes is
resources :ads
This is all that I get
http://imgur.com/Zkyn9wL
<% form_for should be <%= form_for or the form template will not be rendered.

Nested model form with collection in Rails 2.3

How can I make this work in Rails 2.3?
class Magazine < ActiveRecord::Base
has_many :magazinepages
end
class Magazinepage < ActiveRecord::Base
belongs_to :magazine
end
and then in the controller:
def new
#magazine = Magazine.new
#magazinepages = #magazine.magazinepages.build
end
and then the form:
<% form_for(#magazine) do |f| %>
<%= error_messages_for :magazine %>
<%= error_messages_for :magazinepages %>
<fieldset>
<legend><%= t('new_magazine') %></legend>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<fieldset>
<legend><%= t('new_magazine_pages') %>
<% f.fields_for :magazinepages do |p| %>
<p>
<%= p.label :name %>
<%= p.text_field :name %>
</p>
<p>
<%= p.file_field :filepath %>
</p>
<% end %>
</fieldset>
<p>
<%= f.submit :save %>
</p>
</fieldset>
<% end %>
problem is, if I want to submit a collection of magazinepages, activerecord complaints because it's expected a model and not an array.
create action:
def create
#magazine = Magazine.new params[:magazine]
#magazine.save ? redirect_to(#magazine) : render(:action => 'new')
end
In magazine:
accepts_nested_attributes_for :magazinepages
Magazine.new(params[:magazine]) will then handle the object hierarchy for you automatically
I'm not 100% sure what you're asking, but if you're trying to instantiate a new magazine, with many magazinepages, you'll need to iterate over each magazine page. Something like this:
def create
#magazine = Magazine.new(params[:magazine])
if params[:magazinepages]
params[:magazinepages].each do |page|
#magazine.magazinepages.build(page)
end
end
# Save the model, do your redirection or rendering invalid model etc
end

Resources