user3266824
user3266824

Reputation: 1263

trouble with rails rendering partial and finding custom controller

Background: I would like to make a team and have the user verify the address of that team before the team is saved.

In my application I have a form that creates a team when the form is submitted. Within this form I have a partial that is suppose to render with a field location. When the user clicks submit within the partial form the location field (within the partial form and not the create team form) should go to the verify_Address action within the teams_controller. Instead of this happening I get an error when I load the page.

The error on pageload:

undefined local variable or method `verify_address' for #<#<Class:0x000001063ec8d8>:0x00000104555af0> 

with this line highlighted: <%= form_for :team, url: verify_address, method: :post, remote:true do |f|%> below are my files within the app.

route.rb file:

 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"

controller/captains/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

the 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 %>

the main form views/captains/teams/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>

Upvotes: 0

Views: 58

Answers (1)

dostu
dostu

Reputation: 1518

Creating a custom route verify_address generates verify_address_path url helper, which you should use in your form.

Upvotes: 1

Related Questions