Rails 4: has_many的路由:通过关联。

时间:2022-05-10 11:00:49

I have three models:

我有三个模型:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

When I simply use the following routes:

当我简单地使用以下路线:

resources :users
resources :administrations
resources :calendars

everything works, meaning that I can visit

一切都很好,意味着我可以去参观

  • http://localhost:3000/users
  • http://localhost:3000 /用户
  • http://localhost:3000/administrations
  • http://localhost:3000 /*
  • http://localhost:3000/calendars
  • http://localhost:3000 /日历

and CRUD instances of each model.

以及每个模型的CRUD实例。

But this is not what I want to achieve.

但这不是我想要实现的。

What I want is to allow users to:

我想让用户:

  • Access a list of all their calendars at: http://localhost:3000/users/:id/calendars
  • 访问所有日历的列表:http://localhost:3000/users/:id/calendar。
  • Access each of their calendar at http://localhost:3000/users/:id/calendars/:id
  • 在http://localhost:3000/users/:id/calendar /:id/calendar /:id访问每个日历
  • For a given calendar, access a list of all the users of this calendar (but here I am not sure which URL would make sense).
  • 对于给定的日历,访问这个日历的所有用户的列表(但是我不确定哪个URL有意义)。

So I figured I needed to update my routes.

所以我想我需要更新我的路线。

Here is what I have tried:

以下是我尝试过的:

SOLUTION 1

解决方案1

resources :users do
  resources :administrations
  resources :calendars
end

But then, when I visit http://localhost:3000/users/1/calendars, I get the following error:

但是,当我访问http://localhost:3000/users/1/calendar时,会得到以下错误:

NameError in Calendars#index

Showing /Users/TXC/code/rails/calendy/app/views/calendars/index.html.erb where line #27 raised:

undefined local variable or method `new_calendar_path' for #<#<Class:0x007fb807e672d0>:0x007fb807e66510>

Extracted source (around line #27):

<br>

<%= link_to 'New Calendar', new_calendar_path %>

SOLUTION 2

解决方案2

concern :administratable do
  resources :administrations
end

resources :users, concerns: :administratable
resources :calendars, concerns: :administratable

But then, when I visit http://localhost:3000/users/1/calendars, I get the following error:

但是,当我访问http://localhost:3000/users/1/calendar时,会得到以下错误:

Routing Error
No route matches [GET] "/users/1/calendars"

I must be missing something obvious — sorry, I am not very experienced with Rails — but I cannot figure out why.

我肯定漏掉了一些明显的东西——对不起,我对Rails不是很熟悉——但是我不知道为什么。

Any idea how I should nest my resources?

你知道我该如何保存我的资源吗?

1 个解决方案

#1


3  

The solution 1 is the way I would take. The problem is that when you nest the resources, it causes the helpers to change (you can see this by running rake routes). The correct helper method is new_user_calendar_path so change

解1是我的方法。问题是,当您嵌套资源时,会导致helper类更改(您可以通过运行rake路由看到这一点)。正确的助手方法是new_user_calendar_path,因此更改

<%= link_to 'New Calendar', new_calendar_path %>

to

<%= link_to 'New Calendar', new_user_calendar_path(@user) %>

where @user is set in the controller to point to correct user.

在控制器中,将@user设置为指向正确的用户。

Bonus

奖金

You could also take a look at the shallow routes (section 2.7.2 in that page). They provide a cleaner way to call the nested calendars with URL

您还可以查看浅路径(该页第2.7.2节)。它们提供了使用URL调用嵌套日历的更简洁的方法

GET /calendars/:id

instead of

而不是

GET /users/:userId/calendars/:calendarId

since the calendarId is anyway unique.

因为日历是独一无二的。

#1


3  

The solution 1 is the way I would take. The problem is that when you nest the resources, it causes the helpers to change (you can see this by running rake routes). The correct helper method is new_user_calendar_path so change

解1是我的方法。问题是,当您嵌套资源时,会导致helper类更改(您可以通过运行rake路由看到这一点)。正确的助手方法是new_user_calendar_path,因此更改

<%= link_to 'New Calendar', new_calendar_path %>

to

<%= link_to 'New Calendar', new_user_calendar_path(@user) %>

where @user is set in the controller to point to correct user.

在控制器中,将@user设置为指向正确的用户。

Bonus

奖金

You could also take a look at the shallow routes (section 2.7.2 in that page). They provide a cleaner way to call the nested calendars with URL

您还可以查看浅路径(该页第2.7.2节)。它们提供了使用URL调用嵌套日历的更简洁的方法

GET /calendars/:id

instead of

而不是

GET /users/:userId/calendars/:calendarId

since the calendarId is anyway unique.

因为日历是独一无二的。