我正在将Rails 2应用程序转换为Rails 3.我目前有一个控制器设置如下:
class Api::RegionsController < ApplicationController
respond_to :xml, :json
end
Run Code Online (Sandbox Code Playgroud)
使用和看起来如下的操作:
def index
@regions = Region.all
respond_with @regions
end
Run Code Online (Sandbox Code Playgroud)
实现非常简单,api/regions,api/regions.xml和api/regions.json都会按照您的预期做出响应.问题是我希望api/regions默认通过XML响应.我有消费者期望XML响应,我不想让他们更改所有的URL以包含.xml,除非绝对必要.
在Rails 2中,您可以通过执行以下操作来实现:
respond_to do |format|
format.xml { render :xml => @region.to_xml }
format.json { render :json => @region.to_json }
end
Run Code Online (Sandbox Code Playgroud)
但在Rails 3中,我找不到将其默认为XML响应的方法.有任何想法吗?
我有一个has_many through关联工作的问题.
我一直得到这个例外:
Article.find(1).warehouses.build
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :entries in model Article
Run Code Online (Sandbox Code Playgroud)
这些是涉及的模型:
class Article < ActiveRecord::Base
has_many :warehouses, :through => :entries
end
class Warehouse < ActiveRecord::Base
has_many :articles, :through => :entries
end
class Entry < ActiveRecord::Base
belongs_to :article
belongs_to :warehouse
end
Run Code Online (Sandbox Code Playgroud)
这是我的架构:
create_table "articles", :force => true do |t|
t.string "article_nr"
t.string "name"
t.integer "amount"
t.string "warehouse_nr"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "unit"
end
create_table "entries", :force => true do |t|
t.integer "warehouse_id"
t.integer "article_id" …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
def addcar
@car = Car.new(params[:car])
render :action => 'list'
end
<% @allcars.each do |cell| %>
<p>
<%= link_to cell.to_s, :controller => 'car', :action => 'addcar', :car => cell.to_s %>
</p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
undefined方法`stringify_keys!' 为"本田":字符串
我不明白有什么问题:car.
我有一个大型(100gb +)数据库,我正在尝试使用官方postgres图像运行.
我无法将数据存储在docker卷中,因为~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2Docker for Mac中的文件大小限制为大约60gb.
我对将主机目录挂载为卷犹豫不决,因为挂载的主机目录卷中的文件访问速度比常规卷慢得多.
这些是一些有用的链接,可以更详细地讨论这些问题:
你们都吃速度损失并挂载主机目录吗?你手动创建一个qcow文件,可以用qemu增大(如果这样做,你需要在升级之间维护这个文件)吗?你还有别的办法处理这个问题吗?
我已经看到一些SO帖子解释了如何使用pry进入rspec测试并且能够做到这一点.一旦我到达断点,我就很难显示任何有用的信息.对于下面的代码,我想从pry控制台检查响应对象:
describe 'happenings' do
context "#index (GET /api/v1/flat_happenings.json)" do
before(:each) do
30.times { FactoryGirl.create(:flat_happening) }
get "/api/v1/flat_happenings.json"
end
describe "should list all flat_happenings" do
binding.pry
it { JSON.parse(response.body)["flat_happenings"].length.should eq 30 }
end
end
end
Run Code Online (Sandbox Code Playgroud)
关于如何做到这一点的任何想法?
我有一个属于Car页面的Feature页面.除了一件事,这正是我想要的.
在创建,更新或销毁之后,我希望将页面重定向到创建和更新admin_car_path(car)的默认值而不是.admin_car_feature_path(car,feature)admin_car_features_path(car)
我没有成功搜索到那个.
ActiveAdmin.register Car do
end
ActiveAdmin.register Feature do
belongs_to :car
end
Run Code Online (Sandbox Code Playgroud)
TIA
protect_from_forgery with: :exception工作怎么样?
我想编辑代码来查看它并从中学习.但是,我无法找到更高级别的抽象位置.
我想知道git存储库可以处理的提交数量是否有上限.
在我正在进行的一个独立项目中,我一直在本地编写代码,提交/推送git中的更改,然后在我的开发服务器上进行更改.
我认为这是在本地工作和通过FTP上传更改的更简单的替代方案...幸运/遗憾的是,这是一个如此简单的工作流程,我有时会在编码时经历许多编辑/提交/推/拉/浏览器刷新周期.
我想知道这是否会转过来让我陷入困境.如果它可能是一个问题,我想知道如何避免这种麻烦...看起来像是一个转折可能是要走的路,特别是因为我不必担心冲突的分支等.
全新的 Rails 6.1.4.1 应用程序无法运行bin/webpack-dev-server,并出现以下错误:
/rails_app/node_modules/webpack-cli/bin/utils/prompt-command.js:46
return func(...args);
^
TypeError: Class constructor ServeCommand cannot be invoked without 'new'
at runWhenInstalled (/rails_app/node_modules/webpack-cli/bin/utils/prompt-command.js:46:9)
at promptForInstallation (/rails_app/node_modules/webpack-cli/bin/utils/prompt-command.js:140:10)
at /rails_app/node_modules/webpack-cli/bin/cli.js:32:43
at Object.<anonymous> (/rails_app/node_modules/webpack-cli/bin/cli.js:366:3)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
Run Code Online (Sandbox Code Playgroud) 我有两个型号:Project和ProjectDiscipline:
class Project < ActiveRecord::Base
has_many :project_disciplinizations, :dependent => :destroy
has_many :project_disciplines, through: :project_disciplinizations
attr_accessible :project_discipline_ids
attr_accessible :project_disciplines_attributes
accepts_nested_attributes_for :project_disciplines, :reject_if => proc { |attributes| attributes['name'].blank? }
end
class ProjectDiscipline < ActiveRecord::Base
attr_accessible :name
has_many :project_disciplinizations, :dependent => :destroy
has_many :projects, through: :project_disciplinizations
end
class ProjectDisciplinization < ActiveRecord::Base
attr_accessible :project_discipline_id, :project_id
belongs_to :project_discipline
belongs_to :project
end
Run Code Online (Sandbox Code Playgroud)
在新/编辑Project表单上,我有一个学科列表和每个学科的复选框,以便用户可以选择学科:
<div class="control-group">
<label class="control-label">Check disciplines that apply</label>
<div class="controls">
<%= f.collection_check_boxes(:project_discipline_ids, ProjectDiscipline.order('name'), :id, :name, {}, {:class => …Run Code Online (Sandbox Code Playgroud) ruby ×2
activeadmin ×1
activerecord ×1
associations ×1
commit ×1
debugging ×1
docker ×1
git ×1
macos ×1
parameters ×1
postgresql ×1
pry ×1
rebase ×1
rspec ×1
rspec-rails ×1
validation ×1
webpack ×1