【问题标题】:editing namespaced resource编辑命名空间资源
【发布时间】:2016-06-06 03:30:35
【问题描述】:

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

耙路线:

       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

【问题讨论】:

    标签: ruby-on-rails routes namespaces


    【解决方案1】:

    看起来您正在传递一个 Word,而它期待的是 Lesson。查看这部分错误信息::lesson_id=&gt;#&lt;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 并且您只通过其中一个(而且它正在不知道是哪一个)。

    通常你会用 both 课程 and 单词来实例化这条路线,例如:

    edit_teacher_lesson_word_path(@lesson, word)
    

    尝试一下(或它的变体)让它发挥作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2011-07-06
      • 1970-01-01
      相关资源
      最近更新 更多