从另一个控制器渲染视图与params

时间:2015-06-17 10:49:12

标签: ruby-on-rails controllers nested-form-for

我的 products_controller.rb

中有代码
        def create
          @product = Product.new(product_params)
          brand = Brand.find(params[:brand_id])
          @product.brand = brand

          if @product.save
            raise "OK"
          else
            @brand = Brand.find(params[:brand_id])
            render 'brands/show'
          end
        end

保存方法失败后,我需要重定向到品牌 show.html.erb 视图,该视图按ID查找品牌并显示相关信息。

问题:

  1. 这是否正确?
  2. 如果用户手动传递了server_id参数,如何阻止它执行(SQL注入)?或者我应该如何检查并显示错误?
  3. 如何在表单外的brands/show视图中显示错误(因为我在<a data-form="...">属性中存储表单,因此用户在点击链接之前无法看到表单。更多,我使用form_for([@brand,@brand.send(:products).klass.new])

1 个答案:

答案 0 :(得分:1)

使用redirect_to brands_path(@brand)

相关问题