Adding Categories to my Articles in my Rails app (Small Help)
Posted
by user214038
on Stack Overflow
See other posts from Stack Overflow
or by user214038
Published on 2010-03-25T20:51:56Z
Indexed on
2010/03/25
20:53 UTC
Read the original article
Hit count: 240
I'm currently building a semi-small ruby app for a project. The problem I'm currently facing is the following:
I want to be Able to fit the Article into the Categories. I've already accomplish this by having two models. An article model with a foreign key of category_id and my Category model with the name of the category. With a has_one and belogs_to relationship. (We're assuming you can only fit an article into one category). Here's the piece of code.
This is the new method, where i create a new article and load up all the categories.
def new
@article = Article.new
@categories = Category.find(:all)
end
The problem comes when i try to get the category from a combo box in order to insert it along with the article.
This is the combo box code :
f.select(:category_id,@categories) >
And this is the create method:
def create
@category = Category.find(params[:id])
@article = @category.articles.new(params[:article])
if @article.save
flash[:notice] = "Article Submitted Sucessfully"
redirect_to user_path
else
render :action => 'new'
end
end
I believe that the problem i have is in this line when i try to load up the selected category "@category = Category.find(params[:id])" because whenever i hardcode the this line into
@category = Category.find(1)
It Works perfectly
© Stack Overflow or respective owner