views:

65

answers:

2

I have a small problem and it is really bugging me.

I have all the standard scaffold code in the controllers to give me the standard CRUD features.

The collection_select form helper is in my view:

    <%= collection_select(:link,:category_id,@categories,:id,"name") %>

The Link Table has a category_id column. This is being posted ok, as while debugging it gives:` ... "link"=>{"name"=>"", "category_id"=>"1", ...

However it is not being submitted to the database and any validation of the category_id fails.

Controller Methods:

 def new
    @link = Link.new
    @categories = Category.find(:all)
  end

  def create
    @link = Link.new(params[:link])
    if @link.save
      flash[:notice] = "Successfully created link."
      redirect_to @link
    else
      render :action => 'new'
    end
  end

Form from View

<% form_for @link do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name %>......
A: 

Change your collection_select from

 <%= collection_select(:link,:category_id,@categories,:id,"name") %>

to

 <%= f.collection_select(:category_id,@categories,:id,"name") %>
nas
Still doesn't work, nothing gets entered into the database and the validates_presence_of still fails.
vectran
A: 

Finally solved it, I checked the logs and it had this error:

WARNING: Can't mass-assign these protected attributes: category_id

I added the 'category_id' to the attr_accesible' in my model and it works fine.

vectran