我见过很多类似的路由问题和 id=>nil 帖子,但没有一个解决方案能解决我的错误。
首先是整个错误:
ProfileSteps#personal 中的 ActionController::UrlGenerationError
显示 ...profile_steps/personal.html.erb 其中第 1 行引发:
没有路由匹配 {:action=>"show", :controller=>"profile_steps", :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
意见/profile_steps/personal.html.erb
<%= form_for @profile, url: wizard_path do |f| %>
<br>
<div class="field">
<%= f.label :name, "Company Name" %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :website %><br>
<%= f.text_field :website %>
</div>
<br>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
路线.rb
project::Application.routes.draw do
resources :profiles
resources :profile_steps
devise_for :users, :controllers => { :registrations => "registrations" }
root "pages#home"
get "profile" => "pages#profile"
match "profile_steps/personal", to: "profile_steps#personal", via: "post"
提前致谢,如果在之前的帖子中解决了这个问题,我深表歉意。
更新:这是多步骤表单的第一页:
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 %>