[英]Trying to access a join table which has no ID
I've got three tables which all join into one table. 我有三个表,所有表都合并为一个表。 Here is the code:
这是代码:
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
I've already used scaffold for the App and Server model and can access them, but when I created a scaffold for AppServer and tried accessing the index page, it gives me this error: 我已经为App和Server模型使用了脚手架,并且可以访问它们,但是当我为AppServer创建脚手架并尝试访问索引页面时,它给了我这个错误:
No route matches {:action=>"show", :controller=>"app_servers", :id=>#<AppServer app_id: 3, server_id: 1, environment_id: 2>}
I believe the reason for the error is because when I created the AppServer table, I specified that :id => false
. 我相信该错误的原因是因为在创建AppServer表时,我指定了
:id => false
。
So what I'm really trying to do, is just have the create page, which then allows me to map currently saved Servers, with currently saved Application to a specific environment. 因此,我真正想做的只是创建一个页面,该页面允许我将当前保存的服务器与当前保存的应用程序映射到特定环境。 Any ideas on how I can overcome this problem?
关于如何克服这个问题的任何想法?
Also, is it wrong for my join model to not have an ID, as it was originally a has_and_belongs_to_many
model? 另外,我的连接模型最初没有
has_and_belongs_to_many
模型,因此没有ID是has_and_belongs_to_many
吗?
Thanks in advance 提前致谢
Edit Here is the index.html.erb
file: 编辑这是
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 %>
And also the app_servers_controller.rb
file: 还有
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
Ok. 好。 The simplesst solution here is to return
id
back. 这里最简单的解决方案是返回
id
。 Or you should rewrite some standart routes like 或者您应该重写一些标准路线,例如
get '/app_servers/show', :to => "app_servers#show", :as => :app_server
and remove fetching by id
from controllers. 并从控制器中删除按
id
提取。
I mean - return IDS back - that's easiear 我的意思是-退回IDS-这很容易
UPD UPD
Fast and dirty solution here is to add to AppServer
model this small method: 快速而肮脏的解决方案是将这种小方法添加到
AppServer
模型中:
def to_param
"#{environment_it}_#{app_id}_#{server_id}"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.