基于rails 4的条件动态更新强参数

时间:2014-09-27 05:05:42

标签: mysql ruby-on-rails ruby ruby-on-rails-4 paperclip

我有3个型号

User : id,email

Presentation : id,title,description,avatar_filename,avatar_content_type,avatar_file_size

Webnair : id,user_id,presentation_id,choice,title,description,pptfilename,pptcontent_type,pptfile_size

选择是布尔在Webnair模型中,我假设1-newpresentation,0-select from db

在webnaircontroller.rb中我想在creat方法中检查条件:

 def create
   @webnair = Webnair.new(webnairs_params)
     if @webnair.save!
       flash[:notice] = "Webnair created successfully!"
    else
        flash[:error] = "Webnair not created!"
      end
    redirect_to root_path
end
private 

  def webnairs_params
  if params[:choice].to_s == "1"
   webnairs_params = params.require(:webnair).permit(:user_id, :ppt, :choice,     :title,:description)     
else
webnairs_params = params.require(:webnair).permit(:user_id, :presentation_id, choice,:title, :description) 
end
end

0 个答案:

没有答案