背景:我想组建一个团队,让用户在团队保存之前验证该团队的地址。
在我的应用程序中,我有一个表单,可以在提交表单时创建一个团队。在这个表单中,我有一个部分,假设用字段位置呈现。当用户在部分表单中单击提交时,位置字段(在部分表单内而不是创建团队表单)应该转到teams_controller中的verify_Address操作。而不是发生这种情况,我加载页面时出现错误。
页面加载错误:
undefined local variable or method `verify_address' for #<#<Class:0x000001063ec8d8>:0x00000104555af0>
突出显示此行:<%= form_for :team, url: verify_address, method: :post, remote:true do |f|%>
以下是我在应用程序中的文件。
route.rb文件:
resources :users, :path => :captains, :as => :captains, :controller => "captains" do
resources :teams, :only => [:new, :create, :edit, :update, :destroy], controller: 'captains/teams'
end
resources :teams, :only => [:index, :show] do
resources :users, :path => :teammates, :as => :teammates, :controller => "teammates"
end
put 'captains/:id/teams/verify_address' => "teams#verify_address",as: 'verify_address'
get 'captains/:id/teams/verify_address' => "teams#verify_address"
控制器/众将/ teams_controller.rb:
class Captains::TeamsController < ApplicationController
respond_to :html, :js
def new
@team = Team.new
@captain = current_user
end
def verify_address
@address = params[:team][:location]
@validate_address = Team.validate_address(@address)
end
def create
@captain = current_user.id
@team = Team.create(
:name => params[:team][:name],
:location => params[:team][:location],
:sport => params[:team][:sport],
:captain_id => @captain,
:avatar => params[:team][:avatar]
)
if @team.present?
redirect_to @team # show view for team
end
binding.pry
end
end
partial views / captains / teams / _verify_address.html.erb:
<%= form_for :team, url: verify_address, method: :post, remote:true do |f|%>
<div class = "form-group">
<%= f.label :location %>
<%= f.text_field :location, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.submit "Verify address" ,class: 'btn btn-success' ,id: 'verify_address' %>
</div>
<% end %>
主要表单views / captains / teams / new.html.erb:
<%= form_for :team, url: captain_teams_path(@captain, @team), method: :post do |f|
%GT;
<div class="form-group">
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<%= f.hidden_field :avatar_cache %>
</div>
<div class = "form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.label :sport %>
<%= f.text_field :sport, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.label :location %>
<%= f.text_field :location, class: 'form-control', placeholder: "Enter wiki title", id:'team_title' %>
</div>
<div class = "form-group">
<%= f.submit class: 'btn btn-success' ,id: 'team_role_submit' %>
</div>
<% end %>
</div>
<%= render partial: "/captains/teams/verify_address", locals: { address: @address, validate_address: @validate_address}%>
</div>
答案 0 :(得分:1)
创建自定义路由verify_address
生成verify_address_path
网址助手,您应该在表单中使用。