Ruby on rails 我收到一个错误,“没有路由匹配”{…id=>nil}**

Ruby on rails 我收到一个错误,“没有路由匹配”{…id=>nil}**,ruby-on-rails,controller,routes,wicked-gem,Ruby On Rails,Controller,Routes,Wicked Gem,我见过很多类似的路由问题和id=>nil帖子,但没有一个解决方案解决了我的错误 首先是整个错误: ProfileStepPersonal中的ActionController::UrlGenerationError 显示第1行出现的…profile_steps/personal.html.erb: 没有路由匹配{:action=>show,:controller=>profile\u步骤,:id=>nil 我正在使用Wicked创建一个多步骤表单,似乎我没有正确获取:id 以下是“注册”并创建概要

我见过很多类似的路由问题和id=>nil帖子,但没有一个解决方案解决了我的错误

首先是整个错误:

ProfileStepPersonal中的ActionController::UrlGenerationError 显示第1行出现的…profile_steps/personal.html.erb: 没有路由匹配{:action=>show,:controller=>profile\u步骤,:id=>nil

我正在使用Wicked创建一个多步骤表单,似乎我没有正确获取:id

以下是“注册”并创建概要文件的第一步之后的profiles_controller.rb

  def create
    @profile = Profile.new(profile_params[:profile])
    if @profile.save
      session[:profile_id] = @profile.id
      redirect_to profile_steps_path
    else
      render :new
    end
  end
这是profile_steps_controller.rb,它是重定向到的表单的下一步

class ProfileStepsController < ApplicationController
include Wicked::Wizard
    steps :personal




def show
    @profile = Profile.new(params[:profile])
      session[:profile_id] = @profile.id
  render_wizard
  end

   def update
    @profile = Profile.new(profile_params)
    @profile.attributes = (profile_params)
    render_wizard @profile
  end

   private 
  def profile_params
    params.require(:profile).permit(:description, :name, :website)
      end

      def redirect_to_finish_wizard
        redirect_to root_url, notice: "Thank you for signing up."
      end
    end
如果在以前的帖子中已经提到这一点,请提前感谢,并表示歉意

更新: 这是多步骤表单的第一页: new.html.erb


我在向导第一页的视图中遇到了这个问题,我使用助手方法解决了这个问题:

  def safe_previous_wizard_path(id)
    id ? previous_wizard_path : root_path
  end
甚至:

  <% if applicant.persisted? %>
    <a href="<%= previous_wizard_path %>">Back</a>
  <% end %>

看起来您正在设置会话id,然后重定向到步骤。在show中的步骤控制器中之后,您正在创建一个新的配置文件并设置会话。您应该根据会话在那里加载配置文件。此外,您可能希望发布您的路由。看起来您的路由也需要您的id。您的error是由于这一行-。@profile还没有定义,nil也没有定义,这破坏了url生成器。我不知道wicked_wizard的任何信息-在呈现个人模板之前正在运行哪个操作?我不清楚您的代码。我想您需要多读一些关于Rails的信息。类似的情况可能会在新的act中发生除非你真的知道自己在做什么@profile=profile.newparams[:profile]我的建议是让你浏览Rails指南,试着了解自己首先在做什么。此外,像may helpMax I这样的教程包括了多表单new.html.erb的第一步。
  <%= form_for(@profile) do |f| %>
      <% if @profile.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@profile.errors.count, "error") %> prohibited this profile from being saved:</h2>

          <ul>
          <% @profile.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>


    <h1>New profile</h1>

    <div>
        <%= f.radio_button ..., true %> <%= f.label ... %>
      </div>

      <div> 
        <%= f.radio_button ..., false %> <%= f.label ... %>
      </div>

    <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
  def safe_previous_wizard_path(id)
    id ? previous_wizard_path : root_path
  end
  <% if applicant.persisted? %>
    <a href="<%= previous_wizard_path %>">Back</a>
  <% end %>