简体   繁体   English

Rails 4嵌套浅路由:如何在子控制器中获取父ID?

[英]Rails 4 nested shallow routes: how to get parent id in child controller?

In my Rails 4 app, there are four models: 在我的Rails 4应用程序中,有四种模型:

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
  has_many: :posts
end

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

class Post < ActiveRecord::Base
  belongs_to :calendar
end

With this routing: 有了这个路由:

Rails.application.routes.draw do

  root to: 'pages#home'

  devise_for :users, :path => 'account'

  resources :calendars do
    resources :posts, shallow: true
  end

end

Which gives these routes: 这给出了这些路线:

Prefix Verb   URI Pattern                                 Controller#Action
                   posts GET    /posts(.:format)                            posts#index
                         POST   /posts(.:format)                            posts#create
                new_post GET    /posts/new(.:format)                        posts#new
               edit_post GET    /posts/:id/edit(.:format)                   posts#edit
                    post GET    /posts/:id(.:format)                        posts#show
                         PATCH  /posts/:id(.:format)                        posts#update
                         PUT    /posts/:id(.:format)                        posts#update
                         DELETE /posts/:id(.:format)                        posts#destroy
                    root GET    /                                           pages#home
        new_user_session GET    /account/sign_in(.:format)                  devise/sessions#new
            user_session POST   /account/sign_in(.:format)                  devise/sessions#create
    destroy_user_session DELETE /account/sign_out(.:format)                 devise/sessions#destroy
           user_password POST   /account/password(.:format)                 devise/passwords#create
       new_user_password GET    /account/password/new(.:format)             devise/passwords#new
      edit_user_password GET    /account/password/edit(.:format)            devise/passwords#edit
                         PATCH  /account/password(.:format)                 devise/passwords#update
                         PUT    /account/password(.:format)                 devise/passwords#update
cancel_user_registration GET    /account/cancel(.:format)                   devise/registrations#cancel
       user_registration POST   /account(.:format)                          devise/registrations#create
   new_user_registration GET    /account/sign_up(.:format)                  devise/registrations#new
  edit_user_registration GET    /account/edit(.:format)                     devise/registrations#edit
                         PATCH  /account(.:format)                          devise/registrations#update
                         PUT    /account(.:format)                          devise/registrations#update
                         DELETE /account(.:format)                          devise/registrations#destroy
       user_confirmation POST   /account/confirmation(.:format)             devise/confirmations#create
   new_user_confirmation GET    /account/confirmation/new(.:format)         devise/confirmations#new
                         GET    /account/confirmation(.:format)             devise/confirmations#show
             user_unlock POST   /account/unlock(.:format)                   devise/unlocks#create
         new_user_unlock GET    /account/unlock/new(.:format)               devise/unlocks#new
                         GET    /account/unlock(.:format)                   devise/unlocks#show
          calendar_posts GET    /calendars/:calendar_id/posts(.:format)     posts#index
                         POST   /calendars/:calendar_id/posts(.:format)     posts#create
       new_calendar_post GET    /calendars/:calendar_id/posts/new(.:format) posts#new
                         GET    /posts/:id/edit(.:format)                   posts#edit
                         GET    /posts/:id(.:format)                        posts#show
                         PATCH  /posts/:id(.:format)                        posts#update
                         PUT    /posts/:id(.:format)                        posts#update
                         DELETE /posts/:id(.:format)                        posts#destroy
               calendars GET    /calendars(.:format)                        calendars#index
                         POST   /calendars(.:format)                        calendars#create
            new_calendar GET    /calendars/new(.:format)                    calendars#new
           edit_calendar GET    /calendars/:id/edit(.:format)               calendars#edit
                calendar GET    /calendars/:id(.:format)                    calendars#show
                         PATCH  /calendars/:id(.:format)                    calendars#update
                         PUT    /calendars/:id(.:format)                    calendars#update
                         DELETE /calendars/:id(.:format)                    calendars#destroy

