gonzalo2000
gonzalo2000

Reputation: 648

editing namespaced resource

I'm trying to hook up a link that will take users from a lesson show page (that lists a bunch of words) to a form to edit one of the listed words. I have a teacher namespace, such that teachers have many lessons and lessons have many words. Before working on a form to edit the word, the lesson show page is breaking when I add the link to what would be the edit form: 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]

What am I missing in regards to setting the edit_teacher_lesson_word link?

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

teacher/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

teacher/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

teacher/lessons/show:

<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

Upvotes: 0

Views: 24

Answers (1)

Taryn East
Taryn East

Reputation: 27747

looks like you're passing a Word where it's expecting a Lesson. see this part of the error message: :lesson_id=>#<Word id: 19,

I'm guessing, but probably this is the wrong line of code:

edit_teacher_lesson_word_path(word)

your route file says this about that route:

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

the : indicates what things you need to pass in order to create a valid route... and here you need both :lesson_id as well as :id and you are only passing one of those (and it's getting confused about which one).

Usually you'd instantiate this route with both lesson and word eg:

edit_teacher_lesson_word_path(@lesson, word)

Try that (or variations on it) to get it to work.

Upvotes: 1

Related Questions