Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit data from one controller to another controller model in Ruby on rails

Here i want to submit data from

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

employee_register.rb model

Here i want to fetch all data from EmployeeRegister model in Employee::GeneralInformationsController my controller of Employee::GeneralInformation is like this

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

my form is like this

<%= 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 %>

Problem is after data is save from Employee::GeneralInformationsController to EmployeeRegisters model it redirects to employee register show page. But it should redirect to Employee::GeneralInformations show page. Where i am wrong?

like image 759
regmiprem Avatar asked Jul 15 '26 10:07

regmiprem


2 Answers

You are redirecting to @employee_general_information which is a EmployeeRegister model.... therefore it WILL go to EmployeeRegister#show...

If you want to redirect to another controller, use a route or a hash:

With the following route:

namespace :employee do
  resources :general_informations
end

Then:

redirect_to employee_general_information_path

or with Hash:

redirect_to :controller => 'employee/general_informations', :action => 'show'
like image 179
omarvelous Avatar answered Jul 20 '26 06:07

omarvelous


In your respond_to block try removing the @employee_general_information in format.html, like

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
like image 23
Binvij Avatar answered Jul 20 '26 06:07

Binvij



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!