Rails form_for with nested resources -


i'm having issues routing rails app use new , edit form. routes use username within url.

               lists    /lists(.:format)                         lists#index                      post   /user/:username/lists(.:format)          lists#create             new_list    /user/:username/lists/new(.:format)      lists#new            edit_list    /user/:username/lists/:id/edit(.:format) lists#edit                 list    /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    /                                        static#welcome            show_user    /user/:id(.:format)                      users#show 

my routes.rb-

  'lists', to: 'lists#index', as: :lists, via: 'get'    scope 'user/:username'       resources :lists, except: [:index]   end 

my shared form, use updating , creating-

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

using above settings, can edit lists correctly, if navigate lists#new, following error-

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

if pluralize path in form url: lists_path(@user, @list), new page load, when trying create below error-

no route matches [post] "/lists.test" 

how can fix these routes can use same shared form both edits , 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    # /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 |format|       if @list.save         format.html { redirect_to list_path(@list.user.username, @list.id), notice: 'list created.' }       else         format.html { render :new }       end     end   end     def update     respond_to |format|       if @list.update(list_params)         format.html { redirect_to list_path(@list.user.username, @list.id), notice: 'list updated.' }       else         format.html { render :edit }       end     end   end    def destroy     @list.destroy     respond_to |format|       format.html { redirect_to lists_url, notice: 'list destroyed.' }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_list       @list = list.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def list_params       params.require(:list).permit(:name, :category, :user_id)     end end 

try - change :url value in form_for [[@list], :username => @user.username]

like so:

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

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -