我有三个表都加入到一个表中。这是代码:
class App < ActiveRecord::Base
has_many :app_servers
has_many :servers, :through => :app_servers
has_many :environments, :through => :app_servers
end
class Server < ActiveRecord::Base
has_many :app_servers
has_many :apps, :through => :app_servers
has_many :environments, :through => :app_servers
end
class Environment < ActiveRecord::Base
has_many :app_servers
has_many :apps, :through => :app_servers
has_many :servers, :through => :app_servers
end
class AppServer < ActiveRecord::Base
belongs_to :app
belongs_to :server
belongs_to :environment
end
我已经在app和Server模型中使用了scaffold并且可以访问它们,但是当我为AppServer创建一个脚手架并尝试访问索引页面时,它给了我这个错误:
No route matches {:action=>"show", :controller=>"app_servers", :id=>#<AppServer app_id: 3, server_id: 1, environment_id: 2>}
我认为错误的原因是因为当我创建AppServer表时,我指定了:id => false
。
所以我真正想要做的就是拥有创建页面,然后允许我将当前保存的服务器映射到当前保存的应用程序到特定环境。 关于如何克服这个问题的任何想法?
另外,我的连接模型没有ID是错误的,因为它最初是has_and_belongs_to_many
模型?
提前致谢
修改以下是index.html.erb
文件:
<h1>Listing app_servers</h1>
<table>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<% @app_servers.each do |app_server| %>
<tr>
<td><%= app_server%></td>
<td><%= link_to 'Show', app_server %></td>
<td><%= link_to 'Edit', edit_app_server_path(app_server) %></td>
<td><%= link_to 'Destroy', app_server, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New App server', new_app_server_path %>
还有app_servers_controller.rb
文件:
class AppServersController < ApplicationController
# GET /app_servers
# GET /app_servers.xml
def index
@app_servers =AppServer.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @app_servers }
end
end
# GET /app_servers/1
# GET /app_servers/1.xml
def show
@app_server = AppServer.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @app_server }
end
end
# GET /app_servers/new
# GET /app_servers/new.xml
def new
@app_server = AppServer.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @app_server }
end
end
# GET /app_servers/1/edit
def edit
@app_server = AppServer.find(params[:id])
end
# POST /app_servers
# POST /app_servers.xml
def create
@app_server = AppServer.new(params[:app_server])
respond_to do |format|
if @app_server.save
format.html { redirect_to(@app_server, :notice => 'App server was successfully created.') }
format.xml { render :xml => @app_server, :status => :created, :location => @app_server }
else
format.html { render :action => "new" }
format.xml { render :xml => @app_server.errors, :status => :unprocessable_entity }
end
end
end
# PUT /app_servers/1
# PUT /app_servers/1.xml
def update
@app_server = AppServer.find(params[:id])
respond_to do |format|
if @app_server.update_attributes(params[:app_server])
format.html { redirect_to(@app_server, :notice => 'App server was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @app_server.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /app_servers/1
# DELETE /app_servers/1.xml
def destroy
@app_server = AppServer.find(params[:id])
@app_server.destroy
respond_to do |format|
format.html { redirect_to(app_servers_url) }
format.xml { head :ok }
end
end
end
答案 0 :(得分:1)
确定。这里最简单的解决方案是返回id
。或者你应该重写一些标准路线,如
get '/app_servers/show', :to => "app_servers#show", :as => :app_server
并从控制器中删除id
提取。
我的意思是 - 返回IDS - 这很容易
<强> UPD 强>
这里快速而肮脏的解决方案是将AppServer
模型添加到这个小方法:
def to_param
"#{environment_it}_#{app_id}_#{server_id}"
end