Inexplicable unpermitted parameters error in Rails 4 nested form - ruby-on-rails-4

I've searched high and low, and found many posts similiar, but can't seem to debug the following code.
I have a project model, which has many many teams. It has many users through teams. Here are the relevant models.
project.rb
class Project < ActiveRecord::Base
has_many :teams
has_many :users, :through => :teams
accepts_nested_attributes_for :teams
end
team.rb
class Team < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
user.rb
class User < ActiveRecord::Base
has_many :teams
has_many :projects, :through => :teams
end
projects_controller.rb
class ProjectsController < ApplicationController
def index
#projects = Project.all
end
def new
#project = Project.new
end
def create
#project = Project.create(project_params)
redirect_to projects_url
end
def show
#project = Project.find(params[:id])
end
def edit
#project = Project.find(params[:id])
end
def update
#project = Project.find(params[:id])
#project.update(type_params)
redirect_to project_url
end
def destroy
#project = Project.find(params[:id])
#project.destroy
redirect_to projects_url
end
end
private
def project_params
params.require(:project)
project_params = params.require(:project).
permit(:name, :code, :description, :externalId,
{:teams_attributes => [:id, :userId, :projectId]})
end
I have a nested form, that should allow me to create a new project and one team (just typing in the ID), however I get an unpermitted parameters exception ("Unpermitted parameters: team")
Whenever I submit the form. Form code is below.
new.html.erb
<h1>Create New Project</h1>
<%=form_for(#project) do |f| %>
<p>
<%=f.label "Name"%>
<%=f.text_field :name%> <br>
<%=f.label "Code"%>
<%=f.text_field :code%> <br>
<%=f.label "External ID"%>
<%=f.text_field :externalId%> <br>
<%=f.label "Description"%>
<%=f.text_field :description%> <br>
</p>
<ul>
<%= f.fields_for :team do |tf| %>
<li>
<%= tf.label 'User Id' %>
<%= tf.text_field :userId %>
<%= tf.label 'Project Id' %>
<%= tf.text_field :projectId %>
</li>
</ul>
<%end%>
<%=f.submit%>
<%end%>
I've paid special attention to the permitted parameters fields, and think I got them right, but, rails disagrees.
Any help would be appreciated

Your nested form doesn't work with has_many because it is singluar. You want to use f.fields_for :teams do instead (plural). Please try the following changes:
project_controller.rb
def new
#project = Project.new
#project.teams.build
end
[...]
private
[...]
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name, :code, :externalId, :description, teams_attributes: [ :user_id, :project_id ])
end
new.html.erb
<h1>Create New Project</h1>
<%=form_for(#project) do |f| %>
<p>
<%=f.label "Name"%>
<%=f.text_field :name%> <br>
<%=f.label "Code"%>
<%=f.text_field :code%> <br>
<%=f.label "External ID"%>
<%=f.text_field :externalId%> <br>
<%=f.label "Description"%>
<%=f.text_field :description%> <br>
</p>
<ul>
<%= f.fields_for :teams do |tf| %>
<li>
<%= tf.label 'User Id' %>
<%= tf.text_field :user_id %>
<%= tf.label 'Project Id' %>
<%= tf.text_field :project_id %>
</li>
<%end%>
</ul>
<%=f.submit%>
<%end%>

Change this method like this and try
private
def project_params
params.require(:project)
project_params = params.require(:project).
permit(:name, :code, :description, :externalId,
{:teams_attributes => [:id, :userId, :projectId]})
end
to
private
def project_params
params[:project].permit(:name, :code, :description, :externalId,
{:teams_attributes => [:id, :userId, :projectId]})
end

Related

Has Many Nested form not saving to database

Still learning rails and have been stuck on this for a while, I have a feeling it's a simple fix and I'm just not getting it.
I'm trying to use a separate model to populate a dropdown in another model's form. So a nested form. It is the type of activity done in a workout.
Here's what I have set up.
Workout Model
class Workout < ActiveRecord::Base
belongs_to :user
has_many :activities
accepts_nested_attributes_for :activities
validates :activity, presence: true
end
Workout Controller Params
def workout_params
params.require(:workout).permit(:rating, :activity, :workout_date, :activity_id, activity_params:[:id, :title])
end
Activity Model
class Activity < ActiveRecord::Base
belongs_to :workout
end
Activity Controller Params
def activity_params
params.require(:activities).permit(:title, :rating, workout_params:[:id])
end
View
<%= form_for(#workout) do |f| %>
<div class="control-group">
<%= f. label :activity, class: 'control-label' %>
</div>
<div class="checkbox">
<%= collection_select( :activity, :workout_id, Activity.all, :id, :title, {},
{:multiple => false }) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Thanks in advance
Fixed it. Error was in the view > collection_select. The order was wrong and i changed :activity_id to :activity
<%= collection_select :workout, :activity, Activity.all, :id, :title, {}, {:multiple => false } %>

Rails 4 Nested Form not Passing Nested Attributes in Params

When I submit the form it is passing {"utf8"=>"✓", "authenticity_token"=>"blah", "client"=>{"first_name"=>"jack", "last_name"=>"kool","complaints"=>{"symptom"=>"burnt"} in the params and I am getting Unpermitted Parameters: complaints Since complaints is nested in Client, it should be passing complaints_attributes, like I have it set up in the strong params, and I can't figure out why it isn't.
class ClientsController < ApplicationController
def new
#client = Client.new
end
def edit
#client = Client.find(params[:id])
end
def create
#client = Client.new(client_params)
if #client.save
redirect_to #client
else
render 'new'
end
end
def update
#client = Client.find(params[:id])
if #client.update(client_params)
redirect_to #client
else
render 'edit'
end
end
private
def client_params
params.require(:client).permit(:first_name, :last_name, complaints_attributes: [ :symptom, :id ])
end
end
Client model:
class Client < ActiveRecord::Base
has_many :complaints
has_one :personal_disease_history
has_one :family_disease_history
has_many :surgeries
has_many :hospitalizations
has_many :medications
has_many :allergies
validates :first_name, :last_name, presence: true
accepts_nested_attributes_for :complaints
end
Complaint model:
class Complaint < ActiveRecord::Base
belongs_to :client
end
Form:
<%= form_for :client, url: clients_path, html: { class: "form-inline" } do |f| %>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Health History Form</h3>
</div>
<div class="panel-body">
<div class="form-blocks"><legend>Name</legend>
<div class="form-group">
<%= f.label :first_name, class: "sr-only" %>
<%= f.text_field :first_name, class: "form-control", placeholder: "First Name" %>
</div>
<div class="form-group">
<%= f.label :last_name, class: "sr-only" %>
<%= f.text_field :last_name, class: "form-control", placeholder: "Last Name" %>
</div>
</div>
<div class="form-blocks"><legend>Complaints</legend>
<div class="form-group">
<%= f.fields_for :complaints do |builder| %>
<%= builder.label :symptom, class: "sr-only" %>
<%= builder.text_field :symptom, class: "form-control", placeholder: "Symptom" %>
<% end %>
</div>
</div>
<div>
<%= f.submit "Submit Form", class: "btn btn-primary btn-lg"%>
</div>
<% end %>
inside client controller create action, try saying this inside the if #client.save:
Complaint.create(symptom: params[
"client"]["complaints"]["symptom"], client_id: #client.id)
the unpermitted parameter is related to the nested attribute problem but this should get the actual record saving. If that doesn't do it, please post the stack trace surrounding the commit.
have couple other ideas: but (1) you have complaint_attributes in the permitted params but you could change that to complaint bc that's what's coming through in the params.

Rails 4: Nested objects not saving to database

I'm building a forum and I have forum_types that has_many forum_threads. I want to show a list of the different forum threads a forum type has when clicked. I can create a forum thread for a forum type but it's not showing on the forum thread 'index' page (forum_type_forum_threads_path). There is no error, just a blank page where I have placed the loop. I feel like it's something in my forum_threads_controller. If someone could please help me I would appreciate it...thanks in advance.
forum_threads_controller.rb
class ForumThreadsController < ApplicationController
before_action :require_user, except: [:show, :index]
before_action :set_forum_type
before_action :set_forum_thread, except: [:index, :new, :create]
def index
#forum_threads = ForumThread.all #something not right here
end
def show
#forum_post = ForumPost.new
end
def new
#forum_thread = ForumThread.new(params[:id])
#forum_thread.forum_posts.build
end
def create
#forum_thread = current_user.forum_threads.new forum_thread_params
#forum_thread.forum_posts.first.user_id = current_user.id
if #forum_thread.save
flash[:success] = "Your thread has been created."
redirect_to forum_type_forum_thread_path(#forum_type, #forum_thread)
else
render action: :new
end
end
private
def set_forum_type
#forum_type = ForumType.find(params[:forum_type_id])
end
def set_forum_thread
#forum_thread = ForumThread.find(params[:id])
end
def require_same_user
if current_user != #forum_thread.user
flash[:danger] = "You can't do that."
end
end
def forum_thread_params
params.require(:forum_thread).permit(:subject, :description, forum_posts_attributes: [:id, :body])
end
end
forum_types_controller.rb
class ForumTypesController < ApplicationController
before_action :set_forum_type, except: [:index, :new, :create]
def index
query = params[:q].presence || "*"
#forum_types = ForumType.search(query, suggest: true)
end
def show
#forum_type.forum_threads.each
end
def new
#forum_type = ForumType.new
end
def create
#forum_type = ForumType.new(forum_type_params)
if #forum_type.save
flash[:success] = "Forum created."
redirect_to forum_types_path
else
render action: :new
end
end
def destroy
#forum_type = ForumType.find(forum_type_params[:id]).destroy
end
def search
index
render :index
end
private
def set_forum_type
#forum_type = ForumType.find(params[:id])
end
def forum_type_params
params.require(:forum_type).permit(:title, forum_thread_attributes: [:id, :subject,
{ forum_post_attributes: [:id, :body] }])
end
end
views/forum_threads/index/html.erb
<h2><%= #forum_type.title %></h2>
<hr />
<%= link_to "New Thread", new_forum_type_forum_thread_path(#forum_type.id), class: 'btn btn-primary' %>
<% #forum_type.forum_threads.each do |forum_thread| %>
<div class="panel panel-default">
<div class="panel-body">
<h4><%= link_to forum_thread.subject, forum_type_forum_thread_path %>
<small><strong>Created by </strong>
<%= link_to forum_thread.user.name, user_path(forum_thread.user.id) if forum_thread.user %>
<%= local_relative_time(forum_thread.created_at, { type: 'time-ago', class: 'pull-right' }) %>
</small>
</h4>
</div>
</div>
<% end %>
views/forum_types/index.html.erb
<%= render 'shared/page_title', title: "Forums" %>
<%= link_to "New Forum Type", new_forum_type_path, class: 'btn btn-primary' %>
<% #forum_types.each do |forum_type| %>
<div class="panel panel-default">
<div class="panel-body">
<%= link_to forum_type.title, forum_type_forum_threads_path(forum_type.id) %>
</div>
</div>
<% end %>
models/forum_type.rb
class ForumType < ActiveRecord::Base
has_many :forum_threads
accepts_nested_attributes_for :forum_threads
has_many :forum_posts, through: :forum_threads
accepts_nested_attributes_for :forum_posts
validates :title, presence: true
end
models/forum_thread.rb
class ForumThread < ActiveRecord::Base
belongs_to :user
belongs_to :forum_type
has_many :forum_posts
accepts_nested_attributes_for :forum_posts
validates :subject, presence: true
validates_associated :forum_posts
end
routes.rb
resources forum_types do
resources forum_threads do
resources forum_posts
end
end
If you need anymore files let me know.
Added/removed some lines in create action of the forum threads controller. Then passed in the "#forum_type" and "forum_thread" after the link_to button in the view.
forum_threads_controller.rb
class ForumThreadsController < ApplicationController
before_action :require_user, except: [:show, :index]
before_action :set_forum_type
before_action :set_forum_thread, except: [:index, :new, :create]
def index
# #forum_threads = ForumThread.all
forum_threads = #forum_type.forum_threads.all.order("#{:created_at} DESC")
end
def show
#forum_post = ForumPost.new
end
def new
#forum_thread = ForumThread.new
#forum_thread.forum_posts.build
end
def create
#forum_thread = #forum_type.forum_threads.new(forum_thread_params)
#forum_thread.user = current_user
#forum_thread.forum_posts.first.user = current_user
if #forum_thread.save
flash[:success] = "Your thread has been created."
redirect_to forum_type_forum_thread_path(#forum_type, #forum_thread)
else
render action: :new
end
end
private
def set_forum_type
#forum_type = ForumType.find(params[:forum_type_id])
end
def set_forum_thread
#forum_thread = ForumThread.find(params[:id])
end
def require_same_user
if current_user != #forum_thread.user
flash[:danger] = "You can't do that."
end
end
def forum_thread_params
params.require(:forum_thread).permit(:subject, :description, forum_posts_attributes: [:id, :body])
end
end
views/forum_threads/index.html.erb
<%= render 'shared/page_title', title: #forum_type.title %>
<div class="col-md-9">
<%= link_to "New Thread", new_forum_type_forum_thread_path(#forum_type.id), class: 'btn btn-primary' %>
<% #forum_type.forum_threads.each do |forum_thread| %>
<div class="panel panel-default">
<div class="panel-body">
<h4><%= link_to forum_thread.subject, forum_type_forum_thread_path(#forum_type, forum_thread) %>
<small><strong>Created by </strong>
<%= link_to forum_thread.user.name, user_path(forum_thread.user.id) if forum_thread.user %>
<%= local_relative_time(forum_thread.created_at, { type: 'time-ago', class: 'pull-right' }) %>
</small>
</h4>
</div>
</div>
<% end %>
</div>
<%= render 'sidebar' %>

NoMethodError in Posts#show undefined method `post_comments_path'

I used associations in the models whenever I am applying associations I am getting problem ,I am new to ruby as well as rails.I am using Rails4,Eclipse,Windows Xp(sp3) and mysql5.6 ,I am getting the above error when I want clicked the show link the error is
undefined method `post_comments_path' for #<#<Class:0x2aef820>:0x2bd7df8>
Extracted source (around line #25):
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>...here it is line number 25
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
I have two model
post.rb
class Post < ActiveRecord::Base
has_many :comments
validates :title, presence: true,
length: { minimum: 5 }
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end
I have two controllers
posts_controller.rb
class PostsController < ApplicationController
def new
#post=Post.new
end
def show
#post = Post.find(params[:id])
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :text))
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
def create
#post = Post.new(post_params)
# it will also works #post=Post.new(params[:post].permit(:title,:text))
if #post.save
redirect_to #post
# or this command also works redirect_to action: :show, id: #post.id
else
render 'new'
end
end
def index
#posts = Post.all
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
comments_controller.rb
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(#post)
end
end
my routes.rb file is
Blog::Application.routes.draw do
resources :posts do
resources :comments
end
I have two migration files
create_posts.rb
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :text
t.timestamps
end
end
end
create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
#above line sets foreign key column for the association between the two models.
t.timestamps
end
#and bellow add_index line sets up an index for this association column.
add_index :comments, :post_id
end
end
my show.html.erb file is
<p>
<strong>Title:</strong>
<%= #post.title %>
</p>
<p>
<strong>Text:</strong>
<%= #post.text %>
</p>
<h2>Comments</h2>
<% #post.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit Post', edit_post_path(#post) %> |
<%= link_to 'Back to Posts', posts_path %>
I used associations in the models whenever I am applying associations I am getting problem ,I am new to ruby as well as rails.I am using Rails4,Eclipse,Windows Xp(sp3) and mysql5.6 ,I am getting the above error when I want clicked the show link the error is I used associations in the models whenever I am applying associations I am getting problem ,I am new to ruby as well as rails.I am using Rails4,Eclipse,Windows Xp(sp3) and mysql5.6 ,I am getting the above error when I want clicked the show link the error is
I have a feeling you missed the part about nested resources. As in:
resources :posts do
resources :comments
end
This means the routes generated by resources :comments are nested within your posts, which results in URLs like this:
/posts/1/comments

Rails 4.0 & Cocoon: nested fields under fields_for don't appear in Edit

Hoping someone can suggest a fix here. I am fairly new to Rails, and exploring the changes in Rails 4.0. I built this simple recipe book app in Rails 4.0. I have a main model for recipes (name, cook_time, oven_temp, instructions, etc.). Because some recipes may have 5 ingredients and others may have 20, I wanted to break ingredients out in a separate model with a has_many association. So Recipes has_many Ingredients and accepts_nested_attributes_for :ingredients. Here are the models:
recipe.rb
class Recipe < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :ingredients, :dependent => :destroy
validates :name, presence: true, length: { maximum: 50 }
accepts_nested_attributes_for :ingredients,
:reject_if => lambda { |a| a[:name].blank? },
:allow_destroy => true
end
ingredient.rb
class Ingredient < ActiveRecord::Base
belongs_to :recipe
end
Reminder: Rails 4.0 no longer uses attr_accessible, but moves assignment into the controller with strong params.
recipes_controller.rb
class RecipesController < ApplicationController
before_action :find_recipe, only: [:show, :edit, :update, :destroy]
before_action :set_current_user, except: [:index, :show]
respond_to :html, :js
...
def edit
end
def update
if #recipe.update_attributes(recipe_params)
redirect_to #recipe
else
render :edit
end
end
...
def find_recipe
#recipe = Recipe.find(params[:id])
end
private
def recipe_params
params.require(:recipe).permit( :id, :name, :category_id, :cook_time, :oven_temp, :calories, :instructions, :notes, :email, ingredients_attributes: [:id, :name])
end
end
I'm using the excellent Cocoon gem from nathanvda to manage nested form fields dynamically, and it works great in 'recipes#new', and saves the information correctly. However, the ingredients fields do not appear in the 'recipes#edit' view! Here is the code for my 'form' and ingredients_fields partials.
_form.html.erb (abbreviated)
<%= form_for #recipe, html: { class: 'form-horizontal' } do |f| %>
<fieldset>
<legend>Main Information</legend>
<div class="field form-group">
<%= f.label :name, "Recipe name", class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_field :name, class: "form-control" %>
</div>
</div>
<div class="field form-group">
<%= f.label :cook_time, class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_field :cook_time, class: "form-control" %>
</div>
</div>
...
</fieldset>
<legend>Ingredients</legend>
<p>Number of ingredients: <%= f.object.ingredients.count unless f.object.ingredients.nil? %></p>
<fieldset id="ingredients">
<% f.fields_for :ingredients do |builder| %>
<%= render 'ingredient_fields', :f => builder %>
<% end %>
<p class="links">
<%= link_to_add_association 'add ingredient', f, :ingredients, { class:"btn btn-primary" } %>
</p>
</fieldset>
<fieldset>
<legend>How to make it</legend>
<div class="field form-group">
<%= f.label :instructions, class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_area :instructions, class: "form-control", rows: "7" %>
</div>
</div>
...
</div>
</fieldset>
<% end %>
_ingredients_fields.html.erb
<div class="nested-fields">
<div class="form-group">
<%= f.label :name, "Ingredient", class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_field :name, class: "form-control" %>
</div>
<%= link_to_remove_association "remove", f %>
</div>
</div>
As I said, pretty simple, removed all error checking and messaging, just the basics. Any ideas about what I am missing? I know there are ingredients in the recipe as I load edit from a show view. Thanks in advance for any advice!
Kevin
Ack. it was as simple as an '=' sign - must have looked at the <% f.fields_for... %> a thousand times and just didn't notice I'd missed adding the <%=.
<%= f.fields_for :ingredients do |builder| %>