Error undefined method `categories' for #<User:0x00000103047730> - ruby-on-rails

This happen in my app/controllers/categories_controller.rb:3:in `create'
I plan to develop a blog and each user can create fews categories for their micropost. Therefore each micropost can only has one category. I have 3 table :user, microposts and category. My intention is to have a user to add category at user profile page.
model/category.rb
class Category < ActiveRecord::Base
belongs_to :user
attr_accessible :category
end
model/user.rb
has_many :category, :dependent => :destroy
accepts_nested_attributes_for :category, :reject_if =>lambda {|a| a[:category].blank?}
categoriesController
class CategoriesController < ApplicationController
def create
#category = current_user.categories.new(params[:category])
if #category.save
flash[:success] = "Category created!"
redirect_to #user
else
flash[:error] = "Category not created."
render #user
end
end
end
usersController
def show
#user = User.find(params[:id])
#title = #user.name
#category = #user.category.new
end
user show.html
<%= form_for #category do |f|%>
<%= hidden_field_tag :user_id, #user.id %>
<%= f.label :category ,"Category:"%>
<%=h f.text_field :category %><br />
<%= f.submit "Add Category" %>
<% end %>

In User model you have relation
has_many :category
But in controller get from user categor ies
current_user.categories
Rename relation name to has_many :categories.

Related

Adding a student to a team when creating a team in a has_and_belongs_to_many association

