sam0101
sam0101

Reputation: 375

No route matches {:action=>"show", :controller=>"scoo.....missing required keys: [:id]

I have a partial form for create and update...for the new it render the partial but when going for the edit one I keep getting this error

ActionController::UrlGenerationError in Scoopes#edit
no route matches {:action=>"show", :controller=>"scoopes", :id=>nil, :user_name=>#<Scoope id: 11, name: "asdsada", user_id: 3, created_at: "2016-08-07 20:37:52", updated_at: "2016-08-07 20:37:52">} missing required keys: [:id]

I search for an answer from old questions but no one solved my problem...the form is for creating scoopes....

the assosiaction between scoope and users is :

scoope belong_to user
and user has_many scoopes

here is my scoope controller:

class ScoopesController < ApplicationController
  before_action :authenticate_user!, except: [:show]
  before_action :set_scoope, only: [:show, :edit, :update, :destroy, :index]
  before_action :owned_scoope, only: [:edit, :update, :destroy]

  def index
    @scoopes = @user.scoopes.all
  end

  def show
    @scoope = @user.scoopes.find(params[:id])
  end

  def new
    @scoope = current_user.scoopes.build
  end

  def create
    @scoope = current_user.scoopes.build(scoope_params)
    if @scoope.save
        redirect_to scoope_path(current_user.user_name, @scoope)
    else
        render 'new'
    end
  end

  def edit
    @scoope = @user.scoopes.find(params[:id])
  end

  def update
    @scoope = @user.scoopes.find(params[:id])
    if @scoope.update(scoope_params)
        redirect_to scoope_path(@user, @scoope)
    else
        render 'edit'
    end
  end


  def destroy
    @scoope = @user.scoopes.find(params[:id])
    @scoope.destroy
    redirect_to scoopes_path
  end

  private

  def scoope_params
    params.require(:scoope).permit(:name)
  end

  def set_scoope
    @user = User.find_by(user_name: params[:user_name])
  end
  def owned_scoope
    unless @user == current_user
        flash[:danger] = "this scope dont belong to you"
        redirect_to root_path
    end
  end
end

here is my partial form(I think maybe the problem somehow related to the edit path because when I try yo replace scoope with edit_scoope_path then it render the form under the edit page..but it will not solve my whale problem because it is a partial):

<div class="row">
  <div class="col-md-5 formm">
    <%= render 'shared/errors', obj: @scoope %>
    <div class="well">
      <%= form_for @scoope do |f| %>
        <div class="form-group">
          <%= f.label :name %><br/>
          <%= f.text_area :name, rows: 6, class: 'form-control' %>
        </div>
        <div class="form-group">
          <%= f.submit class: 'btn btn-primary'  %> <%= link_to "Back", :back, class: "btn btn-danger" unless current_page?(scoopes_path) %>
        </div>
      <% end %>
    </div>
  </div>
</div>

here is my routes for scoopes:

               scoopes GET    /:user_name/scoopes(.:format)               scoopes#index
                     POST   /:user_name/scoopes(.:format)               scoopes#create
          new_scoope GET    /:user_name/scoopes/new(.:format)           scoopes#new
         edit_scoope GET    /:user_name/scoopes/:id/edit(.:format)      scoopes#edit
              scoope GET    /:user_name/scoopes/:id(.:format)           scoopes#show
                     PATCH  /:user_name/scoopes/:id(.:format)           scoopes#update
                     PUT    /:user_name/scoopes/:id(.:format)           scoopes#update
                     DELETE /:user_name/scoopes/:id(.:format)           scoopes#destroy

my scoope table:

create_table "scoopes", force: :cascade do |t|
  t.string   "name"
  t.integer  "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

Routes:

devise_for :users, :controllers => { :registrations => "user/registrations" }
root "posts#index" 
scope '/:user_name' do
 resources :scoopes
end
get ':user_name', to: 'profiles#show', as: :profile  
get ':user_name/edit', to: 'profiles#edit', as: :edit_profile  
patch ':user_name/edit', to: 'profiles#update', as: :update_profile  
................

Upvotes: 1

Views: 3587

Answers (2)

MarsAtomic
MarsAtomic

Reputation: 10673

Your call is failing because you're working with a nested route, as defined by the scoope belongs to user relationship.

In routes.rb, you can set up ReSTful routing between user and scoope like so:

resources :users do
  resources :scoopes
end

This notation should give you the nested route you need:

edit_user_scoope_path

To organize the nested object under user_name, try:

scope "/:user_name" do
  resources :posts
end

This should give you a route like this:

edit_scoope GET /:user_name/scoopes/:id/edit(.:format) scoopes#edit

Upvotes: 1

monsur
monsur

Reputation: 601

Problem lies with your routes.rb file. You can add nested routes like below:

resources :users do
  resources :scoopes
end 

that will output urls like below.From this output you can clearly see that the path will be user_scoope_path(current_user, @scoope) .

                                                                                                                                             scoopes#index
                                                            POST     /users/:user_id/scoopes(.:format)                                          scoopes#create
                                            new_user_scoope GET      /users/:user_id/scoopes/new(.:format)                                      scoopes#new
                                           edit_user_scoope GET      /users/:user_id/scoopes/:id/edit(.:format)                                 scoopes#edit
                                                user_scoope GET      /users/:user_id/scoopes/:id(.:format)                                      scoopes#show
                                                            PATCH    /users/:user_id/scoopes/:id(.:format)                                      scoopes#update
                                                            PUT      /users/:user_id/scoopes/:id(.:format)                                      scoopes#update
                                                            DELETE   /users/:user_id/scoopes/:id(.:format)                                      scoopes#destroy

Hope that helps.

Upvotes: 0

Related Questions