开发者

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"=>"", 开发者_Go百科"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 %>......


Change your collection_select from

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

to

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