vectran
vectran

Reputation: 1979

collection_select and not being submitted database

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 %>......

Upvotes: 1

Views: 368

Answers (2)

vectran
vectran

Reputation: 1979

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.

Upvotes: 2

nas
nas

Reputation: 3696

Change your collection_select from

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

to

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

Upvotes: 2

Related Questions