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

蒂博克莱门特

在我的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

使用此路由:

Rails.application.routes.draw do

  root to: 'pages#home'

  devise_for :users, :path => 'account'

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

end

给出以下路线:

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

最后,这是内容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

我一直遇到类似的错误。

问题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 %>
...

我得到:

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

问题2:当我尝试从edit.html.erb帖子视图更新帖子时:

<h1>Editing Post</h1>

<%= render 'form' %>

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

我得到:

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

问题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 %>

我得到:

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

如果我的错误的解释是正确的,每一次,这个问题似乎是,我不能检索idcalendarpost所属。

换句话说,我无法从子(发布)控制器中检索父(日历)ID。

我相信这是与我的嵌套浅资源以及在视图中建立链接的方式有关的问题。

我不知道如何使这些链接起作用。

如果您能为我提供上述三种情况之一的解决方案,最重要的是推理,那么我绝对可以为其他两种情况提供解决方案。

任何的想法?

蒂博克莱门特

我找到了三个问题中每个问题的解决方案。

问题#1:我替换了:

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

与:

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

在中posts_controller.rb

问题2:我更换了:

<%= link_to 'Back', posts_path %> 

与:

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

edit.html.erb帖子视图中。

问题#3:我替换了:

<%= link_to 'Back', posts_path %>

与:

calendar_path(@post.calendar_id)

show.html.erb帖子视图中

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

路由嵌套控制器-Rails

如何在Rails 4中测试控制器问题

在Rails中前缀路由控制器

如何在Rails 5中路由到具有可变值的特定控制器?

rails-如何将嵌套控制器作为嵌套资源路由的目标

如何从Rails中的控制器获取按钮名称?

如何从 Rails 中的另一个控制器获取帖子的 ID?

在控制器 Rails 中传递 id

Rails 5.2如何在控制器中获取表单数据值

如何在Rails控制器中获取请求引荐来源

如何在控制器Rails中获取电子邮件

在Rails 4中,如何从控制器内的设计用户那里获取数据?

如何在rails的控制器中重构接收webhook

如何在Ruby on Rails中传递控制器参数

如何在rails的控制器中包含用户信息

如何在Rails控制器中修改参数

如何找到在Rails 4中定义的控制器方法?

从Rails 4中的控制器注销

如何在不重复控制器名称的情况下编写Rails 4路由命名空间?

如何在Rails 4应用程序中路由至batman.js(batman-rails)控制器,而不会覆盖所有其他路由?

如何在Rails 4的控制器内部生成的变量中覆盖create动作

如何在Rails 4中为控制器或操作覆盖X-Frame-Options

如何在Rails 4中使用按钮调用控制器方法

Rails 使用控制器操作在 Route.rb 中获取路由名称

Rails 4控制器对fields_for嵌套资源的操作

如何让 Rails 路由器从内容中动态选择控制器?

在Rails 4中路由具有多个单词的控制器

路由错误-通过Rails 4中的按钮触发控制器动作

Rails:创建动态路由、控制器