I'm having the following error while trying to pass locals to the partial:
undefined method `country_code' for nil:NilClass
This is what my code looks like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Trial</title>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<script type="text/javascript">
$(document).ready(function() {
$('select#order_country_code').change(function(){select_wrapper = $('#order_state_code_wrapper');
$('select', select_wrapper).attr('disabled', true);
country_code = $(this).val();
url = "subregion_options?parent_region=#{country_code}";
$('#order_state_code_wrapper').load(url);
});
});
</script>
</head>
<body>
<%= form_for(:order) do |f| %>
<div class="field">
<%= f.label :country_code %><br />
<%= f.country_select :country_code, priority: %w(US CA), prompt: 'Please select a country' %>
<%= f.label :state_code %><br />
<%= render partial: 'subregion_select', locals: {parent_region: f.object.country_code} %>
</div>
<% end %>
</body>
</html>
Partial:
<div id="order_state_code_wrapper">
<% parent_region ||= params[:parent_region] %>
<% country = Carmen::Country.coded(parent_region) %>
<% if country.nil? %>
<em>Please select a country above</em>
<% elsif country.subregions? %>
<%= subregion_select(:order, :state_code, parent_region) %>
<% else %>
<%= text_field(:order, :state_code) %>
<% end %>
</div>
Controller:
class OrdersController < ApplicationController
layout false
def index
end
def subregion_options
render partial: 'subregion_select'
end
end
Routes:
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'access#index'
match ':controller(/:action(/:id))', :via => [:get, :post]
get 'subregion_options' => 'orders#subregion_options'
end
Thank you very much for your help :)
I think you need to add this in appropriate action in your controller :
@order = Order.new
and in view, instead
form_for(:order)
put
form_for(@order)
Add to your config/routes.rb following:
resources :orders, only: [:index]