# How do I update a foreign key via Views (UI)?

**URL:** <https://www.ruby-forum.com/t/how-do-i-update-a-foreign-key-via-views-ui/79704>\
**Category:** Rails\
**Created:** [December 18, 2006, 11:44am UTC](https://www.ruby-forum.com/t/how-do-i-update-a-foreign-key-via-views-ui/79704 "2006-12-18T11:44:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@tuka](https://www.ruby-forum.com/u/tuka)\
**Post date:** [December 18, 2006, 11:44am UTC](https://www.ruby-forum.com/t/how-do-i-update-a-foreign-key-via-views-ui/79704/1 "2006-12-18T11:44:18Z")

</div>

Hi all,

I am having an issue here that i seem not to wrap my head around… I am  
making a simple online catalog…

I have 2 models Category and LineItems  
where Category has many LineItems

So in the LineItem Model I say:

class LineItem \< ActiveRecord::Base  
belongs\_to :category

Now in my edit form (View) for LineItem called edit.rhtml, with a drop  
down list, I wish to select the category and update it in the database.

For that I use the following before the scaffolding code in the (this is  
a simple test app I am doing here…).

\<%=  
@categories = Category.find(:all, :order =\> “name”).map {|c| [c.name,  
c.id]}  
select(:category, :id, @categories)  
%\>

Items come up ok but I cannot seem to update. I believe the select  
parameters are ok but could the problem be there or am I missing  
something ?  
Note that when I use the Edit view, I wish to read the existing  
category\_id and have it pre-selected for me if it already exists.

TIA,  
Tuka

---

<div class="post-metadata">

**Author:** ![tuka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@tuka](https://www.ruby-forum.com/u/tuka)\
**Post date:** [December 18, 2006, 12:29pm UTC](https://www.ruby-forum.com/t/how-do-i-update-a-foreign-key-via-views-ui/79704/2 "2006-12-18T12:29:35Z")

</div>

Please post your corresponding controller code.

- Roderick

---

<div class="post-metadata">

**Author:** ![tuka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@tuka](https://www.ruby-forum.com/u/tuka)\
**Post date:** [December 18, 2006, 1:11pm UTC](https://www.ruby-forum.com/t/how-do-i-update-a-foreign-key-via-views-ui/79704/3 "2006-12-18T13:11:59Z")

</div>

Your controller code seems in order and I just noticed that it’s your  
select() statement that needs adjustment. Try:

select(:line\_item, :category\_id, @categories)

- Roderick

---

<div class="post-metadata">

**Author:** ![tuka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@tuka](https://www.ruby-forum.com/u/tuka)\
**Post date:** [December 18, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/how-do-i-update-a-foreign-key-via-views-ui/79704/4 "2006-12-18T12:41:03Z")

</div>

Here goes… the full listing so that we miss nothing:

class ItemController \< ApplicationController  
layout ‘home’

def index  
list  
render :action =\> ‘list’  
end

# GETs should be safe (see

[URIs, Addressability, and the use of HTTP GET and POST](http://www.w3.org/2001/tag/doc/whenToUseGet.html))  
verify :method =\> :post, :only =\> [:destroy, :create, :update],  
:redirect\_to =\> { :action =\> :list }

def list  
@line\_item\_pages, @line\_items = paginate :line\_items, :conditions =\>  
["category\_id = ? ",params[:id]],:per\_page =\> 10  
end

def admin\_list  
@line\_item\_pages, @line\_items = paginate :line\_items, :conditions =\>  
["category\_id = ? ",params[:id]],:per\_page =\> 10  
end

def show  
@line\_item = LineItem.find(params[:id])  
end

def new  
@line\_item = LineItem.new  
end

def create  
@line\_item = LineItem.new(params[:line\_item])  
if @line\_item.save  
flash[:notice] = ‘LineItem was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def edit  
@line\_item = LineItem.find(params[:id])  
end

def update  
@line\_item = LineItem.find(params[:id])  
if @line\_item.update\_attributes(params[:line\_item])  
flash[:notice] = ‘LineItem was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @line\_item  
else  
render :action =\> ‘edit’  
end  
end

def destroy  
LineItem.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end  
end

---

<div class="post-metadata">

**Author:** ![tuka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@tuka](https://www.ruby-forum.com/u/tuka)\
**Post date:** [December 18, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/how-do-i-update-a-foreign-key-via-views-ui/79704/5 "2006-12-18T15:13:44Z")

</div>

Roderick, Hurray !!

Not only your indication was correct, it helped me find another error,  
that the select code was not within the ROR start\_form\_tag and thus

 in the resulting page - thats is why it was not writing/reading to the DB.

Thank a ton for your time and pointing me in the right direction !!

Cheers,  
Tuka