And finally, here is the content of posts_controller.rb : 最后,这是posts_controller.rb的内容:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @calendar = Calendar.find(params[:calendar_id])
    @post = @calendar.posts.create(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to calendar_path(@calendar), notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to calendar_path, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @calendar = Calendar.find(params[:calendar_id])
    @post.destroy
    respond_to do |format|
      format.html { redirect_to calendar_path(@calendar), notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:date, :time, :subject, :format, :copy, :media)
    end
end

I keep running into a similar error. 我一直遇到类似的错误。

Issue #1 : when I try to delete a post from the show.html.erb calendar view: 问题#1 :当我尝试从show.html.erb日历视图中删除帖子时:

<h2><%= @calendar.name %> Calendar</h2>

<h3>Posts</h3>
<% if @calendar.posts.any? %>
    <table>
        <tr>
            <th>Date</th>
            <th>Time</th>
            <th>Subject</th>
            <th>Format</th>
            <th>Copy</th>
            <th>Media</th>
        </tr>
  <% @calendar.posts.each do |post| %>
        <tr>
        <td><%= post.date %></td>
            <td><%= post.time %></td>
            <td><%= post.subject %></td>
            <td><%= post.format %></td>
            <td><%= post.copy %></td>
            <td><%= post.media %></td>
        <td><%= link_to 'View', post %></td>
        <td><%= link_to 'Update', edit_post_path(post) %></td>
        <td><%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        </tr>
    </table>
  <% end %>
...

I get: 我明白了:

ActiveRecord::RecordNotFound in PostsController#destroy
Couldn't find Calendar with 'id'=

Issue #2 : when I try to update a post from the edit.html.erb post view: 问题2 :当我尝试从edit.html.erb帖子视图更新帖子时:

<h1>Editing Post</h1>

<%= render 'form' %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>

I get: 我明白了:

ActiveRecord::RecordNotFound in PostsController#update
Couldn't find Calendar with 'id'=

Issue #3 : when I try to go back to the show.html.erb calendar view from the show.html.erb post view: 问题3 :当我尝试从show.html.erb帖子视图返回show.html.erb日历视图时:

<div>
    <p>Date</p>
    <%= @post.date %>
</div>
<div>
    <p>Time</p>
    <%= @post.time %>
</div>
<div>
    <p>Subject</p>
    <%= @post.subject %>
    </div>
<div>
    <p>Format</p>
    <%= @post.format %>
    </div>
<div>
    <p>Copy</p>
    <%= @post.copy %>
</div>
<div>
    <p>Media</p>
    <%= @post.media %>
</div>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

I get: 我明白了:

ActiveRecord::RecordNotFound in PostsController#show
Couldn't find Calendar with 'id'=

If my interpretation of the errors is correct, every time, the problem seems to be that I cannot retrieve the id of the calendar the post belongs to. 如果我对错误的解释是正确的,那么每次问题似乎都是我无法检索post所属的calendarid

In other words, I cannot retrieve the parent (calendar) id from the child (post) controller. 换句话说,我无法从子(post)控制器中检索父(日历)id。

I believe this is a problem related to my nested shallow resources and the way my links are built in the views. 我相信这是一个与我的嵌套浅层资源以及我在视图中构建链接的方式相关的问题。

I can't figure out how to make these links work. 我无法弄清楚如何使这些链接工作。

If you could provide me with the solution — and most importantly the reasoning — for one of the three situations described above, I would most definitely be able to come up with the solution for the other two. 如果您可以为我提供解决方案 - 最重要的是推理 - 对于上述三种情况之一,我绝对能够为其他两种情况提出解决方案。

Any idea? 任何的想法?

I found a solution to each one of my three issues. 我找到了解决我的三个问题的每个问题。

Issue #1 : I replaced: 问题#1 :我换了:

format.html { redirect_to calendar_path(@calendar), notice: 'Post was successfully destroyed.' }

with: 有:

format.html { redirect_to calendar_path(@post.calendar_id), notice: 'Post was successfully destroyed.' } 

in posts_controller.rb . posts_controller.rb

Issue #2 : I replaced: 问题#2 :我换了:

<%= link_to 'Back', posts_path %> 

with: 有:

<%= link_to 'Back', calendar_path(@post.calendar_id) %>

in the edit.html.erb posts view. edit.html.erb帖子视图中。

Issue #3 : I replaced: 问题3 :我换了:

<%= link_to 'Back', posts_path %>

with: 有:

calendar_path(@post.calendar_id)

in the show.html.erb posts view show.html.erb帖子视图中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 浅层嵌套路由仍在寻找父级ID - Shallow nested routes still looking for id of parent 在Rails 3中嵌套浅路径的资源 - nested resources with shallow routes in rails 3 Rails 5在父级上进行浅层布线,但对子级来说不浅 - Rails 5 shallow routing on parent, but not shallow for child Rails,如何通过浅层嵌套资源中的子对象访问belongs_to(父)对象? - Rails, how to access the belongs_to (parent) object, through child object in the shallow-nested resources? Rails 3.2路由:在嵌套资源中错误地将父资源路由到子资源控制器 - Rails 3.2 routes: incorrectly routing parent resource to child resource controller in nested resources Rails路由的范围为“:locale”和浅层嵌套资源 - Rails routes with scope “:locale” and shallow nested resources 如何验证Rails中子模型中是否存在嵌套属性的父ID - How to validate existence of parent id in child model in Rails for nested attributes Rails与控制器嵌套的路线 - Rails nested routes with controller 如何在Rails中使用默认父级创建嵌套路由? - How to create nested routes with default parent in rails? 如何在Rails中为嵌套路由共享控制器动作? - How to share a controller action for nested routes in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM