首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >carmen rails未定义方法`country_code‘for nil:NilClass

carmen rails未定义方法`country_code‘for nil:NilClass
EN

Stack Overflow用户
提问于 2014-06-25 23:58:34
回答 2查看 503关注 0票数 0

当试图将局部变量传递给部分时,我有以下错误:

用于nil:nil类的未定义方法`country_code‘

这就是我的代码的样子:

代码语言:javascript
复制
<!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>

部分:

代码语言:javascript
复制
<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>

主计长:

代码语言:javascript
复制
class OrdersController < ApplicationController

layout false

def index

end

def subregion_options
render partial: 'subregion_select'
end

end

路线:

代码语言:javascript
复制
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

非常感谢你的帮助:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-26 01:10:34

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

代码语言:javascript
复制
@order = Order.new

而在视野中,

代码语言:javascript
复制
form_for(:order)

代码语言:javascript
复制
form_for(@order)

将以下内容添加到您的config/ your es.rb中:

代码语言:javascript
复制
resources :orders, only: [:index]
票数 1
EN

Stack Overflow用户

发布于 2014-06-28 15:03:55

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

代码语言:javascript
复制
url = "subregion_options?parent_region=#{country_code}";

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

代码语言:javascript
复制
url = "subregion_options?parent_region="+country_code;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24420468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档