1

私のルートには謎があります:

resources :organizations

root :to => 'users#index'

devise_for :users, :controllers => { :registrations => 'users/registration', :sessions => 'users/session', :confirmations => 'users/confirmation'}

今、私が到達しようとすると、うまくいき/organizations/1ます。

私が試し/organizationsたり、/organizations/new

どちらの場合も同じエラーが発生します。No route matches {:action=>"show", :controller=>"organizations"}

私が要求したことのないパス。そして、どちらのパスが存在しますか。

ルートを傍受し、レール (または工夫) で隠しリダイレクトを実行できるものはありますか?

アップデート

ルートは次のとおりです。

       organizations GET    /organizations(.:format)              organizations#index
                     POST   /organizations(.:format)              organizations#create
    new_organization GET    /organizations/new(.:format)          organizations#new
   edit_organization GET    /organizations/:id/edit(.:format)     organizations#edit
        organization GET    /organizations/:id(.:format)          organizations#show
                     PUT    /organizations/:id(.:format)          organizations#update
                     DELETE /organizations/:id(.:format)          organizations#destroy
                root        /                                     users#index
    new_user_session GET    /users/sign_in(.:format)              users/session#new
        user_session POST   /users/sign_in(.:format)              users/session#create
destroy_user_session DELETE /users/sign_out(.:format)             users/session#destroy
       user_password POST   /users/password(.:format)             devise/passwords#create
   new_user_password GET    /users/password/new(.:format)         devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format)        devise/passwords#edit
                     PUT    /users/password(.:format)             devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)               users/registration#cancel
       user_registration POST   /users(.:format)                      users/registration#create
   new_user_registration GET    /users/sign_up(.:format)              users/registration#new
  edit_user_registration GET    /users/edit(.:format)                 users/registration#edit
                         PUT    /users(.:format)                      users/registration#update
                         DELETE /users(.:format)                      users/registration#destroy
       user_confirmation POST   /users/confirmation(.:format)         users/confirmation#create
   new_user_confirmation GET    /users/confirmation/new(.:format)     users/confirmation#new
                         GET    /users/confirmation(.:format)         users/confirmation#show
             user_unlock POST   /users/unlock(.:format)               devise/unlocks#create
         new_user_unlock GET    /users/unlock/new(.:format)           devise/unlocks#new
                         GET    /users/unlock(.:format)               devise/unlocks#show

アップデート

OrganizationsController と ApplicationController は次のとおりです。

class OrganizationsController < ApplicationController
  before_filter :authenticate_user!

  def index
    new
    render 'organizations/new'
  end

  def new
    @organization = Organization.new
  end
end

class ApplicationController < ActionController::Base
  protect_from_forgery

  def after_sign_in_path_for(resource)
    accounts_path
  end

  rescue_from CanCan::AccessDenied do |exception|
    flash[:error] = exception.message
    redirect_to :back
  end
end

アップデート

これが組織の new.html.erb ビューです

<%= semantic_form_for @organization, :url => organization_path do |f| %>

    <%= f.inputs do %>
        <%=  f.input :country %>
        <%=  f.input :type, :as => :select, :label => t(:g_type), :collection =>     [[t(:g_company),"Company"],[t(:g_person),"Person"]] %>
    <% end %>
    <%= f.actions %>
<% end %>

アップデート

また、ルートが適切に定義されていることも確認しました。次のように見えます。

>> r = Rails.application.routes
#<ActionDispatch::Routing::RouteSet:0x3dbea00>
>> r.recognize_path("/organizations/new")
{:action=>"new", :controller=>"organizations"}
>> r.recognize_path("/organizations/1")
{:action=>"show", :controller=>"organizations", :id=>"1"}
4

2 に答える 2

0

あなたはインデックスを次のように定義しました

def index
  new 
  render 'organizations/new' 
end

レンダリングするファイル名を指定しない場合、method_name のファイルがレンダリングされます。組織/新規を指定したので、新しい組織のフォームを表示したいのですが、新しい組織のフォームにリダイレクトしますか? はいの場合は、追加します

redirect_to new_organization_path and return

組織のリストを表示し、同じページで新しい組織データを受け入れたい場合は、そうすることができます

def new
  @organizations = Organization.all
  @organization = Organization.new
end

そしてあなたの新しい形で

= form_for(@organization, :url => organizations_path, :method => :post) do |f|

それはorganzations_pathでなければなりません

于 2012-10-09T16:15:42.453 に答える