【问题标题】:Submit data from one controller to another controller model in Ruby on rails在 Ruby on rails 中将数据从一个控制器提交到另一个控制器模型
【发布时间】:2012-12-31 14:19:52
【问题描述】:

我想在这里提交数据

 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 显示页面。我哪里错了?

【问题讨论】:

  • 您能否显示您的路线,其中Employee::GeneralInformationsController 被映射?
  • 命名空间 :employee do resources :general_informations end
  • 嘿@regmiprem 16 小时前给了你答案,希望对你有帮助!

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


【解决方案1】:

您正在重定向到@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'

【讨论】:

    【解决方案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
    

    【讨论】:

    • 在创建一些数据时重定向到employee_register。因此应该在 create 方法中..
    【解决方案3】:

    当你在 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]
    

    这将重定向到#showEmployee::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了解更多信息

    【讨论】:

    • 谢谢老兄,我会尽力回复
    • 我按照您所说的简单形式进行了更改,它给了我错误,例如 #:0x000000037fe0b0> 的未定义方法 `employee_employee_registers_path' 提取的源代码(在第 1 行附近):1 : 2: 3: 4:
    • 呃,阅读指南我发现另一个问题,您无法为不存在的模型创建资源路线。您正在使用resources :general_informations,请尝试将namespace :employee do resources :general_informations 替换为resources :employee_registers, :controller =&gt; 'employee/general_informations'
    • 你可以编辑我的问题吗?我搞砸了。。谢谢你的回复
    • 它给了我路线错误。路由错误未初始化常量Employee::Employee
    【解决方案4】:

    这里是:

    仔细查看您的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
    

    问题是:

    在 create 方法中保存后,您将重定向到 @employee_general_information 但此变量指向 EmployeeRegister 模型,而不是您的Employee::GeneralInformationsController 并且由于创建后在 rails 中调用的默认方法是指向对象的 show 方法,因此您将被重定向到 EmployeeRegister's show。

    如果你问它是如何指向那​​个 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]))
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签