I have two models (teams and students) and when creating a team I want to be able to add a student to the team using their email. I can do this in the rails console by doing team.students << student but I am unsure how to translate that functionality in the controller and view.
Team controller:
def new
#team = Team.new
end
def add_student
#team = Team.find(params[:id])
#team.students << Student.find(params[:student_email])
end
def create
#team = Team.new(team_params)
if #team.save
redirect_to teams_path
else
render 'new'
end
end
private
def team_params
params.require(:team).permit(:name, student_attributes:[])
end
def current_team
team = Team.find(params[:id])
end
end
Team view:
<%= form_with(model: #team, local: true) do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= fields_for :student do |s|%>
<%= s.label :email%>
<%= s.text_field :email, class: 'form-control' %>
<% end %>
<%= f.submit "Create Team", class: "btn btn-primary" %>
<% end %>
Thank you for your help
You can do a lot better then just using HABTM:
class Team < ApplicationRecord
has_many :memberships
has_many :students, through: :memberships
end
class Student < ApplicationRecord
has_many :memberships
has_many :teams, through: :memberships
end
# rails g model team student:belongs team:belongs_to
class Membership < ApplicationRecord
belongs_to :student
belongs_to :team
validates_uniqueness_of :student_id, scope: :team_id
end
This also creates a many to many assocation but it gives you an actual model so you can access additional columns on the table (like for example if you want to add roles or keep track of who added the student to the team) and its actually a real entity in your buisness logic instead of just a peice of plumbing.
HABTM is actually quite useless.
To add/remove members from a team you create and destroy memberships.
resources :teams do
resources :memberships,
only: [:new, :create, :destroy]
shallow: true
end
class MembershipsController < ApplicationController
before_action :set_team, only: [:new, :index, :create]
# GET /teams/1/memberships/new
def new
#students = Student.where.not(id: #team.students)
#membership = #team.memberships.new
end
# POST /teams/1/memberships
def create
#membership = #team.memberships.new(membership_params)
if #membership.save
redirect_to #team, notice: "Student added to team"
else
#students = Student.where.not(id: #team.students)
render :new
end
end
# DELETE /memberships/1
def destroy
#membership.destroy
redirect_to #membership.team, notice: "Student removed from team"
end
private
def set_team
#team = Team.find(params[:team_id])
end
def set_membership
#membership = Membership.find(params[:id])
end
def membership_params
params.require(:membership)
.permit(:student_id)
end
end
<%= form_with(model: #membership, local: true) do |f| %>
<div class="field">
<%= f.label :student_id %>
<%= f.collection_select :student_ids, #students, :id, :email %>
</div>
<%= f.submit %>
<% end %>
As a rule of thumb if you're creating a method on your controller thats not one of the standard CRUD methods and it contains a synonym to of one of them (add, remove, etc) you're almost certainly doing it wrong and should treat it as separate RESTful resource.

Create action by a has many through association to assign a favorite_category

Problem
I'm trying to create a middle table called category_profiles, is a intermediate table to assign favorite categories to my profiles, but I can't access to the category_ids, that I put in my form, always I got the same validation, Category doesn't exist:
Code:
class CategoryProfile < ApplicationRecord
belongs_to :profile
belongs_to :category
end
class Category < ApplicationRecord
has_many :category_profiles
has_many :profiles, through: :category_profiles
class Profile < ApplicationRecord
has_many :category_profiles
has_many :categories, through: :category_profiles
When I'm doing the create action, my controller can't find my category. How do I fix it?
My create action never find the ids of my categories to assign to the category_profiles. It has many through relation:
Module Account
class FavoritesController < Account::ApplicationController
before_action :set_category_profile
def index
#favorites = #profile.categories
end
def new
#categories = Category.all
#category_profile = CategoryProfile.new
end
def create
#category_profile = #profile.category_profiles.new(category_profile_params)
if #category_profile.save
flash[:success] = t('controller.create.success',
resource: CategoryProfile.model_name.human)
redirect_to account_favorites_url
else
flash[:warning] = #category_profile.errors.full_messages.to_sentence
redirect_to account_favorites_url
end
end
def destroy
end
private
def set_category_profile
#category_profile = CategoryProfile.find_by(params[:id])
end
def category_profile_params
params.permit(:profile_id,
category_ids:[])
end
end
end
Form
<%= bootstrap_form_with(model: #category,method: :post , local: true, html: { novalidate: true, class: 'needs-validation' }) do |f| %>
<div class="form-group">
<%= collection_check_boxes(:category_ids, :id, Category.all.kept.children.order(name: :asc), :id, :name, {}, { :multiple => true} ) do |b| %>
<%= b.label class: 'w-1/6 mr-4' %>
<%= b.check_box class: 'w-1/7 mr-4' %>
<%end %>
</div>
<div class="md:flex justify-center">
<%= f.submit 'Guardar categoría favorita', class: 'btn btn-primary' %>
</div>
<% end %>
Seems like you just want to update intermediate table. So you can do it like this.
def create
begin
#profile.categories << Category.find(params[:category_ids])
Or
params[:category_ids].each do |category_id|
#profile.category_profiles.create(category_id: category_id)
end
flash[:success] = t('controller.create.success',
resource: CategoryProfile.model_name.human)
redirect_to account_favorites_url
rescue
flash[:warning] = #category_profile.errors.full_messages.to_sentence
redirect_to account_favorites_url
end
end
Need to find other better way for error handling using either transaction block or something.

Rails nested fields creation in loop

I have three model classes related to each other.
class Student < ActiveRecord::Base
has_many :marks
belongs_to :group
accepts_nested_attributes_for :marks,
reject_if: proc { |attributes| attributes['rate'].blank?},
allow_destroy: true
end
This class describes a student that has many marks and I want to create a Student record along with his marks.
class Mark < ActiveRecord::Base
belongs_to :student, dependent: :destroy
belongs_to :subject
end
Marks are related both to the Subject and a Student.
class Subject < ActiveRecord::Base
belongs_to :group
has_many :marks
end
When I try to create the nested fields of marks in loop labeling them with subject names and passing into in it's subject_id via a loop a problem comes up - only the last nested field of marks is saved correctly, whilst other fields are ignored. Here's my form view code:
<%= form_for([#group, #student]) do |f| %>
<%= f.text_field :student_name %>
<%=f.label 'Student`s name'%><br>
<%= f.text_field :student_surname %>
<%=f.label 'Student`s surname'%><br>
<%=f.check_box :is_payer%>
<%=f.label 'Payer'%>
<%= f.fields_for :marks, #student.marks do |ff|%>
<%#group.subjects.each do |subject| %><br>
<%=ff.label subject.subject_full_name%><br>
<%=ff.text_field :rate %>
<%=ff.hidden_field :subject_id, :value => subject.id%><br>
<%end%>
<% end %>
<%= f.submit 'Add student'%>
<% end %>
Here`s my controller code:
class StudentsController<ApplicationController
before_action :authenticate_admin!
def new
#student = Student.new
#student.marks.build
#group = Group.find(params[:group_id])
#group.student_sort
end
def create
#group = Group.find(params[:group_id])
#student = #group.students.new(student_params)
if #student.save
redirect_to new_group_student_path
flash[:notice] = 'Студента успішно додано!'
else
redirect_to new_group_student_path
flash[:alert] = 'При створенні були деякі помилки!'
end
end
private
def student_params
params.require(:student).permit(:student_name, :student_surname, :is_payer, marks_attributes: [:id, :rate, :subject_id, :_destroy])
end
end
How can I fix it?
#student.marks.build
This line will reserve an object Mark.
If you want multi marks, May be you need something like this in new action :
#group.subjects.each do |subject|
#student.marks.build(:subject=> subject)
end
Hope useful for you.

Simple_form: simple_fields_for not showing the form

I'm trying to create two model in just one form. I have user and unit model both are associated with has_many :through association.
user.rb
class User < ActiveRecord::Base
has_many :units, :through => :user_unit_assocs
has_many :user_unit_assocs
end
unit.rb
class Unit < ActiveRecord::Base
has_many :users, :through => :user_unit_assocs
has_many :user_unit_assocs
end
users_controller.rb
class UsersController < ApplicationController
def create
#user = User.new(user_params)
#user.units.build
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
else
format.html { render :new }
end
end
end
private
def user_params
params.require(:user).permit(:username, :email, units_attributes:[:unitname])
end
end
This is my form, users new
<%= simple_form_for(#user) do |f| %>
<%= f.simple_fields_for :units do |unit| %>
<%= unit.input :unitname, required: true %>
<% end %>
<%= f.input :name, required: true %>
<%= f.input :email, required: true %>
<%= f.button :submit %>
<% end %>
When I run the code, the form only showing a input-field for :name and :email and there is no input-field for units:[:unitname]. How can I show the input-field in the form? Thanks in advance.
References:
1. Rails 4: accepts_nested_attributes_for and mass assignment
2. https://github.com/plataformatec/simple_form/wiki/Nested-Models
Add
def new
#user = User.new
#user.units.build
end
on your controller
You need to build the association in the new action, not the create action.

Building a nested form with a many-to-many relationship in Rails 4

I have a model (User) that has_many of another model (Profession) - and this is supposed to be represented by one (or multiple) select menu in a form.
I cannot get my head around why the select menu doesn't get rendered? Am I constructing the select helper in the wrong way? Or is something else wrong in the view or the controller? The name attribute of the User is showing up alright in the form.
The models:
class User < ActiveRecord::Base
has_many :occupations, dependent: :destroy
has_many :professions, through: :occupations
accepts_nested_attributes_for :occupations
end
class Profession < ActiveRecord::Base
has_many :occupations, dependent: :destroy
has_many :users, through: :occupations
end
class Occupation < ActiveRecord::Base
belongs_to :user
belongs_to :profession
end
The controller:
def edit
end
def create
#user = User.new(user_params)
if #user.save
redirect_to #user, notice: 'User was successfully created.'
else
render action: 'new'
end
end
private
def set_user
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :email, ocuppations_attributes: [:id, :user_id, :profession_id])
end
The view (compressed):
<%= form_for(#user) do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :occupations do |builder| %>
<%= builder.select :profession_id, Profession.all.collect {|x| [x.title, x.id]} %>
<% end %>
<% end %>
Shouldn't that be a collection select?
<%= builder.collection_select(:profession_id, Profession.all, :id, :title) %>

Resources