Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6792957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:02:28+00:00 2026-05-26T18:02:28+00:00

This happen in my app/controllers/categories_controller.rb:3:in `create’ I plan to develop a blog and each

  • 0

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 %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:02:28+00:00Added an answer on May 26, 2026 at 6:02 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

why this is happen ? When u create abstract class in c++ Ex: Class
Can anybody please explain how this could possibly happen? I am completely aware of
I have an navigation-based app with three view controllers. The first has categories of
This happens after I hit Build and Go to run my app on my
i don't understand how this happen. This is portion of my code.. int isGoal(Node
(int)(33.46639 * 1000000) returns 33466389 Why does this happen?
If I use mod_rewrite to control all my 301 redirects, does this happen before
This doesn't happen all the time. A bug is not a bug if cannot
(I foresaw this problem might happen 3 months ago, and was told to be
I'm surprised this doesn't happen automatically, but I would like my applications window to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.