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
Usermodel you have relationBut in controller get from user categor ies
Rename relation name to
has_many :categories.