编辑命名空间资源

时间:2016-06-06 03:30:36

标签: ruby-on-rails routes namespaces

我试图连接一个链接,该链接将用户从课程显示页面(列出一堆单词)到表单来编辑列出的单词之一。我有一个教师命名空间,教师有许多课程和课程有很多单词。在使用表单编辑单词之前,当我将链接添加到编辑表单时,课程表页面就会中断:ActionController::UrlGenerationError in Teacher::Lessons#showNo route matches {:action=>"edit", :controller=>"teacher/words", :id=>nil, :lesson_id=>#<Word id: 19, term: "la casa", reference: "house", lesson_id: 6, created_at: "2016-06-05 23:19:19", updated_at: "2016-06-05 23:19:19", image: "house.jpeg", sound: "test.mp3">} missing required keys: [:id]

在设置edit_teacher_lesson_word链接时我缺少什么?

Rails.application.routes.draw do
  devise_for :users
  root 'static_pages#index'
  resources :lessons, only: [:index, :show]
  resources :words, only: [:show]
  namespace :teacher do
    resources :lessons, only: [:show, :new, :edit, :create, :update] do 
      resources :words, only: [:new, :edit, :create, :update]
    end
  end
end

教师/ words_controller:

class Teacher::WordsController < ApplicationController
  before_action :authenticate_user!
  before_action :require_authorized_for_current_lesson

  def new
    @word = Word.new
  end

  def edit
    @word = Word.find(params[:id])
  end

  def create
    @word = current_lesson.words.create(word_params)
    if @word.valid?
      redirect_to teacher_lesson_path(current_lesson)
    else
      render :new, status: :unprocessable_entity
    end
  end

  private

    def require_authorized_for_current_lesson
      if current_lesson.user != current_user
        render text: 'Unauthorized', status: :unauthorized
      end
    end

    helper_method :current_lesson
    def current_lesson
      @current_lesson ||= Lesson.find(params[:lesson_id])
    end

    def word_params
      params.require(:word).permit(:term, :reference, :image, :sound)
    end
end

教师/ lessons_controller:

class Teacher::LessonsController < ApplicationController
  before_action :authenticate_user!
  before_action :require_authorized_for_current_lesson, only: [:show, :edit, :update]

  def show
    @lesson = Lesson.find(params[:id])
  end

  def new
    @lesson = Lesson.new
  end

  def edit
    @lesson = Lesson.find(params[:id])
  end

  def create
    @lesson = current_user.lessons.create(lesson_params)
    if @lesson.valid?
      redirect_to teacher_lesson_path(@lesson)
    else
      render :new, status: :unprocessable_entity
    end
  end

  def update
    current_lesson.update_attributes(lesson_params)
    redirect_to teacher_lesson_path(current_lesson)
  end

  private
    def require_authorized_for_current_lesson
      if current_lesson.user != current_user
        render text: "Unauthorized", status: :unauthorized
      end
    end

    def current_lesson
      @current_lesson ||= Lesson.find(params[:id])
    end

    def lesson_params
      params.require(:lesson).permit(:title, :description, :subject, :difficulty)
    end
end

教师/经验/显示:

<div class ="booyah-box col-xs-10 col-xs-offset-1">
  <br>
  <p class="text-center"><%= @lesson.description %></p>
  <br>
  <div class="text-center">
    <%= link_to 'Edit Lesson', edit_teacher_lesson_path(@lesson), class: 'btn btn-primary' %>
    <%= link_to 'Student View', lesson_path(@lesson), class: 'btn btn-warning' %>
    <%= link_to 'My Lessons', '#', class: 'btn btn-success' %>
    <%= link_to 'All Lessons', lessons_path, class: 'btn btn-info' %>
  </div>

  <hr>

  <h3>Vocabulary in This Lesson</h3>

  <%= link_to 'Add word', new_teacher_lesson_word_path(@lesson), class: 'btn btn-primary btn-lg pull-right' %>

  <ul>
    <% @lesson.words.each do |word| %>
      <li>
        <b><%= word.term %></b> means <i><%= word.reference %></i>
        <%= link_to 'Edit', edit_teacher_lesson_word_path(word), class: 'btn btn-primary' %>
      </li>
    <% end %>
  </ul>
</div>

rake routes:

       new_user_session GET    /users/sign_in(.:format)                             devise/sessions#new
            user_session POST   /users/sign_in(.:format)                             devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                            devise/sessions#destroy
           user_password POST   /users/password(.:format)                            devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                        devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                       devise/passwords#edit
                         PATCH  /users/password(.:format)                            devise/passwords#update
                         PUT    /users/password(.:format)                            devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                              devise/registrations#cancel
       user_registration POST   /users(.:format)                                     devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                             devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                                devise/registrations#edit
                         PATCH  /users(.:format)                                     devise/registrations#update
                         PUT    /users(.:format)                                     devise/registrations#update
                         DELETE /users(.:format)                                     devise/registrations#destroy
                    root GET    /                                                    static_pages#index
                 lessons GET    /lessons(.:format)                                   lessons#index
                  lesson GET    /lessons/:id(.:format)                               lessons#show
                    word GET    /words/:id(.:format)                                 words#show
    teacher_lesson_words POST   /teacher/lessons/:lesson_id/words(.:format)          teacher/words#create
 new_teacher_lesson_word GET    /teacher/lessons/:lesson_id/words/new(.:format)      teacher/words#new
edit_teacher_lesson_word GET    /teacher/lessons/:lesson_id/words/:id/edit(.:format) teacher/words#edit
     teacher_lesson_word PATCH  /teacher/lessons/:lesson_id/words/:id(.:format)      teacher/words#update
                         PUT    /teacher/lessons/:lesson_id/words/:id(.:format)      teacher/words#update
         teacher_lessons POST   /teacher/lessons(.:format)                           teacher/lessons#create
      new_teacher_lesson GET    /teacher/lessons/new(.:format)                       teacher/lessons#new
     edit_teacher_lesson GET    /teacher/lessons/:id/edit(.:format)                  teacher/lessons#edit
          teacher_lesson GET    /teacher/lessons/:id(.:format)                       teacher/lessons#show
                         PATCH  /teacher/lessons/:id(.:format)                       teacher/lessons#update
                         PUT    /teacher/lessons/:id(.:format)                       teacher/lessons#update

1 个答案:

答案 0 :(得分:1)

看起来你正在通过Word预期Lesson的地方。请参阅错误消息的这一部分::lesson_id=>#<Word id: 19,

我在猜测,但这可能是错误的代码行:

edit_teacher_lesson_word_path(word)

您的路线文件说明了这条路线:

edit_teacher_lesson_word GET    /teacher/lessons/:lesson_id/words/:id/edit(.:format)

:表示您需要传递哪些内容才能创建有效路线...此处您需要:lesson_id以及:id并且您只需要传递一个那些(并且它对哪一个感到困惑)。

通常您使用 一词来实例化此路线,例如:

edit_teacher_lesson_word_path(@lesson, word)

尝试(或其变体)以使其发挥作用。