Rails 上具有多对一关系 Ruby 的模型中的 "show" 和 "index" 路由出现问题
Trouble with "show" and "index" routes in model with many to one relationship Ruby on Rails
我觉得解决这个问题其实很简单,但我想不通。
https://github.com/kingdavidek/StuddyBuddy
我正在编写一个应用程序来帮助我总结非小说类书籍。我想要这本书(或 Piece)的视图,然后是与 chapters/sections 的一对多关系,其中每个部分都有自己的视图。我还希望能够制作与部分等具有多对一关系的小节。
所以这将是一种树状结构,其中每个较小的部分都有自己的摘要。 (我基本上是在关注 Jumpstart 实验室博主项目,但将其改编为我自己使用。http://tutorials.jumpstartlab.com/projects/blogger.html#blogger-2)
我已经能够成功地为该作品创建视图并且 links 到部分。我谈到了关于 jumpstart 教程的评论部分,其中评论与文章具有多对一关系,并且评论显示在父文章下方。我想对 sections/chapters 使用相同的概念,除了我想 link 每个单独的部分并在新视图中单独显示。
截面模型的路线说:
piece_section GET /pieces/:piece_id/sections/:id(.:format) sections#show
piece_sections GET /pieces/:piece_id/sections(.:format) sections#index
我无法在新页面中显示单个部分。
我的第一个问题是不能link直接到个别版块的show action,不知道为什么:
这是 show.html.erb 中家长 "Piece" 或书籍的相关部分:
<% @pieces.sections.each do |section| %>
<li>
<%= link_to section.name, piece_section_path(section), class: 'section_name' %>
<!-- this middle part 'piece.name' is the psart we want the link to say, the next part is our route helper.-->
</li>
<% end %>
<%= link_to "delete", piece_path(@pieces), method: :delete, data: {confirm: "Really delete the article?"}
%>
父级 "Piece" 控制器:
def show
@pieces = Piece.find(params[:id])
end
当我尝试这样做时,出现以下错误:
ActionController::UrlGenerationError in Pieces#show
Showing /home/david/Documents/Learning/StuddyBuddy/app/views/pieces/show.html.erb where line #9 raised:
No route matches {:action=>"show", :controller=>"sections", :id=>nil, :piece_id=>#<Section id: 1, name: "xama", broader_context: "hahahah", summary: nil, key_questions_addressed: nil, thoughts: nil, piece_id: 1, created_at: "2016-07-28 10:46:05", updated_at: "2016-07-28 10:46:05">} missing required keys: [:id]
<%= link_to section.name, piece_section_path(section), class: 'section_name' %>
当我 link 到 sections 模型的索引动作时它起作用了,我看到父 "Piece" 的 "show" 动作非常好。
我的第二个问题是当我在查看 Piece 时。我试着点击一个单独的部分(即这个 link:
<%= link_to section.name, piece_sections_path(section), class: 'section_name' %>
)
由于某种原因,我无法显示截面模型的截面和列。
这里是部分控制器:
class SectionsController < ApplicationController
def index
@pieces = Piece.find(params[:piece_id])
@sections = @pieces.sections.find(params[:section_id])
end
def show
@sections = Section.all
#@sections = Piece.sections.find(params[:id])
#@sections = @piece.sections.all
#@sections.piece_id = @pieces.id
end
end
这是截面模型的 index.html.erb 文件:
<h1> Yoyo</h1>
<p>Piece: <%= @pieces.name%></p>
<p> Section: <%= @sections.name%></p>
<p> Broader Context: <%= @sections.broader_context %></p>
这是 routes.rb 文件:
Rails.application.routes.draw do
root to: 'articles#index'
resources :pieces do
resources :sections
end
end
我遇到的问题是:
ActiveRecord::RecordNotFound in SectionsController#index
Couldn't find Section without an ID
Request
Parameters:
{"piece_id"=>"1"}
我不明白的是我怎么没有权限访问版块 ID??我怎么只能访问piece_id??
非常感谢您的帮助,因为我已经坚持了一段时间。
将 piece id 传递给 link 给 helper,因为 piece_section_path
需要两个参数
piece_section GET /pieces/:piece_id/sections/:id(.:format) sections#show
:piece_id
:id
(section_id
)
将您的 link 更改为:
<%= link_to section.name, piece_section_path(@pieces, section), class: 'section_name' %>
我觉得解决这个问题其实很简单,但我想不通。
https://github.com/kingdavidek/StuddyBuddy
我正在编写一个应用程序来帮助我总结非小说类书籍。我想要这本书(或 Piece)的视图,然后是与 chapters/sections 的一对多关系,其中每个部分都有自己的视图。我还希望能够制作与部分等具有多对一关系的小节。
所以这将是一种树状结构,其中每个较小的部分都有自己的摘要。 (我基本上是在关注 Jumpstart 实验室博主项目,但将其改编为我自己使用。http://tutorials.jumpstartlab.com/projects/blogger.html#blogger-2)
我已经能够成功地为该作品创建视图并且 links 到部分。我谈到了关于 jumpstart 教程的评论部分,其中评论与文章具有多对一关系,并且评论显示在父文章下方。我想对 sections/chapters 使用相同的概念,除了我想 link 每个单独的部分并在新视图中单独显示。
截面模型的路线说:
piece_section GET /pieces/:piece_id/sections/:id(.:format) sections#show
piece_sections GET /pieces/:piece_id/sections(.:format) sections#index
我无法在新页面中显示单个部分。
我的第一个问题是不能link直接到个别版块的show action,不知道为什么:
这是 show.html.erb 中家长 "Piece" 或书籍的相关部分:
<% @pieces.sections.each do |section| %>
<li>
<%= link_to section.name, piece_section_path(section), class: 'section_name' %>
<!-- this middle part 'piece.name' is the psart we want the link to say, the next part is our route helper.-->
</li>
<% end %>
<%= link_to "delete", piece_path(@pieces), method: :delete, data: {confirm: "Really delete the article?"}
%>
父级 "Piece" 控制器:
def show
@pieces = Piece.find(params[:id])
end
当我尝试这样做时,出现以下错误:
ActionController::UrlGenerationError in Pieces#show
Showing /home/david/Documents/Learning/StuddyBuddy/app/views/pieces/show.html.erb where line #9 raised:
No route matches {:action=>"show", :controller=>"sections", :id=>nil, :piece_id=>#<Section id: 1, name: "xama", broader_context: "hahahah", summary: nil, key_questions_addressed: nil, thoughts: nil, piece_id: 1, created_at: "2016-07-28 10:46:05", updated_at: "2016-07-28 10:46:05">} missing required keys: [:id]
<%= link_to section.name, piece_section_path(section), class: 'section_name' %>
当我 link 到 sections 模型的索引动作时它起作用了,我看到父 "Piece" 的 "show" 动作非常好。
我的第二个问题是当我在查看 Piece 时。我试着点击一个单独的部分(即这个 link:
<%= link_to section.name, piece_sections_path(section), class: 'section_name' %>
)
由于某种原因,我无法显示截面模型的截面和列。
这里是部分控制器:
class SectionsController < ApplicationController
def index
@pieces = Piece.find(params[:piece_id])
@sections = @pieces.sections.find(params[:section_id])
end
def show
@sections = Section.all
#@sections = Piece.sections.find(params[:id])
#@sections = @piece.sections.all
#@sections.piece_id = @pieces.id
end
end
这是截面模型的 index.html.erb 文件:
<h1> Yoyo</h1>
<p>Piece: <%= @pieces.name%></p>
<p> Section: <%= @sections.name%></p>
<p> Broader Context: <%= @sections.broader_context %></p>
这是 routes.rb 文件:
Rails.application.routes.draw do
root to: 'articles#index'
resources :pieces do
resources :sections
end
end
我遇到的问题是:
ActiveRecord::RecordNotFound in SectionsController#index
Couldn't find Section without an ID
Request
Parameters:
{"piece_id"=>"1"}
我不明白的是我怎么没有权限访问版块 ID??我怎么只能访问piece_id??
非常感谢您的帮助,因为我已经坚持了一段时间。
将 piece id 传递给 link 给 helper,因为 piece_section_path
需要两个参数
piece_section GET /pieces/:piece_id/sections/:id(.:format) sections#show
:piece_id
:id
(section_id
)
将您的 link 更改为:
<%= link_to section.name, piece_section_path(@pieces, section), class: 'section_name' %>