Rails 4:ActionController :: UrlGenerationError

时间:2015-08-22 22:36:06

标签: ruby-on-rails ruby-on-rails-4 has-many-through nested-resources nested-routes

我有三种模式:

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

这是我的路线:

resources :users do
  resources :administrations
  resources :calendars
end

-----------

更新

以下是我在终端中运行rake routes时得到的结果:

Prefix Verb   URI Pattern                                        Controller#Action
    user_administrations GET    /users/:user_id/administrations(.:format)          administrations#index
                         POST   /users/:user_id/administrations(.:format)          administrations#create
 new_user_administration GET    /users/:user_id/administrations/new(.:format)      administrations#new
edit_user_administration GET    /users/:user_id/administrations/:id/edit(.:format) administrations#edit
     user_administration GET    /users/:user_id/administrations/:id(.:format)      administrations#show
                         PATCH  /users/:user_id/administrations/:id(.:format)      administrations#update
                         PUT    /users/:user_id/administrations/:id(.:format)      administrations#update
                         DELETE /users/:user_id/administrations/:id(.:format)      administrations#destroy
          user_calendars GET    /users/:user_id/calendars(.:format)                calendars#index
                         POST   /users/:user_id/calendars(.:format)                calendars#create
       new_user_calendar GET    /users/:user_id/calendars/new(.:format)            calendars#new
      edit_user_calendar GET    /users/:user_id/calendars/:id/edit(.:format)       calendars#edit
           user_calendar GET    /users/:user_id/calendars/:id(.:format)            calendars#show
                         PATCH  /users/:user_id/calendars/:id(.:format)            calendars#update
                         PUT    /users/:user_id/calendars/:id(.:format)            calendars#update
                         DELETE /users/:user_id/calendars/:id(.:format)            calendars#destroy
                   users GET    /users(.:format)                                   users#index
                         POST   /users(.:format)                                   users#create
                new_user GET    /users/new(.:format)                               users#new
               edit_user GET    /users/:id/edit(.:format)                          users#edit
                    user GET    /users/:id(.:format)                               users#show
                         PATCH  /users/:id(.:format)                               users#update
                         PUT    /users/:id(.:format)                               users#update
                         DELETE /users/:id(.:format)                               users#destroy
                    root GET    /                                                  static_pages#home

-----------

更新2

这是我的calendars_controller.rb文件:

class CalendarsController < ApplicationController
  before_action :set_calendar, only: [:show, :edit, :update, :destroy]

  # GET /calendars
  # GET /calendars.json
  def index
    @calendars = Calendar.all
  end

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

  # GET /calendars/new
  def new
    @user = User.find(params[:user_id])
    @calendar = @user.calendars.new
  end

  # GET /calendars/1/edit
  def edit
    @user = User.find(params[:user_id])
    @calendar = @user.calendar.find(params[:calendar_id])
  end

  # POST /calendars
  # POST /calendars.json
  def create
    @user = User.find(params[:user_id])
    @calendar = @user.calendars.new(calendar_params)

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

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

  # DELETE /calendars/1
  # DELETE /calendars/1.json
  def destroy
    @calendar.destroy
    respond_to do |format|
      format.html { redirect_to calendars_url, notice: 'Calendar was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def calendar_params
      params.require(:calendar).permit(:name)
    end
end

-----------

http://localhost:3000/users/1/calendars/newhttp://localhost:3000/users/1/calendars/1工作正常。

但是,每当我尝试访问http://localhost:3000/users/1/calendars时,都会收到以下错误:

ActionController::UrlGenerationError in Calendars#index
Showing /Users/TXC/code/rails/calendy/app/views/calendars/index.html.erb where line #17 raised:

No route matches {:action=>"show", :controller=>"calendars", :id=>nil, :user_id=>nil} missing required keys: [:id, :user_id]
Extracted source (around line #17):

      <tr>
        <td><%= calendar.name %></td>
        <td><%= link_to 'Show', user_calendar_path(@user, @calendar) %></td>
        <td><%= link_to 'Edit', edit_user_calendar_path(@user, @calendar) %></td>
        <td><%= link_to 'Destroy', calendar, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>

特别是,我不理解missing required keys: [:id, :user_id]部分,因为我@user中有@calendaruser_calendar_path

显然,我错过了一些东西,但我无法弄清楚是什么。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

问题在于,您正在点击映射到http://localhost:3000/users/1/calendars操作的calendars#index网址,其正确路径为:user_calendars_path且只需要一个参数{{1}因为它会将您带到用户的索引页面,并将显示数据库中的所有用户日历。

但是,在user_id视图中,您有:

index.html.erb

您无法拥有,因为您的控制器中的<td><%= link_to 'Edit', edit_user_calendar_path(@user, @calendar) %></td> 操作未设置index@user。您可以在@calendar中使用此功能,但不能在show.html.erb中使用。所以,这就是你得到这个错误的原因。

简而言之,您不能在index.html.erb中使用:

index.html.erb

因为它已映射到<td><%= link_to 'Show', user_calendar_path(@user, @calendar) %></td> 操作,相应的视图应该是calendars#show但是目前您已将其show.html.erb放入其中!

回答您的上一个问题:

  

特别是,我不明白缺少必需的键:[:id,   :user_id]部分,因为我同时拥有@user和@calendar   user_calendar_path。

是的,您在控制器中定义了index.html.erb@user,并在@calendar方法中show映射到user_calendar_path方法。但是嘿!您是通过与calendars#show操作相对应的index.html.erb来调用此内容的。这就是你所缺少的。希望现在很清楚: - )

最后,差异很微妙。

calendars#index适用于user_calendars_path视图,其中显示了特定用户的所有日历。在这种情况下,您必须在控制器的index操作中设置@user

index适用于user_calendar_path,它会将您带到特定用户的特定日历。在这种情况下,您必须在控制器的show方法中设置@user@calendar

答案 1 :(得分:1)

您尚未在@user方法中设置@calendarcalendars#shownew操作会使您的视图很好,但它不知道如何填充参数voor @user@calendar