Insert on controller action into another controller action - ruby-on-rails

I am trying to add a controller action into another controller actions because I have an index page that lists a bunch of files. On that same page, I have a file upload sheet and I would like to call the document#new controller#method with the Home#index controller method. I tried include, but it gave me a uninitialized constant HomeController::DocumentController error. Any help appreciated.
class HomeController < ApplicationController
def index
if user_signed_in?
#show folders shared by others
#being_shared_folders = [] #current_user.shared_folders_by_others
#show only root folders (which have no parent folders)
#folders = current_user.folders.roots
#show only root files which has no "folder_id"
#documents = current_user.documents.where("folder_id is NULL").order("name desc")
include DocumentController::new
else
redirect_to sign_up_index_path
end
end
end
class DocumentsController < ApplicationController
def new
#document = current_user.documents.build
if params[:folder_id] #if we want to upload a file inside another folder
#current_folder = current_user.folders.find(params[:folder_id])
#document.folder_id = #current_folder.id
end
end
end

You can store actions in a common module, and include that module into whatever controller needs it:
# lib/common_actions.rb
module CommonActions
def index
# whatever
end
end
# app.controllers/home_controller.rb
class HomeController < ApplicationController
include CommonActions
end
# app.controllers/documents_controller.rb
class DocumentsController < ApplicationController
include CommonActions
end

Related

How to create nested files in welcome view rails

I have 2 folders with some views that I want to display on the welcome/main index of my rails app.
Views
Main
Owner
info.html
I can route to the file but the controller for main has no direction on how to get there. I tried
class MainController < ApplicationController
def owner
def info
end
end
But I know this isn't right. What do I need to do?
There are 2 ways you can handle this,
Using namespaces,
# app/controllers/owner/main_controller.rb
module Owner
class MainController < ApplicationController
def info
end
end
end
# app/views/owner/main/info.html
<html>...</html>
Note the change in file structure of view
or with explicit render with name of view
class MainController < ApplicationController
def info
render 'main/owner/info' # Relative path from app/views
end
end

access application_helper in before_action

I have the following in my application_helper.rb file:
module ApplicationHelper
def require_employer_profile_for_employers(page)
if current_user.type == 'Employer'
if current_user.employer_profile
else
flash[:error] = "You must create a profile before accessing #{page}."
redirect_to new_employer_profile_path
end
end
end
end
I try calling it in my projects controller like this:
before_action "require_employer_profile_for_employers('Projects')"
but my server responds with this error:
NoMethodError (undefined method `require_employer_profile_for_employers' for #<ProjectsController:0x007fb741f82e38>):
How do I access the helper in the before_action in the projects controller?
include ApplicationHelper in your ProjectsController:
class ProjectsController < ApplicationController
include ApplicationHelper
# ...
end
Helpers are not directly accessible within a controller as opposed to view layer where they are freely accessible.

How to change index method in my controller to be more DRY

I'm a Rails beginner and I learn that I always must try to be more DRY.
I'm have a comment system associated to my content model, and I load my comment with ajax on page scroll.
In my view I have:
%section.article-comments{'data-url' => content_comments_path(#content)}
and in my routes.rb file I have the route
resources :contents, only: :index do
resources :comments, only: :index
end
My comment controller of course is
def index
#content = Content.find(params[:content_id])
#comments = #content.comments
render ...
end
Now I want to add comments also to videos and gallery.
So I need to add a route for every resource and I need a gallery_index and a video_index.
Content, video and gallery index method in comment controlelr are repeated, and I cannot understand how can I be more DRY.
All your controllers presumably inherit from ApplicationController:
class CommentsController < ApplicationController
If you find yourself with a lot of repetition in any of the controller methods you could define it in ApplicationController instead, with maybe some specific processing in each controller.
For example:
class ApplicationController < ActionController::Base
def index
...some common processing...
specific_index_processing
end
private
def specific_index_processing
# empty method; will be overridden by each controller as required
end
end
class CommentsController < ApplicationController
private
def specific_index_processing
...specific procesing for the comments index method...
end
end
And of course, if one of your controllers needs to be completely different from this common approach you can always just override the entire index method.

Helper method not exposed to controller Rails

Here's the code:
class SyncController < ApplicationController
def get_sync
#passed_ids = params[:ids].split(',')
#users = #passed_ids.collect{|id| User.find(id)}
#add the current user to the list
#users << current_user
#recommendations = get_recommendations(#users)
end
end
module SyncHelper
def get_recommendations(users)
users
end
end
I'm getting a can't find get_recommendations method error...
Your SyncHelper module needs to be included into your SyncController class. You can either add the line
include SyncHelper
in your class definition, or, if SyncHelper lives in the expected app/helpers file, you can use the rails helper method.

How is it possible the class inheritance in namespaces using Ruby on Rails 3?

In my RoR3 application I have a namespace called NS1 so that I have this filesystem structure:
ROOT_RAILS/controllers/
ROOT_RAILS/controllers/application_controller.rb
ROOT_RAILS/controllers/ns/
ROOT_RAILS/controllers/ns/ns_controller.rb
ROOT_RAILS/controllers/ns/profiles_controller.rb
I would like that 'ns_controller.rb' inherits from application controller, so in 'ns_controller.rb' file I have:
class Ns::NsController < ApplicationController
...
end
Is this the right approach? Anyway if I am in this situation...
In ROOT_RAILS/config/routes.rb I have:
namespace "ns" do
resources :profiles
end
#profile is a ActiveRecord:
#profile.find(1).name
=> "Ruby on"
#profile.find(1).surname
=> "Rails"
In application_controller.rb I have:
class ApplicationController < ActionController::Base
#profile = Profile.find(1)
end
In ns_controller.rb I have:
class Ns::NsController < ApplicationController
#name = #profile.name
#surname = #profile.surname
end
... #name and #surname variables are not set. Why?
Unless there's some code you're not showing here, you're trying to set an instance variable in a class body rather than an instance method, which means the variable won't be available in controller actions (which are instance methods).
If you want find method that can be inherited, you could do something like this:
class ApplicationController < ActionController::Base
def load_profile
#profile = Profile.find(params[:id])
end
end
class Ns::NsController < ApplicationController
before_filter :load_profile
def show
# #profile assigned a value in load_profile
end
end

Resources