ruby-on-rails - 编辑命名空间资源

标签 ruby-on-rails routes namespaces

我正在尝试连接一个链接,该链接会将用户从类(class)展示页面(列出一堆单词)带到一个表单以编辑列出的单词之一。我有一个老师命名空间,这样老师有很多课,课有很多词。在使用表单来编辑单词之前,当我将链接添加到将是编辑表单的链接时,类(class)显示页面会中断:ActionController::UrlGenerationError in Teacher::Lessons#show , No 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

老师/类(class)_ 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

老师/类(class)/表演:
<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 路线:
       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

最佳答案

看起来你正在传递一个 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而您只是通过了其中一个(并且对哪一个感到困惑)。

通常你会同时使用类(class)和单词来实例化这条路线,例如:
edit_teacher_lesson_word_path(@lesson, word)

试试那个(或它的变体)让它工作。

关于ruby-on-rails - 编辑命名空间资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649155/

相关文章:

javascript - 如何知道访客来自哪里?

vb.net - 找不到 Microsoft.Win32.TaskScheduler - 我想在 VB.NET 中导入这个命名空间

c++ - 使用声明不能引用类成员

python - Django 2.0 |包含多个 urls conf 命名空间

ruby-on-rails - 避免加载关联关系对象

ruby-on-rails - 为什么在 Rails 中使用 %w[]?

ruby-on-rails - 空哈希参数的 Rails 3 到 4 错​​误数量的参数错误

routes - 可选整数参数的 Nancy 路由

asp.net-mvc-4 - MVC4 路由映射 JavaScript 文件到 Controller

php - 根据数据库结果设置 Yii2 catchAll 路由