0

尝试将本地人传递给部分时出现以下错误:

nil:NilClass 的未定义方法“country_code”

这就是我的代码的样子:

<!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>

部分的:

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

控制器:

class OrdersController < ApplicationController

layout false

def index

end

def subregion_options
render partial: 'subregion_select'
end

end

路线:

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

非常感谢您的帮助 :)

4

2 回答 2

1

我认为您需要在控制器中的适当操作中添加此内容:

@order = Order.new

并认为,相反

form_for(:order)

form_for(@order)

添加到您的 config/routes.rb 如下:

resources :orders, only: [:index]
于 2014-06-26T01:10:34.207 回答
0

最后它在你的建议之后起作用了。我也有这个错误,它没有在 url 中传递参数:

url = "subregion_options?parent_region=#{country_code}";

我不知道在 cofeescript 但在 javascript 中正确的方法是这样的:

url = "subregion_options?parent_region="+country_code;
于 2014-06-28T15:03:55.093 回答