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 8681999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:39:16+00:00 2026-06-12T21:39:16+00:00

I can’t get what I’m doing wrong. I’m following instuctions from revised railscast #102.

  • 0

I can’t get what I’m doing wrong.

I’m following instuctions from revised railscast #102.

I have model Article:

  belongs_to :category

   def category_name
      category.try(:name)
   end  

   def category_name=(name)
    self.category = Category.find_by_name(name)
   end

Category.rb:

  has_many :articles

Create_categories migration:

  def change
    create_table :categories do |t|
    t.string :name
    t.timestamps
   end
  end

Create_articles:

  def change
  create_table :articles do |t|
  t.string :name
  t.text :content
  t.integer :category_id
  t.timestamps
   end
 end

and code from _form:

   <div class="field">
    <%= f.label :category_name %><br />
   <%= f.text_field :category_name %>
  </div>

After submit I get this error:

       Can't mass-assign protected attribute :category_name

EDIT

     def create
@article = Article.new(params[:article])

respond_to do |format|
  if @article.save
    format.html { redirect_to @article, notice: 'Article was successfully created.' }

  else
    format.html { render action: "new" }
  end
end
end

My log:

     Started PUT "/articles/4" for 127.0.0.1 at 2012-10-16 00:59:44 +0300
   Processing by ArticlesController#update as HTML
   Parameters: {"utf8"=>"тЬУ", "authenticity_token"=>"YDPlS//tXg6Adl1npEEyNNBMZI0
   a7hW8bV5XFPmRre4=", "article"=>{"name"=>"112312", "category_name"=>"asdasd"}, "c
   ommit"=>"Update Article", "id"=>"4"}
    ←[1m←[35mArticle Load (0.0ms)←[0m  SELECT "articles".* FROM "articles" WHERE "
   articles"."id" = ? LIMIT 1  [["id", "4"]]
   ←[1m←[36m (0.0ms)←[0m  ←[1mbegin transaction←[0m
   ←[1m←[35mCategory Load (0.0ms)←[0m  SELECT "categories".* FROM "categories" 
 WHERE "categories"."name" = 'asdasd' LIMIT 1
  ←[1m←[36m (0.0ms)←[0m  ←[1mcommit transaction←[0m

  Redirected to http://127.0.0.1:3000/articles/4
  Completed 302 Found in 16ms (ActiveRecord: 0.0ms)

So it seems to be saving, but I don’t see it in my view:

 <p>
  <b>Category:</b>
  <%= @article.category %>
 </p>

Gives me blank space.

  • 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-06-12T21:39:17+00:00Added an answer on June 12, 2026 at 9:39 pm

    For this line to work <%= f.text_field :category_name %>, add this line to Article model

    attr_accessible :category_name
    

    If you tried that already, make sure you restart the web server after you call your migrations.

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

Sidebar

Related Questions

Can someone please explain to me why the output from the following code is
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
I have a jquery bug and I've been looking for hours now, I can't
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can't figure out how to do this in a pretty way : I have
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can anyone give me an example of how to return the following json simply

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.