我是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"
答案 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}}