找不到没有ID错误消息的类别

时间:2013-08-07 11:49:57

标签: ruby-on-rails-3.2

我是Ruby on Rails的新手,我遇到了一些问题

我试图影响对象的cathogory id。

所以这是我的控制器动作:

  def create_card
    @card = Card.new(params[:card])

    if !params[:category].blank?
     @card.category_id = params[:category_id]
    end
    @category = Category.find(params[:id])
    @card.category_id = @category.id
    @card.save

    if @card.save
      flash[:notice] = "Card created"
      redirect_to :action => "card_information", :id => @card.id
    else
      render new_card_path
    end 
  end

这是我的观点代码:

= form_for @card, :url => create_card_path do |f|
  .row
    .span3
      = f.label "Card name"
      = f.text_field :title

  .row    
    .span3
      = f.label "Card Category"
      = select_tag :category_id, options_for_select(Category.all.collect{ |c| [c.title, c.id]})


  %br/
 .row
   .span3
     = f.submit "Submit", :class => "btn btn-primary"

1 个答案:

答案 0 :(得分:0)

category_id已作为params[:card][:category_id]传递到您的控制器,因此当您执行@card时,Card.new(params[:card])已被分配到def create_card @card = Card.new(params[:card]) if @card.save flash[:notice] = "Card created" redirect_to :action => "card_information", :id => @card.id else render new_card_path end end 。因此,您可以从搜索类别等的操作中删除任何代码:

{{1}}