carmen rails undefined方法`country_code'代表nil:NilClass

时间:2014-06-25 23:58:35

标签: ruby-on-rails undefined carmen

我在尝试将locals传递给partial时遇到以下错误:

  

未定义的方法`country_code'为零:NilClass

这就是我的代码:

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

非常感谢你的帮助:))

2 个答案:

答案 0 :(得分:1)

我认为您需要在控制器中以适当的操作添加它:

@order = Order.new

并且在视野中,而不是

form_for(:order)

form_for(@order)

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

resources :orders, only: [:index]

答案 1 :(得分:0)

最后它可以在你的建议之后工作。我也有这个错误,它没有在网址中传递参数:

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

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

url = "subregion_options?parent_region="+country_code;