简体   繁体   中英

Rails Wicked Gem - Understanding routing

Okay, so I'm not really understanding nested routing in the wicked gem.

So far I have this. I'm not sure if everything is in the right folder or if I'm doing that right.

routes.rb

resources :events   
resources :events do
  resources :build, controller: 'events/build' 
end

controllers/events_controller.rb

 def create
        @event = Event.new(event_params)
        if @event.save
            flash[:success] = "Event Created!"
            redirect_to event_build_path(event_id: "event", id: @event.id) 
            # previously had redirect_to event_build_path without parameters)
        else
            render 'new'
        end
    end

controllers/events/build_controller.rb

class Events::BuildController < ApplicationController
    include Wicked::Wizard

    steps :details, :visibility

    def show
        @event = Event.find(params[:event_id])
        render_wizard
    end
end

views/build/details.html.erb

<%= form_for @event do |f| %>
#blab blah 
<% end %>

I had the event_build_path without parameters at first and I had this error No route matches {:action=>"show", :controller=>"events/build"} missing required keys: [:event_id, :id]

Had influence from this Rails wicked gem redirect with params but don't entirely understand the routing

I don't have an event_id set and I don't really understand how wicked keeps track of the step of via the id (or if its event_id).

As my object (event) is not created yet, what is "event_id" and the id at the end represent?

Not really an answer, but some clarifications. The thing you're trying to do is pretty hard, and requires a bunch of customizations to suit your own case. If you're not comfortable with wicked, or if that tutorial is nearly incomprehensible, it might be better to skip doing a wizard for now and come back and try it again in a month or so once you've had time to meditate on it.

Form

This is your wicked form

<%= form_for @event do |f| %>
#blab blah 
<% end %>

Wicked works by doing two things, storing state in your url domain.com/build_pah/<step> and providing you with helper methods to easily manipulate the current state. Once you render the form you need to tell the browser where to submit info to when enter is pressed. Right now it is going to @event path, which isn't what we want. Instead we need to do something like:

<%= form_for @event, :url => wizard_path, :method => :put do |f| %>

<% end %>

This tells the form to go to the wizard_path url, this is a helper we provide. It also tells the form to submit using the PUT HTTP method, which should trigger your def update action inside of your Events::BuildController if it is set up correctly. On another note it doesn't look like Events::BuildController has an update action.

Event Controller

Your event controller looks fine, however you're redirecting

redirect_to event_build_path(event_id: "event", id: @event.id) 

Wicked needs the id parameter to be the step you want to go to. So it should be:

redirect_to event_build_path(event_id: @event.id, id: :details) 

or

redirect_to event_build_path(event_id: @event.id, id: Wicked::FIRST_STEP)

You can also get fancy and redirect to the index action which will do another redirect to the first step, but i always prefer being explicit.

Other Questions

Here is someone with a similar question: https://github.com/schneems/wicked/issues/141 take a look at their code, and their question. Try to understand what was wrong and how it was fixed. Then compare between what they're trying to do and what you're trying to do.

This question

It's hard to be more helpful without an explicit question. Breaking it down into I did this => I expected this => I got this instead , I tried to debug using this . Anywhoo, hope some of this was helpful. Maybe spin up another Rails example app and try to walk through my wicked tutorial in the readme, it will give you some more experience with what wicked does (and doesn't) do for you.

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