Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Name space route going somewhere else?

name space routes routing

  • Please log in to reply
4 replies to this topic

#1 Vell

Vell

    Dispatcher

  • Members
  • 45 posts
  • LocationWashington, DC

Posted 26 August 2013 - 09:22 PM

Hey guys,

 

I am in my rails 4 app and have noticed that my routing is starting to act a bit weird. Granted I noticed this after I started using bootstrap but I'm even noticing now that in order to get things to work I can't use the

<%= render partial: 'form %>

Instead I have to physically split the edit and new views into their own full fledge view just so I can set the url: routing properly. Of course I don't like this at all. But now I even that is not working when I am attempting to get a view to submit to a namespaced controller.

 

Here is the snippet of my routes file

  resources :blogs do
    collection do
      get 'show_comment_form'
    end
  end

namespace :owner do
    resources :blogs do
      collection do
        get 'show_hide_search_form'
        get 'show_comment_form'
      end
    end
end

Here is my new form in views/owners/blogs

<%= form_for @blog, url: owner_blogs_path, :html => { :class => 'form-horizontal' }, method: :post do |f| %>
<%= render partial: 'new_edit_fields', locals: {f: f} %>

  <div class="form-actions">
    <%= f.submit nil, :class => 'btn btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                owner_blogs_path, :class => 'btn' %>
  </div>
<% end %>

Here is my error:

Unknown action
The action 'create' could not be found for BlogsController

And this is true, I removed the create action in that controller because that is for the public who should not be creating them. The question is, why is it going to that controller when I specified the owner_blogs_path with a method of :post.

 

Here is all the routing I for blogs resource in the owner namespace. It appears to be correct to me unless I am missing something.

owner_blogs GET    /owner/blogs(.:format)                                owner/blogs#index
                                           POST   /owner/blogs(.:format)                                owner/blogs#create
owner_blog GET    /owner/blogs/:id(.:format)                            owner/blogs#show
                                           PATCH  /owner/blogs/:id(.:format)                            owner/blogs#update
                                           PUT    /owner/blogs/:id(.:format)                            owner/blogs#update
                                           DELETE /owner/blogs/:id(.:format)                            owner/blogs#destroy

Any help would be lovely in trying to figure out what I (or bootstrap) might have messed up.



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 27 August 2013 - 05:24 AM

This seems to me to be working as intended.

 

Your line with 

<%= form_for @blog, url: owner_blogs_path, :html => { :class => 'form-horizontal' }, method: :post do |f| %>

specifies that you'd like to use the owners_blogs_path with the POST method. Looking in the routes output, you only have one (of course) route that matches, namely 

    POST    /owner/blogs(.:format)    owner/blogs#create

See the #create at the end, this means it points to the create method. If you do not want this default behaviour, you need to overwrite it in the routes.rb file.


Blog: http://ohm.sh | Twitter: madsohm


#3 Vell

Vell

    Dispatcher

  • Members
  • 45 posts
  • LocationWashington, DC

Posted 27 August 2013 - 01:08 PM

Ohm, I completely agree without. But the weird issue is that when I am inspecting my form in firebug the action is pointing to '/blogs' instead of '/owners/blogs'. This is what is baffling me. The form is showing the correct thing in the template but when I render it in the browser, it points to the incorrect controller and to an action that doesn't exist.



#4 Vell

Vell

    Dispatcher

  • Members
  • 45 posts
  • LocationWashington, DC

Posted 28 August 2013 - 08:18 PM   Best Answer

I guess the only think I can think of is that it has something to do with the instance variable I am setting in the controller for the view. thats the only correlation I can get. Because I am  calling my instance variable @blog instead of @owner_blog it is assuming that i want to be taken to BlogController#create or BlogController#edit instead of Owner::BlogController#create or Owner::BlogController. So in this case, I have to actually create 2 separate views where I have to place the correct method (put or post) in the form or change my instance variable to be @owner_blog.

 

Now that I realize this it makes sense. I didn't realize it till I actually had to create another controller that is in the owner namespace and saw that it was set with @owner in the instance variable name. I guess it would have helped if I had mentioned that I copied the original controllers into a namspace and added Owner:: to the controller name. Should have known better :)



#5 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 01 September 2013 - 09:43 PM

http://icebergist.co...ing-scaffolding

It's an old post but I still use the principles


Programming is just about problem solving!






Also tagged with one or more of these keywords: name space, routes, routing

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users