Kyle H
Kyle H

Reputation: 3303

Rails form_for with nested resources

I'm having issues with routing a rails app to use a new and edit form. The routes use the username within the URL.

               lists GET    /lists(.:format)                         lists#index
                     POST   /user/:username/lists(.:format)          lists#create
            new_list GET    /user/:username/lists/new(.:format)      lists#new
           edit_list GET    /user/:username/lists/:id/edit(.:format) lists#edit
                list GET    /user/:username/lists/:id(.:format)      lists#show
                     PATCH  /user/:username/lists/:id(.:format)      lists#update
                     PUT    /user/:username/lists/:id(.:format)      lists#update
                     DELETE /user/:username/lists/:id(.:format)      lists#destroy
                root GET    /                                        static#welcome
           show_user GET    /user/:id(.:format)                      users#show

My routes.rb-

  get 'lists', to: 'lists#index', as: :lists, via: 'get'

  scope 'user/:username' do
      resources :lists, except: [:index]
  end

My shared form, for use with updating and creating-

<%= form_for [@user, @list], url: list_path(@user, @list) do |f| %>

Using the above settings, I can edit lists correctly, but if I navigate to lists#new, I get the following error-

No route matches {:action=>"show", :controller=>"lists", :id=>nil, :username=>"test"} missing required keys: [:id]

If I pluralize the path in my form to be url: lists_path(@user, @list), then the new page will now load, but when trying to create I get the below error-

No route matches [POST] "/lists.test"

How can I fix these routes so I can use the same shared form for both edits and creating?

Adding controller code-

class ListsController < ApplicationController
  before_action :set_list, only: [:show, :edit, :update, :destroy]

  def index
      @lists = List.all
  end

  def show
  end

  # GET /lists/new
  def new
    @list = List.new
    @user = User.find_by(username: params[:username])
  end

  def edit
    @user = User.find_by(username: params[:username])
  end


  def create
    @list = current_user.lists.new(list_params)

    respond_to do |format|
      if @list.save
        format.html { redirect_to list_path(@list.user.username, @list.id), notice: 'List was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end


  def update
    respond_to do |format|
      if @list.update(list_params)
        format.html { redirect_to list_path(@list.user.username, @list.id), notice: 'List was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  def destroy
    @list.destroy
    respond_to do |format|
      format.html { redirect_to lists_url, notice: 'List was successfully destroyed.' }
    end
  end

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

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

Upvotes: 1

Views: 1540

Answers (1)

Mark Swardstrom
Mark Swardstrom

Reputation: 18130

Try this - Change the :url value in the form_for to [[@list], :username => @user.username]

Like so:

<%= form_for [@user, @list], url: [[@list], :username => @user.username] do |f| %>

Upvotes: 1

Related Questions