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:
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
.
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?
Thanks in advance
Edit Here is the index.html.erb
file:
<h1>Listing app_servers</h1>
<table>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<% @app_servers.each do |app_server| %>
<tr>
<td><%= app_server%>开发者_如何学Python;</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:
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. 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.
I mean - return IDS back - that's easiear
UPD
Fast and dirty solution here is to add to AppServer
model this small method:
def to_param
"#{environment_it}_#{app_id}_#{server_id}"
end
精彩评论