在Ruby on rails上将数据从一个控制器提交到另一个控制器模型

时间:2013-01-17 04:51:00

标签: ruby-on-rails ruby ruby-on-rails-3 redirect routes

我想在这里提交数据

 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 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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

答案 2 :(得分: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 =&gt; 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

了解详情

答案 3 :(得分: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

问题是:

创建方法保存后,您将重定向到@employee_general_information,但此变量指向 EmployeeRegister 模型,不是你的Employee::GeneralInformationsController,因为在创建之后调用rails的默认方法是指向对象的show方法,所以你被重定向到 EmployeeRegister的节目

如果你问它是如何指向 EmployeeRegister

答案是:

再次看到 Employee :: GeneralInformationsController 创建方法中定义的第一行。其@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]))