3

在这里我想提交数据

 class Employee::GeneralInformationsController < ApplicationController
to class EmployeeRegistersController < ApplicationController

employee_register.rb 模型

在这里,我想从 Employee::GeneralInformationsController 中的 EmployeeRegister 模型中获取所有数据,我的 Employee::GeneralInformation 控制器是这样的

class Employee::GeneralInformationsController < ApplicationController
  def index
    @employee_general_informations = EmployeeRegister.all
  end

  def show
    @employee_general_information = EmployeeRegister.find(params[:id])
  end

  def new
    @employee_general_information = EmployeeRegister.new
  end

  def edit
    @employee_general_information = EmployeeRegister.find(params[:id])
  end


  def create
    @employee_general_information = EmployeeRegister.new(params[:employee_general_information])

    respond_to do |format|
      if @employee_general_information.save
        format.html { redirect_to @employee_general_information, notice: 'General information was successfully updated.' }
        format.json { render json: @employee_general_information, status: :created, location: @employee_general_information }
      else
        format.html { render action: "new" }
        format.json { render json: @employee_general_information.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @employee_general_information = EmployeeRegister.find(params[:id])

    respond_to do |format|
      if @employee_general_information.update_attributes(params[:employee_general_information])
        format.html { redirect_to @employee_general_information, notice: 'General information was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @employee_general_information.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @employee_general_information = EmployeeRegister.find(params[:id])
    @employee_general_information.destroy

    respond_to do |format|
      format.html { redirect_to employee_general_informations_url }
      format.json { head :no_content }
    end
  end
end

我的表格是这样的

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

  <div class="form-inputs">
    <%= f.input :first_name %>
  </div>

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

问题是数据从 Employee::GeneralInformationsController 保存到 EmployeeRegisters 模型后,它重定向到员工注册显示页面。但它应该重定向到 Employee::GeneralInformations 显示页面。我哪里错了?

4

4 回答 4

4

您正在重定向到@employee_general_information,这是一个 EmployeeRegister 模型....因此它将转到 EmployeeRegister#show...

如果要重定向到另一个控制器,请使用路由或哈希:

使用以下路线:

namespace :employee do
  resources :general_informations
end

然后:

redirect_to employee_general_information_path

或使用哈希:

redirect_to :controller => 'employee/general_informations', :action => 'show'
于 2013-01-24T23:01:07.203 回答
2

当您要重定向到 rails 3 中的特定控制器时,您需要使用命名路由或数组来构建路由,请查看:

# Here is the routes that you need
namespace :employee do
  resources :employee_registers, :controller => 'employee/general_informations'
end

因此,要解决您的问题,您需要更换所有问题

redirect_to @employee_general_information

经过

redirect_to [:employee, @employee_general_information]

这将重定向到#show行动Employee::GeneralInformationsController

还有一件事,在您的表单中,您需要指定控制器!如果您只使用simple_form_for(@employee_general_information)这种形式,则将数据发送到,EmployeeRegistersController因为 Rails 将对象的类映射到具有相同名称 ( model EmployeeRegister=> EmployeeRegistersController) 的控制器。

要解决此问题,您需要在表单中使用它

<%= simple_form_for([:employee, @employee_general_information]) do |f| %>
...

最终结果应该是:

# Employee::GeneralInformationsController
def create
  @employee_general_information = EmployeeRegister.new(params[:employee_general_information])

  if @employee_general_information.save
    redirect_to [:employee, @employee_general_information],
                notice: 'General information was successfully updated.'
  else
    render action: "new"
  end
end

形式应该是

<%= simple_form_for([:employee, @employee_general_information]) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :first_name %>
  </div>

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

在http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing了解更多信息

于 2013-01-20T17:17:11.737 回答
2

它是这样的:

Employee::GeneralInformationsController仔细看你——

def create
    @employee_general_information = EmployeeRegister.new(params[:employee_general_information])

    respond_to do |format|
      if @employee_general_information.save
        format.html { redirect_to @employee_general_information, notice: 'General information was successfully updated.' }
        format.json { render json: @employee_general_information, status: :created, location: @employee_general_information }
      else
        format.html { render action: "new" }
        format.json { render json: @employee_general_information.errors, status: :unprocessable_entity }
      end
    end
  end

问题是:

save 之后的create方法中,您将重定向到 但此变量指向EmployeeRegister模型而不是您的,并且由于创建后在 rails 中调用的默认方法是指向对象的方法,因此您将被重定向到EmployeeRegister 的 show@employee_general_informationEmployee::GeneralInformationsControllershow

如果你问它是如何指向那​​个 EmployeeRegister的?

答案是:

再次查看在Employee::GeneralInformationsController的create方法中定义的第一行。它的@employee_general_information = EmployeeRegister.new(params[:employee_general_information])

在上面的代码中,您正在为EmployeeRegister模型创建参数,然后将其保存到相应的表中。按照 Rails 约定,您保存数据的模型,通过重定向到它来调用相应控制器的show方法。因此结果。

解决方案:

要重定向到Employee::GeneralInformationsController show 方法,您必须明确定义控制器的 show 方法的根。

通过运行rake routes检查路径

然而它可能是这样的:

 redirect_to(employee_general_information_path(params[:employee_general_information]))
于 2013-01-25T12:19:37.103 回答
2

在您的 respond_to 块中,尝试删除 format.html 中的 @employee_general_information,例如

def update
@employee_general_information = EmployeeRegister.find(params[:id])

respond_to do |format|
  if @employee_general_information.update_attributes(params[:employee_general_information])

    format.html ###goes to the default view for this action
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @employee_general_information.errors, status: :unprocessable_entity }
  end
 end
end
于 2013-01-17T08:31:36.967 回答