Hey guys I need your valuable help. In the Ruby on Rails tutorial, at the 10th chapter, the author has 2 controllers in one view. I build a similar app in which I also have 2 controllers in one view, however, when I render a method, which belongs to the second controller, from the first I have problems with the params. (in the tutorial the author does not use any parameters in the action of the other controller)
More specifically I have 2 controllers: UsersController and MicropostsController. Also, in the show.html.haml page of Users, I use both controllers: UsersController for showing user's microposts and MicropostsController in order to allow the user to create a new micropost.
Inside MicropostsController:
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to user_path(current_user)
else
#render text: renderActionInOtherController(UsersController,:show, {:id => 1})
@user = User.find(current_user)
@microposts = @user.microposts.paginate(page: params[:page])
render 'users/show'
end
end
Inside UsersController
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
@micropost = current_user.microposts.build if signed_in?
end
In the app/views/users/show.html.haml
- provide(:title, @user.name)
.users_page
.row
%aside.span4
- if !signed_in?
%section
%h1
= gravatar_for @user
= @user.name
- else
%section
= render 'shared/user_info'
%section
= render 'shared/micropost_form'
.span8
- if @user.microposts.any?
%h3 Microposts (#{@user.microposts.count})
%ol.microposts
= render @microposts
= will_paginate @microposts
So basically my question summarizes as follows:
1)Is it a good practice to have multiple controllers in 1 view? I have found contradicting answers in the net. (actually, I am not even sure if this code remains RESTful)
2) If 1 is yes (or at least it is not a bad practice) could I implement the same thing in a more efficient way? Because the way I see it, every time I render action from another controller, I have to redefine the variables.
3) I found a similar topic in stackoverflow in which one proposes to use this method (which I don't get what it does exactly since I am new in RoR).
def renderActionInOtherController(controller,action,params)
controller.class_eval{
def params=(params); @params = params end
def params; @params end
}
c = controller.new
c.request = @_request
c.response = @_response
c.params = params
c.send(action)
c.response.body
end
If I use this version of create action inside MicropostsController,
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to user_path(current_user)
else
render text: renderActionInOtherController(UsersController,:show, {:id => 1})
#@user = User.find(current_user)
#@microposts = @user.microposts.paginate(page: params[:page])
#render 'users/show'
end
end
when I hit post button I get totally nothing in the browser. Moreover, when I try to view tha users/1 page I get the following error(!!):
undefined method `[]' for nil:NilClass
Any help will be really valuable! If you need any other info please inform me!