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 🙂
As Vibhu said, it’s quite likely your issue stem from the fact that you should have
has_many :items(note the plural) in yourPersoncontroller.To add a hidden filed in your form specifying the person’s id, add this in your creation form: