【问题标题】:Rails nested form redirect issueRails嵌套表单重定向问题
【发布时间】:2012-11-26 20:12:02
【问题描述】:

我正在创建应用程序,注册过程如下:

  1. 用户注册他们的公司(父帐户)并创建他们的 用户帐户(子帐户)以相同的形式。
  2. 用户登录并为其他员工添加其他子帐户。

每个公司帐户都充当连接所有用户帐户的“包装器”。

我已经完成了这项工作,但在用户提交表单后似乎无法让代码重定向回应用程序的根 url。目前,它们正在被转发到公司索引中。

谢谢!

我的两个模型:

class Company < ActiveRecord::Base
  attr_accessible :name, :address1, :address2, :city, :state, :zip, :users_attributes

  has_many :projects, :dependent => :destroy
  has_many :users, :dependent => :destroy

  accepts_nested_attributes_for :users
end


class User < ActiveRecord::Base
  authenticates_with_sorcery!

  attr_accessible :email, :password, :password_confirmation, :first_name, :last_name, :created_on, :company_id

  belongs_to :company
  has_many :comments
  has_many :tasks

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email

end

公司控制人

  def new
    @company = Company.new
    @company.users.build
  end

  def create
    @company = Company.new(params[:company])

    if @company.save
      redirect_to root_url
    else
      render action: "new"
    end
  end

还有形式

<%= simple_form_for(@company) do |f| %>
  <%= f.error_notification %>

  <%= f.simple_fields_for :users do |u| %>
    <%= u.input :first_name %>
    <%= u.input :last_name %>
    <HR>
    <%= u.input :email %>
    <%= u.input :password %>
    <%= u.input :password_confirmation %>
  <% end %>


  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.input :address1 %>
    <%= f.input :address2 %>
    <%= f.input :city %>
    <%= f.input :state %>
    <%= f.input :zip %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

编辑:

我的路线

  resources :password_reset

  get "logout" => "sessions#destroy", :as => "logout"
  get "login" => "sessions#new", :as => "login"
  get "adduser" => "users#new", :as => "adduser"
  resources :users
  resources :sessions

  resources :companies
  get "signup" => "companies#new", :as => "signup"

  resources :comments

  root :to => "projects#index"

  resources :tasks do
    member do
      get :change
    end
  end

  resources :phases

  resources :projects

【问题讨论】:

  • FWIW,我将您的代码复制到了一个测试应用程序中,它对我有用(将我路由回项目/索引作为根 url)。

标签: ruby-on-rails forms redirect nested


【解决方案1】:

只需重定向到“/”路径,而不是使用路由进行快速修复。

公司控制人

def new
  @company = Company.new
  @company.users.build
end

def create
  @company = Company.new(params[:company])

  if @company.save
    redirect_to '/'
  else
    render action: "new"
  end
end

【讨论】:

  • 成功了!仍然想知道为什么我的路由搞砸了,因为重定向到 root_url 正在其他领域工作,但谁知道呢。谢谢!!!
【解决方案2】:

routes.rb 中定义了什么?

可能你忘记把它改成你想要的 root_url 了

【讨论】:

  • root :to => "projects#index"
  • 你能发布你所有的路线吗?
猜你喜欢
  • 1970-01-01
  • 2015-08-26
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
相关资源
最近更新 更多