Rails Wicked Gem - Understanding routing

元气小坏坏 提交于 2019-12-12 02:07:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/26792258/rails-wicked-gem-understanding-routing

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