[英]Ruby on Rails- issues properly navigating through my nested routes (and their databases)
I'm fairly new to Ruby on Rails and am currently stuck on a problem involving my app not stepping through my two database tables the way I'd like it to, when navigating my nested routes. 我是Ruby on Rails的新手,目前遇到一个问题,该问题涉及到我的应用在导航嵌套路由时未按照我希望的方式逐步通过两个数据库表。
My basic set-up is like this: I have a table called 'countries', and a table called 'relations'. 我的基本设置是这样的:我有一个名为“国家”的表和一个名为“关系”的表。 I also have an show.html.erb page for localhost/countries/x/relations/y that is set to request and display the @country.name and the @relation.name instance variables from the two tables.
我还有一个localhost / countries / x / relations / y的show.html.erb页面,该页面设置为请求并显示两个表中的@ country.name和@ relation.name实例变量。
This is the problem: 这就是问题:
-Typing localhost/countries/ 1 /relations/ 1 into my address shows me my countries table's 1st country (right now, a dummy value called 'select a country') and my relations table's 1st relation (right now, Canada). -在我的地址中键入localhost / countries / 1 / relations / 1会显示我的国家表的第一个国家(现在是一个名为“选择国家”的虚拟值)和我的关系表的第一个关系(现在是加拿大)。 This is what I want to see in this instance.
这就是我想在这种情况下看到的。 So far, so good!
到现在为止还挺好!
-Typing localhost/countries/ 1 /relations/ 2 shows me my countries table's 2nd country (currently 'Andorra') and my relation's tables 2nd relation (currently, 'Iran'). -键入localhost / countries / 1 / relations / 2将显示我的国家表的第二个国家(当前为“安道尔”)和我的关系表的第二个国家(当前为“伊朗”)。 Here we have the first mistake: though the address bar call requests 'countries/1' the request is clearly fetching the value of 'countries/2' instead.
这是我们的第一个错误:尽管地址栏调用请求“ countries / 1”,但该请求显然是在获取“ countries / 2”的值。
-Typing localhost/countries/ 2 /relations/ 2 displays the exact same output as localhost/countries/1/relations/2, a second confirmation of this issue. -键入localhost / countries / 2 / relations / 2将显示与localhost / countries / 1 / relations / 2完全相同的输出,此问题的第二次确认。
-As a final example, typing localhost/countries/ 4 /relations/ 3 displays each table's 3rd entries, instead of the 4th entry in the countries table, and the 3rd entry in the relations table. -作为最后一个示例,键入localhost / countries / 4 / relations / 3将显示每个表的第3个条目,而不是countrys表中的第4个条目以及Relationships表中的第3个条目。 So we see the theme here.
因此,我们在这里看到了主题。
So what seems to be happening here is something like this: my nested route relations is overriding the persistence of the countries value with whatever happens to be the id called for the relations values. 因此,这里似乎正在发生的事情是这样的:我嵌套的路线关系正以任何被称为关系值的id取代了国家值的持久性。
I can sort of guess why this is happening (the @country.name in the views/relations/show.html.erb page is likely being processed by the relations controller instead of the countries controller and is defaulting to relation's whims if I don't tell it explicitly otherwise). 我可以猜测为什么会发生这种情况(views / relations / show.html.erb页面中的@ country.name可能是由关系控制器而不是国家/地区控制器处理的,如果我不这样做,则默认为关系的异想天开否则请明确告知)。 But I have little idea at the moment of how to fix that issue.
但是我目前不知道如何解决该问题。
The RailsGuides resource Routing from the Outside In got me through a couple of my earlier issues on this broad topic but I couldn't glean the answer from it this time. RailsGuides资源从外部路由进来使我了解了有关该广泛主题的较早版本的一些问题,但是这次我无法从中获得答案。 Any advice on my issue?
关于我的问题有什么建议吗?
Here's what I imagine are the relevant lines of code: 我想像的是以下相关代码行:
config/routes.rb
Rails.application.routes.draw do
...
...
resources :countries do
resources :relations
end
end
... ...
app/views/relations/show.html.erb
<strong>Country:</strong>
<%= @country.name %>
</p>
<p>
<strong>Relation:</strong>
<%= @relation.name %>
</p>
... ...
app/controllers/relations_controller.rb
class RelationsController < ApplicationController
before_action :set_relation, only: [:show, :edit, :update, :destroy]
def index
@relations = Relation.all
end
def show
@country = Country.find(params[:id])
end
def new
@relation = Relation.new
end
...
...
... ...
app/controllers/countries_controller.rb
class CountriesController < ApplicationController
def show
@country = Country.find(params[:id])
@countries = Country.all
end
def new
@country = Country.find(params[:id])
@countries = Country.all
end
end
... ...
app/models/country.rb
class Country < ActiveRecord::Base
...
...
has_many :relations
end
... ...
app/models/relation.rb
class Relation < ActiveRecord::Base
validates_uniqueness_of :name
belongs_to :country
end
Thanks for any help or advice! 感谢您的任何帮助或建议! Let me know if you have other questions.
如果您还有其他问题,请告诉我。
With your nested route countries/1/relations/2 the two ids are assigned to different parameters. 使用嵌套的路线国家/ 1 /关系/ 2,会将两个ID分配给不同的参数。 This form of the route makes that clear: countries/:country_id/relations/:id.
路线的这种形式可以清楚地表明:countries /:country_id / relations /:id。 You can see the route in this format if you rake routes .
如果您耙路线,则可以以这种格式查看路线 。
So your relations controller needs to look something like this: 因此,您的关系控制器需要看起来像这样:
class RelationsController < ApplicationController
before_action :set_country
before_action :set_relation, only: [:show, :edit, :update, :destroy]
def index
@relations = @county.relations
end
def show
# don't need anything here as set_relation already assigning @relation
end
def new
@relation = @country.relations.build
end
....
private
def set_country
@country = Country.find(params[:country_id])
end
def set_relation
@relation = @country.relations.find(params[:id])
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.