Can't get 'has_and_belongs_to_many' to work - ruby-on-rails

Made a previous post about this but after a lot of testing and changing it's outdated.
I'm trying to tie users and activities together.(One user can have several activities. An activity can have several users). I'ts pretty much a multi-user agenda thing.
Whenever i create a new activity i can select users by checkbox which will be doing the activity.
I can't get it to work though; The users won't show up in my activity show. Printing #activity.users in my show results in #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_User:0x43d2158> but looping it, or checking my activities.js shows nothing(The activities.js shows "users":[] in the activity. So I'm pretty certain they're not associated properly.
Here's some code:
The activities.js index method
def index
#activities = Activity.all
respond_to do |format|
format.html
format.js {render_json #activities.to_json(:include => [:pictogram ,:users]) }
end
end
The activities' form(Loops all users)
<%= form_for(#activity) 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 :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :start_date %><br>
<%= f.date_select :start_date %>
</div>
<div class="field">
<%= f.label :end_date %><br>
<%= f.date_select :end_date %>
</div>
<div class="users">
<% for user in User.all %>
<label class="activity">
<%= check_box_tag "activity[user_ids][]", user.id %>
<%= user.name %>
</label>
<% end %>
</div>
<div class="pictograms">
<% for p in Pictogram.all %>
<% #f.fields_for :pictograms do |x| %>
<%= p %>
<% #end %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The schema.rb
ActiveRecord::Schema.define(version: 20130911095113) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "activities", force: true do |t|
t.string "title"
t.date "start_date"
t.date "end_date"
t.integer "pictogram_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "activities", ["pictogram_id"], name: "index_activities_on_pictogram_id", using: :btree
create_table "activities_users", id: false, force: true do |t|
t.integer "activity_id"
t.integer "user_id"
end
add_index "activities_users", ["activity_id"], name: "index_activities_users_on_activity_id", using: :btree
add_index "activities_users", ["user_id"], name: "index_activities_users_on_user_id", using: :btree
create_table "pictograms", force: true do |t|
t.string "title"
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "name"
t.text "avatar"
t.date "birthdate"
t.datetime "created_at"
t.datetime "updated_at"
end
end
Activity.rb
class Activity < ActiveRecord::Base
belongs_to :pictogram
has_and_belongs_to_many :users
accepts_nested_attributes_for :pictogram, allow_destroy: false
accepts_nested_attributes_for :users, allow_destroy: false
end
User.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :activities
validates_presence_of :name, :on => :create
validates_presence_of :birthdate, :on => :create
accepts_nested_attributes_for :activities, allow_destroy: false
end
And the activity params from my activities controller
def activity_params
params.require(:activity).permit(:title,
:start_date,
:end_date,
:users,
:pictogram)
end

Try sth like this:
class Activity < ActiveRecord::Base
belongs_to :pictogram
has_and_belongs_to_many :users
accepts_nested_attributes_for :pictogram, allow_destroy: false
def user_ids=(values)
self.users << User.find(values)
end
end
#controller
def activity_params
params.require(:activity).permit(:title,
:start_date,
:end_date,
{:user_ids =>[]},
:pictogram)
end

I suggest you try some minimalist debugging first,
User.create!(...) # create a valid user record
Activity.create!(...) # same as above
User.all.first.activities << Activity.all.first
User.all.first.activities.any?
See if this works, also keep an eye on your join_table and check that actual records are being persisted. Your schema looks good as well.

I think, none of them read about the strong_parameters with nested attributes :
You can easily define :users_attributes in permit parameters for nested_attributes.
activities controller :
def activity_params
params.require(:activity).permit(:title,
:start_date,
:end_date,
users_attributes: [:id])
end
This is the ideal way to do this. Thanks

Related

Working on rails, i have text fields and text areas to receive text, but it shows the validation error "Topic cannot be empty"

After typing in topic and details in the appropriate text field and area, it still shows "validation error" "Topic cannot be empty". what is the problem? I cannot seem to figure it out.
This is the task partial
_task.html.erb:
<li id="task-<%= task.id %>">
<%= link_to gravatar_for(task.user, size: 50), task.user %>
<span class="task"><%= link_to task.user.name, task.user %></span>
<span class="topiclabel">Title:</span>
<span class="topic"style=color:black> <%= task.topic %></span>
<span class="detailslabel">Details: </span>
<span class="detail"><%= task.detail %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(task.created_at) %> ago.
</span>
</li>
Schema:
ActiveRecord::Schema.define(version: 20170526194708) do
create_table "stages", force: :cascade do |t|
t.text "topic"
t.datetime "duration"
t.text "detail"
t.integer "task_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["task_id"], name: "index_stages_on_task_id"
t.index ["user_id", "created_at"], name: "index_stages_on_user_id_and_created_at"
t.index ["user_id"], name: "index_stages_on_user_id"
end
create_table "tasks", force: :cascade do |t|
t.text "topic"
t.integer "member"
t.text "detail"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id", "created_at"], name: "index_tasks_on_user_id_and_created_at"
t.index ["user_id"], name: "index_tasks_on_user_id"
end
routes.rb:
resources :tasks, only: [:create, :destroy]
task.rb
class Task < ApplicationRecord
belongs_to :user
has_many :stages
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :topic, presence: true, length: { maximum: 140 }
validates :detail, presence: true, length: { maximum: 140 }
end
Project_pages_controller.rb:
def home
if logged_in?
#task = current_user.tasks.build
end
_task_form.html.erb:
<%= form_for(#task) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :topic, placeholder: "Compose new task..." %>
<%= f.text_area :detail, placeholder: "Write task details..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
This is the show page
show.html.erb
<div class="col-md-8">
<% if #user.tasks.any? %>
<% if #user.stages.any? %>
<h3>Tasks (<%= #user.tasks.count %>)</h3>
<ol class="tasks">
<%= render #tasks %>
<%= render #stages %>
</ol>
<%= will_paginate #tasks %>
<% end %>
<% end %>
</div>
This is my controller;
class TasksController < ApplicationController
include TasksHelper
before_action :logged_in_user, only: [:create, :destroy]
def create
#task = current_user.tasks.build(task_params)
if #task.save
flash[:success] = "Task created!"
redirect_to root_url
else
#feed_items = []
render 'workshareapp_pages/home'
end
end
def destroy
end
private
def task_params
params.require(:task).permit(:detail)
end
end
validates :topic, presence: true, length: { maximum: 140 }
This clause validates the topic attribute in your record to be present and within 140 char length. This is used to validate record before save. You may remove it if you really dont need it here. Another option you can create a form model which is used specifically for this form.
It's probably a validation in your model Task.
You are missing the :topic field in your permit, which is why it's not being processed.
def task_params
params.require(:task).permit(:detail, :topic)
end

Can't save my data into sqlite database

I am trying to implement a Parking Permit application page using ROR. I couldn't get my data saved into the database. The permit database is associated with the user also. The program won't save the data and execute the else statement. There is no error generated, i think i have missed something but i don't know the exact problem. Any help is appreciated!
Permit_controller.rb
class PermitsController < ApplicationController
before_action :set_permit, only: [:show, :destroy]
def index
#permits = Permit.all
end
def new
#permits = Permit.new
end
def create
#permits = Permit.new(permit_params)
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
def destroy
end
def show
#permits = Permit.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate,:permitstart, :permitend)
end
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
end
Create_permit.rb
class CreatePermits < ActiveRecord::Migration[5.0]
def change
create_table :permits do |t|
t.string :vehicle_type
t.string :name
t.string :studentid
t.string :department
t.string :carplate
t.date :permitstart
t.date :permitend
t.references :user, foreign_key: true
t.timestamps
end
add_foreign_key :permits, :user
add_index :permits, [:user_id, :created_at]
end
end
User.rb
class User < ApplicationRecord
has_secure_password
has_many :permits
end
#book pg 264 Validation
permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#permits) do |f| %>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, class: 'form-control' %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, class: 'form-control' %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :permit_start %>
<%= f.date_select :permitstart, class: 'form-control' %>
<%= f.label :permit_end %>
<%= f.date_select :permitend, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
schema.rb
ActiveRecord::Schema.define(version: 20160921071908) do
create_table "permits", force: :cascade do |t|
t.string "vehicle_type"
t.string "name"
t.string "studentid"
t.string "department"
t.string "carplate"
t.date "permitstart"
t.date "permitend"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_permits_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.integer "user_type"
end
end
check with this #permits.save!.
it shows the exact error.
module ApplicationHelper
#for current user to use through out the app
def current_user
#current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
end
end
and
def create
#permits = Permit.new(permit_params)
#permits.user = current_user
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
test it
Or you can just say that a permit has a single user and avoid the confusion.
#models/permit.rb
class Permit < ApplicationRecord
has_one :user
end
#controllers/permit_controller.rb
def create
#user = User.find(session[:user_id]) #use your session variable
#permits = Permit.new(permit_params)
if #permits.save
#user.permits << #permits
redirect_to root_path
else
redirect_to contact_path
end
end
It will save permits for the logged in user.

How to set up a form to create several records on a has_many :through join table?

I'm trying to create a form that creates a new exercise with muscle_groups that are primary and secondary to the exercise.
Here's the relevant part of the schema:
create_table "exercised_muscle_groups", force: true do |t|
t.integer "muscle_group_id"
t.integer "exercise_id"
t.string "used_as"
end
add_index "exercised_muscle_groups", ["exercise_id"], name: "index_exercised_muscle_groups_on_exercise_id", using: :btree
add_index "exercised_muscle_groups", ["muscle_group_id"], name: "index_exercised_muscle_groups_on_muscle_group_id", using: :btree
create_table "exercises", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "muscle_groups", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
Here's what I have for the models:
class Exercise < ActiveRecord::Base
has_many :exercised_muscle_groups, dependent: :destroy
has_many :muscle_groups, through: :exercised_muscle_groups
accepts_nested_attributes_for :muscle_groups
accepts_nested_attributes_for :exercised_muscle_groups
end
class MuscleGroup < ActiveRecord::Base
has_many :exercised_muscle_groups
has_many :exercises, through: :exercised_muscle_groups
accepts_nested_attributes_for :exercises
accepts_nested_attributes_for :exercised_muscle_groups
end
class ExercisedMuscleGroup < ActiveRecord::Base
belongs_to :exercise
belongs_to :muscle_group
end
Here's what I have so far for the controller actions for new and create:
def new
#exercise = Exercise.new
#exercise.exercised_muscle_groups.build
end
def create
#exercise = Exercise.new(exercise_params)
#exercise.save
#then loop through all muscle groups marked as primary and then create a record for each one with value of "primary" for used_as column
#then loop through all muscle groups marked as secondary and then create a record for each one with value of "secondary" for used_as column
#if all saves are successful, then redirect to show page for that exercise
#if any save unsuccessful, then redirect to new page with error message
end
Here's what I have for the form in the view. I know it's wrong and it doesn't work.
<%=form_for(#exercise) do |exercise_form| %>
<%= exercise_form.label :name %>
<%= exercise_form.text_field :name, class: 'text_field' %><br>
<div class="col-xs-12">
<% exercise_form.fields_for :exercised_muscle_groups do |muscle_group_form| %>
<%= muscle_group_form.label :primary %>
</div>
<% MuscleGroup.all.each do |muscle_group| %>
<div class="col-xs-6">
<span class="col-xs-3">
<%= check_box_tag "exercise[muscle_group_ids][]", muscle_group.id %>
</span>
<span class="col-xs-3">
<%= muscle_group.name %>
</span>
</div>
<% end %>
<div class="col-xs-12">
<%= muscle_group_form.label :secondary %>
</div>
<% MuscleGroup.all.each do |muscle_group| %>
<div class="col-xs-6">
<span class="col-xs-3">
<%= check_box_tag "exercise[muscle_group_ids][]", muscle_group.id %>
</span>
<span class="col-xs-3">
<%= muscle_group.name %>
</span>
</div>
<% end %>
<% end %>
<%= exercise_form.submit "Submit", class: "btn btn-lg btn-primary" %>
<% end %>
How do I set up the form for the exercise and controller actions? Am I overcomplicating this? Is there an easier way?
Any help would be greatly appreciated. Thanks!

How to add text field in views to add content to other table inside a form

Im new to ror so sorry if that's a really basic question.
So, in my app i have quizzes and each quiz, beside questions and answers, should have 5 links.
class Quiz < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :questions
has_many :links
end
class Link < ActiveRecord::Base
belongs_to :quizzes
end
Schema looks like this:
create_table "links", force: true do |t|
t.string "name"
t.string "www"
t.integer "quiz_id"
end
create_table "questions", force: true do |t|
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "quiz_id"
end
create_table "quizzes", force: true do |t|
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
end
Now, in views I want to add links as i Add/Edit my quiz.
View for form looks like this:
<%= form_for(#quiz) do |f| %>
<% if #quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#quiz.errors.count, "error") %> prohibited this quiz from being saved:</h2>
<ul>
<% #quiz.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_field :content %>
<%= **THIS IS PLACE FOR TEXT FIELD FOR LINK_NAME AND LINK_WWW** %>
</div>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
How do I create a form to add the links?
You can use fields_for:
<%= f.label :content %><br>
<%= f.text_field :content %>
<%= f.fields_for #quiz.links.new do |ff| %>
<%= ff.label :name %>
<%= ff.text_field :name %>
<% end %>
And allow the Quiz to update the Link records associated:
class Quiz < ActiveRecord::Base
accepts_nested_attributes_for :links

Unpermitted parameters: user error when passing user params using nested attributes

I have a form that collects company information as well as the first user (the company admin). When I submit the form, the company attributes are saved to the db. However, the user attributes are not. I get the error Unpermitted parameters: user. I can't figure out why the user is not being created and saved.
I have:
class CompaniesController < ApplicationController
def new
#company = Company.new
#plans = Plan.all
end
def create
#company = Company.new(company_params)
#user = User.new
#user.role = "admin"
#user.save
if #company.save
redirect_to #company, notice: 'Company was successfully created.'
else
render action: 'new'
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def company_params
params.require(:company).permit(:name, :plan_id, users_attributes: [:id, :company_id, :email, :password, :password_confirmation, :first_name, :last_name, :role, :rate])
end
end
and
class UsersController < ApplicationController
# include UsersHelper
def index
#users = User.all
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
#user.save
flash.notice = "User '#{#user.first_name} #{#user.last_name}' was successfully created."
redirect_to user_path(#user)
end
def show
#user = User.find(params[:id])
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
#user.update(user_params)
flash.notice = "User '#{#user.first_name}' has been updated."
redirect_to user_path(#user)
end
def destroy
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :first_name, :last_name, :role, :rate)
end
end
and
class Company < ActiveRecord::Base
has_many :users
belongs_to :plan
accepts_nested_attributes_for :users, :allow_destroy => true
end
and
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates_confirmation_of :password, message: "should match confirmation", if: :password
has_many :jobs
belongs_to :company
end
and
<%= form_for(#company) do |f| %>
<% if #company.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#company.errors.count, "error") %> prohibited this company from being saved:</h2>
<ul>
<% #company.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name, :id => "name" %>
</div>
<div class="field">
<%= collection_select( :company, :plan_id, #plans, :id, :name ) %>
</div>
<%= f.fields_for :user do |user| %>
<div class="field">
<%= user.label :email %><br>
<%= user.text_field :email %>
</div>
<div class="field">
<%= user.label :password %><br>
<%= user.password_field :password %>
</div>
<div class="field">
<%= user.label :password_confirmation %><br>
<%= user.password_field :password_confirmation %>
</div>
<div class="field">
<%= user.label :first_name %><br>
<%= user.text_field :first_name %>
</div>
<div class="field">
<%= user.label :last_name %><br>
<%= user.text_field :last_name %>
</div>
<div class="field">
<%= user.label :role %><br>
<%= user.text_field :role %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and
ActiveRecord::Schema.define(version: 20140421235514) do
create_table "companies", force: true do |t|
t.string "name"
t.string "stripe_token"
t.integer "plan_id"
t.integer "user_id", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "companies", ["plan_id"], name: "index_companies_on_plan_id"
add_index "companies", ["user_id"], name: "index_companies_on_user_id"
create_table "plans", force: true do |t|
t.string "stripe_id"
t.string "name"
t.integer "amount"
t.string "interval"
t.string "currency"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "email", null: false
t.string "crypted_password", null: false
t.string "salt", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "reset_password_token"
t.datetime "reset_password_token_expires_at"
t.datetime "reset_password_email_sent_at"
t.string "first_name"
t.string "last_name"
t.string "role"
t.integer "rate"
t.integer "company_id"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token"
end
Company and User are associated with 1-M Relationship , i.e., Company has_many :users
In that case, in your view for Company, the nested form should be
<%= f.fields_for :users do |user| %> ## Notice users in plural
and NOT
<%= f.fields_for :user do |user| %>
Refer to the Nested Attributes Examples for One to Many
Currently, fields_for is setup incorrectly with singular :user so in params hash you got the key as :user and again a warning Unpermitted parameters: user because of which the user attributes were not stored in database.
Now, as you have setup accepts_nested_attributes_for in Company model. Controller is expecting user attributes in key users_attributes within params hash.
Changing the fields_for with plural :users argument would result in creation of users_attributes key in params hash upon form submission.
UPDATE
Company has many users, its 1-M relationship
Only users table should have foreign key as company_id.
You need to remove user_id from companies table.
Also, update the CompaniesController#new action as below:
def new
#company = Company.new
#users = #company.users.build
#plans = Plan.all
end
Strong params permitting looks fine to me but i think the issue is in the nested form, you used wrong relation name user while its users which generates a params hash titled with user which is not permitted, instead you should do:
<%= f.fields_for :users do |user| %>
#rest of the form elements
<% end %>

Resources