Reputation: 735
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?
Upvotes: 3
Views: 4073
Reputation: 2784
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'
Upvotes: 4
Reputation: 23354
Here it goes:
See your Employee::GeneralInformationsController
carefully -
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
The problem is:
In the create method after save, you are redirecting to @employee_general_information
but this variable is pointing to the EmployeeRegister model and not your Employee::GeneralInformationsController
and since after creation the default method calling in rails is show
method of the pointing object so you are being redirected to the EmployeeRegister's show.
If you ask how it's pointing to that EmployeeRegister ?
The answer is:
Again see your first line defined in create method of Employee::GeneralInformationsController. Its @employee_general_information = EmployeeRegister.new(params[:employee_general_information])
In the above code, you are creating the parameters for EmployeeRegister
model and then saving it to its corresponding table. By rails convention, the model to which you saves the data, the corresponding controller's show method is called by redirecting to it.
Hence the outcome.
Solution:
To redirect to the Employee::GeneralInformationsController show method, you have to explicitly define the root to the show method of the controller.
Check the path by running rake routes
However it's probably be like:
redirect_to(employee_general_information_path(params[:employee_general_information]))
Upvotes: 2
Reputation: 1367
When you what to redirect to a specific controller in rails 3, you need to use named routes or a array to build the route, look:
# Here is the routes that you need
namespace :employee do
resources :employee_registers, :controller => 'employee/general_informations'
end
So to solve your problem you need to replace all yours
redirect_to @employee_general_information
By
redirect_to [:employee, @employee_general_information]
this will redirect to #show
action at Employee::GeneralInformationsController
One more thing, in your form you need to specify the controller to! If you use only simple_form_for(@employee_general_information)
this form will send the data to EmployeeRegistersController
because Rails map the class of the object to a controller with same name (model EmployeeRegister
=> EmployeeRegistersController
).
To solve this you need to use this at your form
<%= simple_form_for([:employee, @employee_general_information]) do |f| %>
...
The final result should be:
# 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
And the form should be
<%= 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 %>
Read more about this at http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
Upvotes: 2
Reputation: 67
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
Upvotes: 2