views:

127

answers:

2

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

+2  A: 

The problem lies with f.select. The second parameter doesn't set the value of the list of options to the id of the category. The form builder's select methods are kinda lacking. I would do this instead:

<%= collection_select :article, :category_id, @categories, :id, :name, @article.category_id %>

and that should get the right value to the controller.

Then in the controller, you can replace:

@category = Category.find(params[:id])  
@article = @category.articles.new(params[:article])  

with

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

The category_id will come from the params and be set correctly.

Tilendor
For some reason i keep getting "undefined method `stringify_keys' "On the select line. :/
Gotjosh
Sorry, was going from memory and used the wrong functions. I corrected my answer based off the actual documentation.
Tilendor
That did worked! :] You're answer is a lot better than mine. :)Just replaced the select with <%= collection_select :article, :category_id, @categories, :id, :name %>and it works perfectly no need for that object at the end
Gotjosh
A: 

Your answer didn't work for me some reason. I think

select_tag

option won't admit the 2 symbols you're trying to pass, instead this is what worked for me.

<%= select_tag :category_id, options_from_collection_for_select(@categories, :id, :name) %>

and in the controller

@category = Category.find(params[:category_id])
@article = @category.articles.new(params[:article])

Because it wouldn't find the category_id with just

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

I'm curious that it might be, because I'm using

<% form_for @article do |f| -%> ?

Thanks for the help.

Gotjosh