Unable to access some routes in polymorphic associations in Rails 5.2ActiveRecord, has_many :through, and Polymorphic Associationsmultiple joins in railsruby on rails has_many relation form validation of childrenassociation in rails 3 associate 1 survey to 1 userRAILS 3.1 - unknown attribute: id on buildRails: includes with polymorphic associationrails 4. how to create actions using relationships between three models?NoMethodError in Users#indexRails Dot in Url with two routes for one controllerLooking for proper way to handle has_many through association in react-redux-rails application

Do universities maintain secret textbooks?

How can I store milk for long periods of time?

New coworker has strange workplace requirements - how should I deal with them?

Create a list of snaking numbers under 50,000

In what language did Túrin converse with Mím?

Can a system of three stars exist?

Coupling two 15 Amp circuit breaker for 20 Amp

Using font to highlight a god's speech in dialogue

Welche normative Autorität hat der Duden? / What's the normative authority of the Duden?

What caused the end of cybernetic implants?

How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?

What is the motivation behind designing a control stick that does not move?

Why haven't the British protested Brexit as ardently as the Hong Kong protesters?

How can I portray a character with no fear of death, without them sounding utterly bored?

Why 50 Ω termination results in less noise than 1 MΩ termination on the scope reading?

Ideas behind the 8.Bd3 line in the 4.Ng5 Two Knights Defense

Resources to learn about firearms?

Could a simple hospital oxygen mask protect from aerosol poison?

Divide Numbers by 0

Heuristic argument for the Riemann Hypothesis

How can I improve my formal definitions?

When you have to wait for a short time

How many possible file types in the output `ls -l` command?

'spazieren' - walking in a silly and affected manner?



Unable to access some routes in polymorphic associations in Rails 5.2


