ruby - Rails 4 form helper fails on nested routes -


i've been banging head on issue 2 hours now. makes no sense me. i'm trying to write simple form helper 3 level nested. (i'm aware 3 level nests not recommended, roll me here second.)

my routes.rb excerpt looks this:

resources :vendors, path: '', except: [:new, :create]    resources :item_links     resources :messages   end    resources :projects     resources :designs   end  end 

now when render following, errors out:

<%= form_for [@vendor, @item_link, message.new], url: vendor_item_link_messages_path, method: :post, remote: true |f| %>  no route matches {:action=>"index", :controller=>"messages", :vendor_id=>"thearborist", :project_id=>"4fad5bd7", :id=>"77ec58b5"} missing required keys: [:item_link_id] 

i'm specifying url here because vendor class , itemlink class both have sub-classes (sti) , if don't specify url, end qualified route don't want.

based on error, ignoring method param specified entirely because trying access "index" action instead of "create" action. secondly, inserting project_id param equation reason makes no sense. in either params specified or url path listed reference project.

furthermore, decided go specific , list out routes specfic sub-classes:

resources :vendor_arborists   resources :item_link_standards     resources :messages   end end 

this still blows same sort of error

<%= form_for [@vendor, @item_link, message.new], url: vendor_arborist_item_link_standard_messages_path, method: :post, remote: true |f| %>  no route matches {:action=>"index", :controller=>"messages", :vendor_id=>"thearborist", :project_id=>"4fad5bd7", :id=>"77ec58b5"} missing required keys: [:vendor_arborist_id, :item_link_standard_id] 

only when remove url param entirely start working:

<%= form_for [@vendor, @item_link, message.new], method: :post, remote: true |f| %> 

to add mystery, if isn't screwed enough, second set of routes specified above work fine or without url specified , or without subclasses specified.

    <%= form_for [@vendor, @project, design.new], url: vendor_project_designs_path, method: :post, remote: true |f| %> 

so i'm confused going wrong. models basic, these simple rails 101-esque routes. i'll post other details if needs them debug this, don't know else look. hell doing wrong???

update:

so after continuing testing, seems works well. suppose can move forward this, doesn't explain why works in example above design, fails in examples message. overall, seems in rails 4 causing these route helpers screwed up, because these examples behaved fine in rails 3.2 i'm migrating on from.

credit to: rails 4 shallow routes resource form submission not working

<%= form_for message.new, url: vendor_item_link_messages_path(@vendor, @item_link), method: :post, remote: true |f| %> 

additional reading:
rails 4 [best practices] nested resources , shallow: true

the controller method

this method rendering view contains busted form helper. tried nulling out @project param in case form helper somehow automagically accessing it, setting null doesn't seem have impact.

def show   @vendor = vendor.find_by_subdomain( params[:vendor_id] )   @project = @vendor.projects.find( params[:project_id] )   @item_link = @project.item_links.find( params[:id] )    @item_link.increment!(:views)    respond_to |format|     format.html   end end 

rake routes output

    vendor_item_link_messages      /:vendor_id/item_links/:item_link_id/messages(.:format)                                                              item_links/messages#index                               post     /:vendor_id/item_links/:item_link_id/messages(.:format)                                                              item_links/messages#create  new_vendor_item_link_message      /:vendor_id/item_links/:item_link_id/messages/new(.:format)                                                          item_links/messages#new edit_vendor_item_link_message      /:vendor_id/item_links/:item_link_id/messages/:id/edit(.:format)                                                     item_links/messages#edit      vendor_item_link_message      /:vendor_id/item_links/:item_link_id/messages/:id(.:format)                                                          item_links/messages#show                               patch    /:vendor_id/item_links/:item_link_id/messages/:id(.:format)                                                          item_links/messages#update                               put      /:vendor_id/item_links/:item_link_id/messages/:id(.:format)                                                          item_links/messages#update                               delete   /:vendor_id/item_links/:item_link_id/messages/:id(.:format)                                                          item_links/messages#destroy 


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -