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

  • Home
  • SEARCH
  • 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 7584669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:58:41+00:00 2026-05-30T18:58:41+00:00

I have three controllers – users, stories, categories. I want when I am logged

  • 0

I have three controllers – users, stories, categories. I want when I am logged in as a administrator to create categories and then to write stories for every category. I did part of the task but in DB in table Stories category_id is empty and I cannot understand how to fix it. Here is part of my code:

stories/new.html.erb:

<%= form_for(@story) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :category %><br />
    <%= f.collection_select :category_id, @categories, :id, :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Add" %>
  </div>
<% end %>

stories_controller.rb:

class StoriesController < ApplicationController
  before_filter :admin_user

  def new
    @story = Story.new
    @categories = Category.all
    @title = "Add news"
  end

  def create
    @categories = Category.all
    @story = current_user.stories.build(params[:story])
    if @story.save
      flash[:success] = "Successfullly added news"
      redirect_to @story
    else
      @title = "Add news"
      render 'new'
    end
  end

  private

  def admin_user
    redirect_to(root_path) unless current_user.admin?
  end
end

story.rb:

class Story < ActiveRecord::Base
  attr_accessible :title, :content

  belongs_to :user
  belongs_to :category

  validates :title, :presence => true
  validates :content, :presence => true
  validates :user_id, :presence => true

  default_scope :order => 'stories.created_at DESC'
end

category.rb:

class Category < ActiveRecord::Base
  attr_accessible :title

  has_many :stories

  validates :title, :presence     => true,
            :length       => { :within => 6..40 }
end

user.rb:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :stories

  validates :name,  :presence => true,
            :length   => { :maximum => 50 }
  validates :email, :presence => true

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, :presence   => true,
                :format     => { :with => email_regex },
                :uniqueness => true

  validates :password, :presence     => true,
                   :confirmation => true,
                   :length       => { :within => 6..40 }

  before_save :encrypt_password

  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end

  def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil  if user.nil?
    return user if user.has_password?(submitted_password)
  end

  def self.authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt) ? user : nil
  end

  private

  def encrypt_password
    self.salt = make_salt unless has_password?(password)
    self.encrypted_password = encrypt(password)
  end

  def encrypt(string)
    secure_hash("#{salt}--#{string}")
  end

  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end

  def secure_hash(string)
    Digest::SHA2.hexdigest(string)
  end
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-30T18:58:43+00:00Added an answer on May 30, 2026 at 6:58 pm

    You need add attr_accessible :category_id to your story model

    Its preventing mass assignment in your controllers create method. Alternatively you could pull out the category_id from your params hash and assign it on a separate line.

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

Sidebar

Related Questions

ANSWERED! IT WAS A TYPO OMG. Always check EVERY CHARACTER I have three controllers,
I have three view controllers. I used the flipsideproject template and then added another
I wanna create an app that will have three view controllers. To navigate between
I have an navigation-based app with three view controllers. The first has categories of
So have a stack with three view controllers where A is root, B is
I have three controllers with three actions: List View Info The only difference between
In my app, I have [for example] three controllers: groups , forums , and
E.g. I have three payment controllers, each specific to a third party payment processor,
I have three view controllers, one root controller, one login view controller and one
I have three view controllers: A -> B -> C managed by a navigation

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.