0
背景:我想組隊並讓用戶在保存團隊之前驗證該團隊的地址。軌道渲染部分和找到自定義控制器的問題
在我的應用程序中,我有一個表單在表單提交時創建一個團隊。在這個表單中,我有一個部分是假設使用字段位置呈現的。當用戶在部分表單中單擊提交時,位置字段(在部分表單而不是創建團隊表單中)應該轉到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
的局部視圖/船長/支/ _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 %>
主要形式意見/隊長/隊/ new.html.erb:
<%= form_for :team, url: captain_teams_path(@captain, @team), method: :post do |f|
%>
<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>