我想我还是个新手,但我需要你的帮助。当我与habtm协会合作时,我遇到了问题。我有表格,我输入有关计算机的信息。关联habtm介于机器和操作系统之间。
class Machine < ActiveRecord::Base
....
has_and_belongs_to_many :operatingsystems, :join_table => "machines_operatingsystems", :readonly => false
....
end
class Operatingsystem < ActiveRecord::Base
....
has_and_belongs_to_many :machines, :join_table => "machines_operatingsystems", :readonly => false
....
end
我用复选框显示操作系统。
我修改了机器控制器的更新
def update
params[:machine][:operatingsystem_ids] ||= []
@machine = Machine.find(params[:id], :readonly => false)
respond_to do |format|
if @machine.update_attributes(params[:machine])
flash[:notice] = 'Machine was successfully updated.'
format.html { redirect_to(@machine) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @machine.errors, :status => :unprocessable_entity }
end
end
end
但是如果我编辑一台没有更改名称的机器并点击更新我有以下错误:
ActiveRecord::ReadOnlyRecord (ActiveRecord::ReadOnlyRecord):
app/controllers/machines_controller.rb:72
app/controllers/machines_controller.rb:71:in `update'
passenger (2.2.15) lib/phusion_passenger/rack/request_handler.rb:92:in `process_request'
passenger (2.2.15) lib/phusion_passenger/abstract_request_handler.rb:207:in `main_loop'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:441:in `start_request_handler'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:381:in `handle_spawn_application'
passenger (2.2.15) lib/phusion_passenger/utils.rb:252:in `safe_fork'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:377:in `handle_spawn_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `__send__'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `main_loop'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:163:in `start'
passenger (2.2.15) lib/phusion_passenger/railz/application_spawner.rb:222:in `start'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:253:in `spawn_rails_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server_collection.rb:126:in `lookup_or_add'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:247:in `spawn_rails_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server_collection.rb:80:in `synchronize'
passenger (2.2.15) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:246:in `spawn_rails_application'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:145:in `spawn_application'
passenger (2.2.15) lib/phusion_passenger/spawn_manager.rb:278:in `handle_spawn_application'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `__send__'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:352:in `main_loop'
passenger (2.2.15) lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously'
提前感谢您的帮助。
答案 0 :(得分:1)
除了machines_operatingsystems
和machine_id
之外,您的operatingsystem_id
表格中是否包含任何列?如果是这样,ActiveRecord将关联返回的记录标记为只读。
一个可能的罪魁祸首是连接表上的时间戳列。如果创建machines_operatingsystems
的迁移包含对timestamps
的调用,则可能是您问题的根源。