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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:38:46+00:00 2026-05-27T10:38:46+00:00

Alright so I’m pretty new to this rails stuff so please bear with me…

  • 0

Alright so I’m pretty new to this rails stuff so please bear with me…

I’m trying to make the most simple application ever, a Christmas list, and I need a little bit of help. Let me fill you in:

I scaffolded a person and an item. I modified my models a little bit and here is what they look like.

class Person < ActiveRecord::Base
  has_many :item, :dependent => :destroy
end

class Item < ActiveRecord::Base
  belongs_to :person
end

class CreateItems < ActiveRecord::Migration
  def change
    create_table :items do |t|
      t.integer :person_id
      t.string :description

      t.timestamps
    end
  end
end

class CreatePeople < ActiveRecord::Migration
  def change
    create_table :people do |t|
      t.string :name

      t.timestamps
    end
  end
end

Seems like that’s all cool. The index action on the people_controller lists all the people(duh)

<% @people.each do |person| %>
  <tr>
    <td><%= link_to person.name, "/people/#{person.id}" %></td>
  </tr>
<% end %>

and when you click on one, it calls the show action(same controller) which gets all of the items for that person

def show
  @person = Person.find(params[:id])
  @items = Item.where(:person_id => params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @person }
  end
end

and pulls the show view

<table>
<% @items.each do |item| %>
  <tr>
    <td><%= item.description %></td>
    <td><%= link_to 'Edit', edit_item_path %></td>
    <td><%= link_to 'Remove', "" %></td>
  </tr>
<% end %>
</table>
<br/>
<%= link_to 'Add', :controller => :items, :action => :new, :id => @person.id %>

The link at the bottom is to add a new item for the person who’s summary we are viewing. So then in the new action on the items_controller I have:

def new
  @item = Item.new
  @item.person_id = params[:id]

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @item }
  end
end

Now I know this doesn’t get saved until @item.save is called. I imagine this happens from the _form.html.erb submit button which in turn calls the create action in the controller?

def create
  @item = Item.new(params[:item])

  respond_to do |format|
    if @item.save
      format.html { redirect_to @item, notice: 'Item was successfully created.' }
      format.json { render json: @item, status: :created, location: @item }
    else
      format.html { render action: "new" }
      format.json { render json: @item.errors, status: :unprocessable_entity }
    end
  end
end

I’m just a little confused why this is never getting set, it seems like it should be so easy(I’m sure it is haha). Also while I’m at it, you may have noticed I have no link for my ‘Remove’ link above. This is because I also couldn’t figure out how to destroy action from that link to remove the correct item.

Like I said, this is all new to me. I appreciate any help! Please feel free to critique EVERYTHING I have done here. I don’t have feelings 🙂

  • 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-27T10:38:47+00:00Added an answer on May 27, 2026 at 10:38 am

    As Vibhu said, it’s quite likely your issue stem from the fact that you should have has_many :items (note the plural) in your Person controller.

    To add a hidden filed in your form specifying the person’s id, add this in your creation form:

    f.hidden_field :person_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright I'm pretty new to programming and stuff and I'm now trying to code
Alright, so I've been trying to implement a simple binary search tree that uses
Alright, I'm extremely new to programming so odds are this is a really easy
Alright, I'm stupid new at this. Can anyone point me in the direction of
Alright, I am trying to accomplish this: When a user clicks a button that
Alright guys, this should be a very simple request. I'd like to run a
Alright, I'm trying to figure out why I can't understand how to do this
Alright so I have a simple has_many :through relationship in Rails 3.1 class Event
Alright, it has been a long couple of days trying to figure this out
Alright, so here is what I'm hoping for... Any request to this http://www.fileorchard.com/3451928347592 will

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.