Rails wicked gem redirect with params

2019-07-01 20:35发布

问题:

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])