【发布时间】:2016-11-06 22:28:26
【问题描述】:
目前,我正在开发一个关于 Ruby on Rails 的客户项目,该项目要求提供用户名、名字、姓氏、街道 1、街道 2、城市和州等信息。在这个项目中,City 和 State 应该是选择框,其中包含来自 city-state gem 的填充条目。
我在 Gemfile 中输入 gem 'city-state',然后在终端 (Mac OS) 中运行 bundle install。
现在在我的表单文件中,_form.html.erb:
<%= form_for(@customer) do |f| %>
<% if @customer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@customer.errors.count, "error") %> prohibited this customer from being saved:</h2>
<ul>
<% @customer.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %><br>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :street1 %><br>
<%= f.text_field :street1 %>
</div>
<div class="field">
<%= f.label :street2 %><br>
<%= f.text_field :street2 %>
</div>
<div class="field">
<%= f.label :state %><br>
<select id="states-of-country" name="state">
<option value="" selected="">Select Your State</option>
<% CS.states(:us).each do |key, value| %>
<option value="<%= key %>"><%= value %></option>
<% end %>
</select>
</div>
<div class="field">
<%= f.label :city %><br>
<select id="cities-of-state" name="city">
<option value="" selected="">Select Your City</option>
</select>
</div>
<script>
$('#states-of-country').change(function () {
var input_state = $(this);
var cities_of_state = $("#cities-of-state");
if ($(this).val() == "") {
cities_of_state.html("");
} else {
$.getJSON('/cities/' + $(this).val(), function (data {
// cities_of_state.empty();
var opt = '<option value="" selected="">Select Your City</option>';
console.log(data);
if (data.length == 0) {
} else {
data.forEach(function (i) {
opt += '<option value="+ i +">' + i + '</option>';
cities_of_state.html(opt);
});
}
});
}
});
</script>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
错误集中在CS.states(:us) 然后当我运行服务器时,每次我去创建或编辑客户时,我都会收到这个错误:
权限被拒绝 - /Library/Ruby/Gems/2.0.0/gems/city-state-0.0.13/lib/db/states.us
PS。在我的 gem 文件中输入 gem 'city-state' 之前,我首先在终端中执行此操作:sudo gem install 'city-state'
【问题讨论】:
-
尝试使用 gem install 'city-state' 运行。然后重试看看是否有效。
-
你应该使用
sudo命令来安装你的gem,除非绝对必要 -
这正是我最初所做的。我已经安装了城邦,但我不断收到 Errno::EACCES 错误。 @oreoluwa
-
好的。不过,一个建议是,始终使用版本管理器,例如
rbenv或rvm来管理您的 ruby 版本。将为您节省大量的生产时间 -
我已经通过输入“sudo chown -R $(whoami) /Library”解决了这个问题:)
标签: ruby-on-rails ruby macos rubygems