Rob
Rob

Reputation: 1855

Custom rails routing with has_many not working with form_for

Trying to make a custom route for rails with has_many

This is my model for 'Animes'

has_many :anime_categories, dependent: :destroy

This is the route I have made

 match 'animes/:id/categories/new' => 'anime_categories#new', :via => :get

problem is when i visit /animes/animename/categories/new i'm getting a no method error which is raised from form_for:

NoMethodError in AnimeCategories#new
Showing     

app/views/anime_categories/new.html.erb where line #11 raised:

undefined method `anime_categories_path' for #<#    <Class:0x007fb643147d40>:0x007fb6431365b8>

here is line 11

 <%= form_for(@anime_category) do |f| %>

AnimeCategory new method:

class AnimeCategoriesController < ApplicationController
 def new

@anime = Anime.friendly.find(params[:id])
@anime_category = Anime.friendly.find(@anime.id).anime_categories.new

end 

Cant see what exactly is causing the problem. I have the 'new' method in the anime categories controller so I think its a routing problem.

I would like to keep the url format the way it is. How can i do this?

Thanks

Upvotes: 0

Views: 35

Answers (1)

Triveni Badgujar
Triveni Badgujar

Reputation: 941

You have not mentioned method in form_for, so in your case it will take it by default as post method. And the route you have defined is the :get method.

And on form_for you should specify create method path and not the post method path.

I am not sure about your use case but if you want to redirect to new

<%= form_for(@anime_category, method: :get) do |f| %>

Upvotes: 1

Related Questions