Rails wicked gem redirect with params

半腔热情 提交于 2019-12-10 18:27:02

问题


I am using the wicked gem to build a form wizard. In the documentation, it is using the current_user object but I want to use a different object. I am struggling with adding a parameter to redirect_to so I can use this object.

products_controller.rb

def create
  @product = current_user.product.build(params[:product])
  @product.ip_address = request.remote_ip

  if @product.save
    redirect_to product_steps_path # This is where I think I need to pass product object but not sure how
  else
    render 'new'
  end
end

product_steps_controller.rb

class ProductStepsController < ApplicationController
  include Wicked::Wizard
  steps :apps, :templates

  def show
    @product = Product.find(params[:id])
    render_wizard
  end
end

routes:

resources :products
resources :product_steps

The error I get with the above code is:

ActiveRecord::RecordNotFound in ProductStepsController#show

Couldn't find Product with id=apps

How do I use the wicked gem on a specific object like this instead of the current_user example in the documentation?


回答1:


This is what i got to work.

products_controller.rb

if @product.save
  redirect_to product_step_path(:id => "apps", : product_id => @ product.id)
else
...

product_steps_controller.rb

def show
  @asset = Asset.find(params[:asset_id])
  render_wizard
end



回答2:


The wizard gem takes :id parameter as step name. So you should not pass :id as the parameter since it will conflict with the step name.

products_controller.rb

redirect_to product_step_path(product_id: @product.id)

product_steps_controller.rb

@product = Product.find(params[:product_id])


来源:https://stackoverflow.com/questions/12862076/rails-wicked-gem-redirect-with-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!