ActiveRecord, has_many :through, and Polymorphic Associationsmultiple joins in railsruby on rails has_many relation form validation of childrenassociation in rails 3 associate 1 survey to 1 userRAILS 3.1 - unknown attribute: id on buildRails: includes with polymorphic associationrails 4. how to create actions using relationships between three models?NoMethodError in Users#indexRails Dot in Url with two routes for one controllerLooking for proper way to handle has_many through association in react-redux-rails application






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I've built a classic Rails API backend that has User, Comment, Sighting. I've set up Comment as Commentable via polymorphic morphic relationship. Incidentally, Sighting is also a join table between User and Animal but Animal is not relevant here.
I am able to create comments via Sighting.first.comments.create() and comment on comments via `Comment.first.comments.create() in Console but when I try to view them in routes, I get not method errors or routing errors. I need to do fetch requests from React so these routes need to work.



My Models:




class Comment < ApplicationRecord

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy
end



class Sighting < ApplicationRecord
has_one_attached :image
belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy

def image_filename
self.image.filename.to_s if self.image.attached?
end

def image_attached?
self.image.attached?
end


end


class User < ApplicationRecord
include Rails.application.routes.url_helpers
has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy

has_secure_password
has_one_attached :avatar

def avatar_filename
self.avatar.filename.to_s if self.avatar.attached?
end

def avatar_attached?
self.avatar.attached?
end



validates :username, uniqueness: true

def attachment_url
if self.attachment.attached?
Rails.application.routes.url_helpers.rails_blob_url(self.attachement, only_path: false)
else
nil
end
end

end


My Controllers:



class Api::V1::CommentsController < ApplicationController
before_action :find_comment, only: [:index, :update, :destroy]
before_action :find_commentable


def index
@comments = Comment.all
render json: @comments
end



def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end


def update
@comment.update(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end




private

def comment_params
params.require(:comment).permit(:body, :likes)
end

def find_comment
@comment = Comment.find(params[:id])
end

def find_commentable
@commentable = Sighting.find_by_id(params[:sighting_id])
if params[:sighting_id]
@comentables = Comment.find_by_id(params[:comment_id])
if params[:comment_id]
end
end
end

end


class Api::V1::SightingsController < ApplicationController
before_action :find_sighting, only: [:update, :show, :destroy]

def index
@sightings = Sighting.all
render json: @sightings
end

def show
render json: @sighting, status: :ok
end




def create
@sighting = Sighting.new(sighting_params)
# @sighting.image.attach(params[:sighting][:image])
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def update

# if curr_user.id == @sighting.user_id
@sighting.update(sighting_params)
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @sighting.user_id

@sighting.delete
render json: "sighting deleted"
else
render json: errors: "You are not authorized to delete"
end
end


private

def sighting_params
params.require(:sighting).permit(:title, :body, :likes, :image, :user_id, :animal_id)
end

def find_sighting
@sighting = Sighting.find(params[:id])
end
end


class Api::V1::UsersController < ApplicationController

before_action :find_user, only: [:update, :show, :avatar_upload, :destroy]


def index
@users = User.all
render json: @users
end


def create

@user = User.new(
username: params[:username],
password: params[:password]
)
if @user.save
encode_token(@user.id)
render json: user: UserSerializer.new(@user), token: ENV['jwt'], status: :ok
else
render json: errors: @user.errors.full_messages
end
end


def show
if @user
if curr_user.id == @user.id
render json: @user
elsif
curr_user.id == @user.id
# @user.avatar.attach(params[:user][:avatar])
render json: @user
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
else
render json: errors: "User not found!"
end

end


def update

if curr_user.id == @user.id
@user.update(user_params)
# @user.avatar.attach(params[:user][:avatar])
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end
end

def avatar_upload
@user.update(user_params)
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @user.id
@user.avatar.purge_later if @user.avatar
@user.delete
render json: "user deleted"
else
render json: errors: "You are not authorized to delete"
end
end

private

def user_params
params.require(:user).permit(:name, :username, :password, :avatar)
end


def find_user
@user = User.find(params[:id])
end

end


my Serializers



class CommentSerializer < ActiveModel::Serializer

attributes :id, :body, :likes, :user_id

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy

def user_name
User.all.find .username
end

end


class SightingSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy


attributes :id, :title, :body, :likes, :image, :created_at, :user_id



def image
rails_blob_path(object.image, only_path: true) if object.image.attached?
end

end


class UserSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

attributes :id, :name, :username, :avatar

has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy


def avatar
rails_blob_path(object.avatar, only_path: true) if object.avatar.attached?
end


end



and My Routes:



Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
post "/rails/active_storage/direct_uploads", to: "direct_uploads#create"

root to: 'sightings#index'

namespace :api do
namespace :v1 do

resources :users, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :animals, only: [:index, :show, :update]

resources :sightings, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :comments, only: [:create, :update, :destroy] do
resources :comments
end

put "/users/avatar_upload/:id", to: "users#avatar_upload"
post "/login", to: "auth#login"
get "/current_user", to: "auth#get_user_from_token"

end
end

end


Here is an error for localhost:9000/api/v1/sightings/17/comments



Started GET "/api/v1/sightings/17/comments" for 127.0.0.1 at 2019-03-27 19:15:03 -0400
Processing by Api::V1::CommentsController#index as HTML
Parameters: "sighting_id"=>"17"
Completed 404 Not Found in 27ms (ActiveRecord: 19.8ms)



ActiveRecord::RecordNotFound (Couldn't find Comment without an ID):

app/controllers/api/v1/comments_controller.rb:42:in `find_comment'



and for localhost:9000/api/v1/sightings



Completed 200 OK in 157ms (Views: 117.3ms | ActiveRecord: 33.9ms)


Started GET "/api/v1/comments" for 127.0.0.1 at 2019-03-27 19:17:07 -0400

ActionController::RoutingError (No route matches [GET] "/api/v1/comments"):










share|improve this question


























  • Could really use some help if anyone if familiar with polymorphic associations.

    – Demian Sims
    Mar 28 at 3:58

















0















I've built a classic Rails API backend that has User, Comment, Sighting. I've set up Comment as Commentable via polymorphic morphic relationship. Incidentally, Sighting is also a join table between User and Animal but Animal is not relevant here.
I am able to create comments via Sighting.first.comments.create() and comment on comments via `Comment.first.comments.create() in Console but when I try to view them in routes, I get not method errors or routing errors. I need to do fetch requests from React so these routes need to work.



My Models:




class Comment < ApplicationRecord

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy
end



class Sighting < ApplicationRecord
has_one_attached :image
belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy

def image_filename
self.image.filename.to_s if self.image.attached?
end

def image_attached?
self.image.attached?
end


end


class User < ApplicationRecord
include Rails.application.routes.url_helpers
has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy

has_secure_password
has_one_attached :avatar

def avatar_filename
self.avatar.filename.to_s if self.avatar.attached?
end

def avatar_attached?
self.avatar.attached?
end



validates :username, uniqueness: true

def attachment_url
if self.attachment.attached?
Rails.application.routes.url_helpers.rails_blob_url(self.attachement, only_path: false)
else
nil
end
end

end


My Controllers:



class Api::V1::CommentsController < ApplicationController
before_action :find_comment, only: [:index, :update, :destroy]
before_action :find_commentable


def index
@comments = Comment.all
render json: @comments
end



def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end


def update
@comment.update(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end




private

def comment_params
params.require(:comment).permit(:body, :likes)
end

def find_comment
@comment = Comment.find(params[:id])
end

def find_commentable
@commentable = Sighting.find_by_id(params[:sighting_id])
if params[:sighting_id]
@comentables = Comment.find_by_id(params[:comment_id])
if params[:comment_id]
end
end
end

end


class Api::V1::SightingsController < ApplicationController
before_action :find_sighting, only: [:update, :show, :destroy]

def index
@sightings = Sighting.all
render json: @sightings
end

def show
render json: @sighting, status: :ok
end




def create
@sighting = Sighting.new(sighting_params)
# @sighting.image.attach(params[:sighting][:image])
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def update

# if curr_user.id == @sighting.user_id
@sighting.update(sighting_params)
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @sighting.user_id

@sighting.delete
render json: "sighting deleted"
else
render json: errors: "You are not authorized to delete"
end
end


private

def sighting_params
params.require(:sighting).permit(:title, :body, :likes, :image, :user_id, :animal_id)
end

def find_sighting
@sighting = Sighting.find(params[:id])
end
end


class Api::V1::UsersController < ApplicationController

before_action :find_user, only: [:update, :show, :avatar_upload, :destroy]


def index
@users = User.all
render json: @users
end


def create

@user = User.new(
username: params[:username],
password: params[:password]
)
if @user.save
encode_token(@user.id)
render json: user: UserSerializer.new(@user), token: ENV['jwt'], status: :ok
else
render json: errors: @user.errors.full_messages
end
end


def show
if @user
if curr_user.id == @user.id
render json: @user
elsif
curr_user.id == @user.id
# @user.avatar.attach(params[:user][:avatar])
render json: @user
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
else
render json: errors: "User not found!"
end

end


def update

if curr_user.id == @user.id
@user.update(user_params)
# @user.avatar.attach(params[:user][:avatar])
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end
end

def avatar_upload
@user.update(user_params)
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @user.id
@user.avatar.purge_later if @user.avatar
@user.delete
render json: "user deleted"
else
render json: errors: "You are not authorized to delete"
end
end

private

def user_params
params.require(:user).permit(:name, :username, :password, :avatar)
end


def find_user
@user = User.find(params[:id])
end

end


my Serializers



class CommentSerializer < ActiveModel::Serializer

attributes :id, :body, :likes, :user_id

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy

def user_name
User.all.find .username
end

end


class SightingSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy


attributes :id, :title, :body, :likes, :image, :created_at, :user_id



def image
rails_blob_path(object.image, only_path: true) if object.image.attached?
end

end


class UserSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

attributes :id, :name, :username, :avatar

has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy


def avatar
rails_blob_path(object.avatar, only_path: true) if object.avatar.attached?
end


end



and My Routes:



Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
post "/rails/active_storage/direct_uploads", to: "direct_uploads#create"

root to: 'sightings#index'

namespace :api do
namespace :v1 do

resources :users, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :animals, only: [:index, :show, :update]

resources :sightings, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :comments, only: [:create, :update, :destroy] do
resources :comments
end

put "/users/avatar_upload/:id", to: "users#avatar_upload"
post "/login", to: "auth#login"
get "/current_user", to: "auth#get_user_from_token"

end
end

end


Here is an error for localhost:9000/api/v1/sightings/17/comments



Started GET "/api/v1/sightings/17/comments" for 127.0.0.1 at 2019-03-27 19:15:03 -0400
Processing by Api::V1::CommentsController#index as HTML
Parameters: "sighting_id"=>"17"
Completed 404 Not Found in 27ms (ActiveRecord: 19.8ms)



ActiveRecord::RecordNotFound (Couldn't find Comment without an ID):

app/controllers/api/v1/comments_controller.rb:42:in `find_comment'



and for localhost:9000/api/v1/sightings



Completed 200 OK in 157ms (Views: 117.3ms | ActiveRecord: 33.9ms)


Started GET "/api/v1/comments" for 127.0.0.1 at 2019-03-27 19:17:07 -0400

ActionController::RoutingError (No route matches [GET] "/api/v1/comments"):










share|improve this question


























  • Could really use some help if anyone if familiar with polymorphic associations.

    – Demian Sims
    Mar 28 at 3:58













0












0








0








I've built a classic Rails API backend that has User, Comment, Sighting. I've set up Comment as Commentable via polymorphic morphic relationship. Incidentally, Sighting is also a join table between User and Animal but Animal is not relevant here.
I am able to create comments via Sighting.first.comments.create() and comment on comments via `Comment.first.comments.create() in Console but when I try to view them in routes, I get not method errors or routing errors. I need to do fetch requests from React so these routes need to work.



My Models:




class Comment < ApplicationRecord

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy
end



class Sighting < ApplicationRecord
has_one_attached :image
belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy

def image_filename
self.image.filename.to_s if self.image.attached?
end

def image_attached?
self.image.attached?
end


end


class User < ApplicationRecord
include Rails.application.routes.url_helpers
has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy

has_secure_password
has_one_attached :avatar

def avatar_filename
self.avatar.filename.to_s if self.avatar.attached?
end

def avatar_attached?
self.avatar.attached?
end



validates :username, uniqueness: true

def attachment_url
if self.attachment.attached?
Rails.application.routes.url_helpers.rails_blob_url(self.attachement, only_path: false)
else
nil
end
end

end


My Controllers:



class Api::V1::CommentsController < ApplicationController
before_action :find_comment, only: [:index, :update, :destroy]
before_action :find_commentable


def index
@comments = Comment.all
render json: @comments
end



def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end


def update
@comment.update(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end




private

def comment_params
params.require(:comment).permit(:body, :likes)
end

def find_comment
@comment = Comment.find(params[:id])
end

def find_commentable
@commentable = Sighting.find_by_id(params[:sighting_id])
if params[:sighting_id]
@comentables = Comment.find_by_id(params[:comment_id])
if params[:comment_id]
end
end
end

end


class Api::V1::SightingsController < ApplicationController
before_action :find_sighting, only: [:update, :show, :destroy]

def index
@sightings = Sighting.all
render json: @sightings
end

def show
render json: @sighting, status: :ok
end




def create
@sighting = Sighting.new(sighting_params)
# @sighting.image.attach(params[:sighting][:image])
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def update

# if curr_user.id == @sighting.user_id
@sighting.update(sighting_params)
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @sighting.user_id

@sighting.delete
render json: "sighting deleted"
else
render json: errors: "You are not authorized to delete"
end
end


private

def sighting_params
params.require(:sighting).permit(:title, :body, :likes, :image, :user_id, :animal_id)
end

def find_sighting
@sighting = Sighting.find(params[:id])
end
end


class Api::V1::UsersController < ApplicationController

before_action :find_user, only: [:update, :show, :avatar_upload, :destroy]


def index
@users = User.all
render json: @users
end


def create

@user = User.new(
username: params[:username],
password: params[:password]
)
if @user.save
encode_token(@user.id)
render json: user: UserSerializer.new(@user), token: ENV['jwt'], status: :ok
else
render json: errors: @user.errors.full_messages
end
end


def show
if @user
if curr_user.id == @user.id
render json: @user
elsif
curr_user.id == @user.id
# @user.avatar.attach(params[:user][:avatar])
render json: @user
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
else
render json: errors: "User not found!"
end

end


def update

if curr_user.id == @user.id
@user.update(user_params)
# @user.avatar.attach(params[:user][:avatar])
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end
end

def avatar_upload
@user.update(user_params)
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @user.id
@user.avatar.purge_later if @user.avatar
@user.delete
render json: "user deleted"
else
render json: errors: "You are not authorized to delete"
end
end

private

def user_params
params.require(:user).permit(:name, :username, :password, :avatar)
end


def find_user
@user = User.find(params[:id])
end

end


my Serializers



class CommentSerializer < ActiveModel::Serializer

attributes :id, :body, :likes, :user_id

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy

def user_name
User.all.find .username
end

end


class SightingSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy


attributes :id, :title, :body, :likes, :image, :created_at, :user_id



def image
rails_blob_path(object.image, only_path: true) if object.image.attached?
end

end


class UserSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

attributes :id, :name, :username, :avatar

has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy


def avatar
rails_blob_path(object.avatar, only_path: true) if object.avatar.attached?
end


end



and My Routes:



Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
post "/rails/active_storage/direct_uploads", to: "direct_uploads#create"

root to: 'sightings#index'

namespace :api do
namespace :v1 do

resources :users, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :animals, only: [:index, :show, :update]

resources :sightings, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :comments, only: [:create, :update, :destroy] do
resources :comments
end

put "/users/avatar_upload/:id", to: "users#avatar_upload"
post "/login", to: "auth#login"
get "/current_user", to: "auth#get_user_from_token"

end
end

end


Here is an error for localhost:9000/api/v1/sightings/17/comments



Started GET "/api/v1/sightings/17/comments" for 127.0.0.1 at 2019-03-27 19:15:03 -0400
Processing by Api::V1::CommentsController#index as HTML
Parameters: "sighting_id"=>"17"
Completed 404 Not Found in 27ms (ActiveRecord: 19.8ms)



ActiveRecord::RecordNotFound (Couldn't find Comment without an ID):

app/controllers/api/v1/comments_controller.rb:42:in `find_comment'



and for localhost:9000/api/v1/sightings



Completed 200 OK in 157ms (Views: 117.3ms | ActiveRecord: 33.9ms)


Started GET "/api/v1/comments" for 127.0.0.1 at 2019-03-27 19:17:07 -0400

ActionController::RoutingError (No route matches [GET] "/api/v1/comments"):










share|improve this question
















I've built a classic Rails API backend that has User, Comment, Sighting. I've set up Comment as Commentable via polymorphic morphic relationship. Incidentally, Sighting is also a join table between User and Animal but Animal is not relevant here.
I am able to create comments via Sighting.first.comments.create() and comment on comments via `Comment.first.comments.create() in Console but when I try to view them in routes, I get not method errors or routing errors. I need to do fetch requests from React so these routes need to work.



My Models:




class Comment < ApplicationRecord

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy
end



class Sighting < ApplicationRecord
has_one_attached :image
belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy

def image_filename
self.image.filename.to_s if self.image.attached?
end

def image_attached?
self.image.attached?
end


end


class User < ApplicationRecord
include Rails.application.routes.url_helpers
has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy

has_secure_password
has_one_attached :avatar

def avatar_filename
self.avatar.filename.to_s if self.avatar.attached?
end

def avatar_attached?
self.avatar.attached?
end



validates :username, uniqueness: true

def attachment_url
if self.attachment.attached?
Rails.application.routes.url_helpers.rails_blob_url(self.attachement, only_path: false)
else
nil
end
end

end


My Controllers:



class Api::V1::CommentsController < ApplicationController
before_action :find_comment, only: [:index, :update, :destroy]
before_action :find_commentable


def index
@comments = Comment.all
render json: @comments
end



def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end


def update
@comment.update(comment_params)
if @comment.save
render json: @comment, status: :accepted
else
render json: errors: @comment.errors.full_messages , status: :unprocessible_entity
end
end




private

def comment_params
params.require(:comment).permit(:body, :likes)
end

def find_comment
@comment = Comment.find(params[:id])
end

def find_commentable
@commentable = Sighting.find_by_id(params[:sighting_id])
if params[:sighting_id]
@comentables = Comment.find_by_id(params[:comment_id])
if params[:comment_id]
end
end
end

end


class Api::V1::SightingsController < ApplicationController
before_action :find_sighting, only: [:update, :show, :destroy]

def index
@sightings = Sighting.all
render json: @sightings
end

def show
render json: @sighting, status: :ok
end




def create
@sighting = Sighting.new(sighting_params)
# @sighting.image.attach(params[:sighting][:image])
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def update

# if curr_user.id == @sighting.user_id
@sighting.update(sighting_params)
if @sighting.save
render json: @sighting, status: :accepted
else
render json: errors: @sighting.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @sighting.user_id

@sighting.delete
render json: "sighting deleted"
else
render json: errors: "You are not authorized to delete"
end
end


private

def sighting_params
params.require(:sighting).permit(:title, :body, :likes, :image, :user_id, :animal_id)
end

def find_sighting
@sighting = Sighting.find(params[:id])
end
end


class Api::V1::UsersController < ApplicationController

before_action :find_user, only: [:update, :show, :avatar_upload, :destroy]


def index
@users = User.all
render json: @users
end


def create

@user = User.new(
username: params[:username],
password: params[:password]
)
if @user.save
encode_token(@user.id)
render json: user: UserSerializer.new(@user), token: ENV['jwt'], status: :ok
else
render json: errors: @user.errors.full_messages
end
end


def show
if @user
if curr_user.id == @user.id
render json: @user
elsif
curr_user.id == @user.id
# @user.avatar.attach(params[:user][:avatar])
render json: @user
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
else
render json: errors: "User not found!"
end

end


def update

if curr_user.id == @user.id
@user.update(user_params)
# @user.avatar.attach(params[:user][:avatar])
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end
end

def avatar_upload
@user.update(user_params)
if @user.save
render json: @user, status: :accepted
else
render json: errors: @user.errors.full_messages , status: :unprocessible_entity
end
end


def destroy
if curr_user.id == @user.id
@user.avatar.purge_later if @user.avatar
@user.delete
render json: "user deleted"
else
render json: errors: "You are not authorized to delete"
end
end

private

def user_params
params.require(:user).permit(:name, :username, :password, :avatar)
end


def find_user
@user = User.find(params[:id])
end

end


my Serializers



class CommentSerializer < ActiveModel::Serializer

attributes :id, :body, :likes, :user_id

belongs_to :commentable, :polymorphic => true
has_many :comments, as: :commentable, dependent: :destroy

def user_name
User.all.find .username
end

end


class SightingSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

belongs_to :user
belongs_to :animal
has_many :comments, :as => :commentable, dependent: :destroy


attributes :id, :title, :body, :likes, :image, :created_at, :user_id



def image
rails_blob_path(object.image, only_path: true) if object.image.attached?
end

end


class UserSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

attributes :id, :name, :username, :avatar

has_many :sightings
has_many :animals, through: :sightings
has_many :comments, :as => :commentable, dependent: :destroy


def avatar
rails_blob_path(object.avatar, only_path: true) if object.avatar.attached?
end


end



and My Routes:



Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
post "/rails/active_storage/direct_uploads", to: "direct_uploads#create"

root to: 'sightings#index'

namespace :api do
namespace :v1 do

resources :users, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :animals, only: [:index, :show, :update]

resources :sightings, only: [:index, :show, :create, :update, :destroy] do
resources :comments
end

resources :comments, only: [:create, :update, :destroy] do
resources :comments
end

put "/users/avatar_upload/:id", to: "users#avatar_upload"
post "/login", to: "auth#login"
get "/current_user", to: "auth#get_user_from_token"

end
end

end


Here is an error for localhost:9000/api/v1/sightings/17/comments



Started GET "/api/v1/sightings/17/comments" for 127.0.0.1 at 2019-03-27 19:15:03 -0400
Processing by Api::V1::CommentsController#index as HTML
Parameters: "sighting_id"=>"17"
Completed 404 Not Found in 27ms (ActiveRecord: 19.8ms)



ActiveRecord::RecordNotFound (Couldn't find Comment without an ID):

app/controllers/api/v1/comments_controller.rb:42:in `find_comment'



and for localhost:9000/api/v1/sightings



Completed 200 OK in 157ms (Views: 117.3ms | ActiveRecord: 33.9ms)


Started GET "/api/v1/comments" for 127.0.0.1 at 2019-03-27 19:17:07 -0400

ActionController::RoutingError (No route matches [GET] "/api/v1/comments"):







ruby-on-rails activerecord polymorphic-associations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 7:19









Flimzy

44.4k13 gold badges73 silver badges111 bronze badges




44.4k13 gold badges73 silver badges111 bronze badges










asked Mar 27 at 23:19









Demian SimsDemian Sims

751 silver badge10 bronze badges




751 silver badge10 bronze badges















  • Could really use some help if anyone if familiar with polymorphic associations.

    – Demian Sims
    Mar 28 at 3:58

















  • Could really use some help if anyone if familiar with polymorphic associations.

    – Demian Sims
    Mar 28 at 3:58
















Could really use some help if anyone if familiar with polymorphic associations.

– Demian Sims
Mar 28 at 3:58





Could really use some help if anyone if familiar with polymorphic associations.

– Demian Sims
Mar 28 at 3:58












1 Answer
1






active

oldest

votes


















1















First Error



/api/v1/sightings/17/comments is /api/v1/sightings/:sighting_id/comments



That route has only params[:sighting_id].



It calls before_action :find_comment before running index action and find_comment needs params[:id]



But Api::V1::CommentsController#index does not have params[:id]



So Rails would emit to 'Couldn't find Comment without an ID'



I think that you don't need call a before_action when request to index



Remove :index to only condition



before_action :find_comment, only: [:update, :destroy]


Second Error



 resources :comments, only: [:create, :update, :destroy] do
resources :comments
end


this routes does not have :index



Add :index to only condition



 resources :comments, only: [:index, :create, :update, :destroy] do





share|improve this answer

























  • That was it, thank!

    – Demian Sims
    Mar 28 at 13:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387941%2funable-to-access-some-routes-in-polymorphic-associations-in-rails-5-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















First Error



/api/v1/sightings/17/comments is /api/v1/sightings/:sighting_id/comments



That route has only params[:sighting_id].



It calls before_action :find_comment before running index action and find_comment needs params[:id]



But Api::V1::CommentsController#index does not have params[:id]



So Rails would emit to 'Couldn't find Comment without an ID'



I think that you don't need call a before_action when request to index



Remove :index to only condition



before_action :find_comment, only: [:update, :destroy]


Second Error



 resources :comments, only: [:create, :update, :destroy] do
resources :comments
end


this routes does not have :index



Add :index to only condition



 resources :comments, only: [:index, :create, :update, :destroy] do





share|improve this answer

























  • That was it, thank!

    – Demian Sims
    Mar 28 at 13:38















1















First Error



/api/v1/sightings/17/comments is /api/v1/sightings/:sighting_id/comments



That route has only params[:sighting_id].



It calls before_action :find_comment before running index action and find_comment needs params[:id]



But Api::V1::CommentsController#index does not have params[:id]



So Rails would emit to 'Couldn't find Comment without an ID'



I think that you don't need call a before_action when request to index



Remove :index to only condition



before_action :find_comment, only: [:update, :destroy]


Second Error



 resources :comments, only: [:create, :update, :destroy] do
resources :comments
end


this routes does not have :index



Add :index to only condition



 resources :comments, only: [:index, :create, :update, :destroy] do





share|improve this answer

























  • That was it, thank!

    – Demian Sims
    Mar 28 at 13:38













1














1










1









First Error



/api/v1/sightings/17/comments is /api/v1/sightings/:sighting_id/comments



That route has only params[:sighting_id].



It calls before_action :find_comment before running index action and find_comment needs params[:id]



But Api::V1::CommentsController#index does not have params[:id]



So Rails would emit to 'Couldn't find Comment without an ID'



I think that you don't need call a before_action when request to index



Remove :index to only condition



before_action :find_comment, only: [:update, :destroy]


Second Error



 resources :comments, only: [:create, :update, :destroy] do
resources :comments
end


this routes does not have :index



Add :index to only condition



 resources :comments, only: [:index, :create, :update, :destroy] do





share|improve this answer













First Error



/api/v1/sightings/17/comments is /api/v1/sightings/:sighting_id/comments



That route has only params[:sighting_id].



It calls before_action :find_comment before running index action and find_comment needs params[:id]



But Api::V1::CommentsController#index does not have params[:id]



So Rails would emit to 'Couldn't find Comment without an ID'



I think that you don't need call a before_action when request to index



Remove :index to only condition



before_action :find_comment, only: [:update, :destroy]


Second Error



 resources :comments, only: [:create, :update, :destroy] do
resources :comments
end


this routes does not have :index



Add :index to only condition



 resources :comments, only: [:index, :create, :update, :destroy] do






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 10:28









ogelacinycogelacinyc

6996 silver badges20 bronze badges




6996 silver badges20 bronze badges















  • That was it, thank!

    – Demian Sims
    Mar 28 at 13:38

















  • That was it, thank!

    – Demian Sims
    Mar 28 at 13:38
















That was it, thank!

– Demian Sims
Mar 28 at 13:38





That was it, thank!

– Demian Sims
Mar 28 at 13:38








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387941%2funable-to-access-some-routes-in-polymorphic-associations-in-rails-5-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript