简体   繁体   中英

Rails wicked gem redirect with params

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM