开发者

Routing Sub-Controllers

开发者 https://www.devze.com 2023-01-04 06:56 出处:网络
My route looks like the following: map.namespace(:admin) do |admin| admin.resources :pages end and my controller name looks like the following:

My route looks like the following:

map.namespace(:admin) do |admin|
  admin.resources :pages
end

and my controller name looks like the following:

class Admin::PagesController < ApplicationController

and my new.html.erb file looks like the following:

<% form_for(@page) do |f| %>
    <%= f.error_messages %>
    <p>
        <%= f.label :title %>
        <%= f.text_field :title %>  
    </p>
    <p>
        <%= f.label :body %>
        <%= f.text_area :body %>
    </p>
    <p>
        <%= f.submit "Create" %>
    </p>
<% end %>

<%= link_to 'Back', :action => "index" %>

Yet I keep on getting the following error:

NoMethodError in Admin/pages#new

Showing app/views/admin/pages/new.html.erb where line #1 raised:

undefined method `pages_path' for #<ActionView::Base:0x104528000>
Extracted source (around line #1):

1: <% form_for(@page) do |f| %>
2:  <%= f.error_messages %>
3:  <p>
4:      <%= f.label :title %>

I can't figure out why as I'm assuming the route is correct. If I try other routes then it will work until I try submitting t开发者_JAVA百科he form, then it thinks it should be taking me back to site.com/pages which it shouldn't.

Any ideas?


Your model @page isn't aware that it's being used in a namespace like that. You can use rake routes to see all your routes for your admin namespace. You need to manually change your url path:

<% form_for(@page) do |f| %>

to

<% form_for(@page, :url => admin_pages_path) do |f| %>

Another example for when you're updating a page:

admin_page_path(@page)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号