rails: use jbuilder template in various controller methods - ruby-on-rails

Is it possible to reuse jbuilder-template in another controller method? In other words: how to explicitly say controller method to use concrete jbuilder-template?

From Rails guide.
Rendering an Action's Template from Another Controller.
What if you want to render a template from an entirely different
controller from the one that contains the action code? You can also do
that with render, which accepts the full path (relative to app/views)
of the template to render. For example, if you're running code in an
AdminProductsController that lives in app/controllers/admin, you can
render the results of an action to a template in app/views/products
this way
def my_action
# some code here
render "products/show"
end

def my_action
# some code here
render "products/show.json"
end
or
def my_action
# some code here
render "show.json"
end
without the .json it will try to render an html file.

Related

Can I render rails welcome page from my controller?

When I want to show default welcome page by routes.rb,
I can add route like this:
root "rails/welcome#index"
Then, I want to render the same template in my controller out of curiosity.
The template is in rails/railties/lib/rails/templates/rails/welcome/index.html.erb
I thought `render "controller/action" works for my own files,
but it doesn't seem to work for built-in files.
def index
render "rails/welcome/index" # This shows error `Missing template rails/welcome/index`.
end
So, Is there a handy way to render built-in template?
Yes you can render the rails welcome page from your controller by using the Rails::WelcomeController
class WelcomeController < Rails::WelcomeController
end
Don't define the index method in WelcomeController. Rails will pick the index method of Rails::WelcomeController and will render the default welcome page.

Shared resource for partial in Rails

Premise: I'm quite new to Rails. I'm trying to render a partial in the application layout that will have to display some Event objects. The partial will have to be displayed in every page of the application (it's basically a sidebar). I am aware that I should pass a local variable to the partial, like
<%= render partial: "shared/aside", locals: {events: #events} %>
But this will only work if I define #events in every single controller of the application. Is there a way of setting it globally?
It might be worth noting that events might not be the only resource needed to the partial.
You could do a before action in the application controller. The before action would go at the top of the file, the private method at the bottom
class ApplicationController < ActiveController::Base
# Place at top of file
before_action :set_events
...your other code...
# Place at bottom of file
private
def set_events
#events = Event.all
end

Rails: action and views

My question is : do we need a view file for each action in our controller?
(like if we defined a say_hello action in a controller, is it necessary to add say_hello.html.erb in his view directory?
I'll edit this to say it depends (with same content). If you plan on using that controller action as JS or JSON you don't need a view file. if you want one to share in multiple views, the file can contain a shared partial (which can be used in other views). This examples is shown by the generators scaffolding create examples like this. They are helpful if you are learning rails. Not great otherwise.
If you were to share a partial, you could have a partial named _form.html.erb and then inside your say_hello.html.erb file, it would just call:
<%= render 'form' %>
If you want to render JSON or JS files you can respond_to in your action:
respond_to do |format|
format.html # say_hello.html.erb
format.json { render json: #hello } #no file needed
format.js { render js: #hello }
#format.js   {} #do nothing... or use a little javascript in there...
# or have a file named say_hello.js.erb and use your #hello variable
end
Edit:
One last update. Your say_hello.js.erb file can do the anything on another view (if called remotely):
say_hello.js.erb
<% if #hello.attribute == "some value" %>
$('#div_in_another_view').show();
<% else %>
$('#div_in_somewhere_else').hide();
<% end %>
You can do jQuery and anything you want to the view calling it (as long as it's using AJAX).
End edit
Guides are great place to get started. Railscasts.com as well (even though Ryan isn't updating anymore).
Edit: A great example on the different options on the respond_to is on this rails guide regarding javascript
You can just pass javascript straight from that format.js call, or use a file if you need more complicated stuff. You don't need to do anything also. You could just have it return xml or nothing as well, depending on your use case.
No, it is not required. For example, you can render json or xml data from the controller without needed a view at all. This article explains it very well http://guides.rubyonrails.org/layouts_and_rendering.html
No you do not need a view for each action. BUT you do need a view for each action that will reach the end of the method.
If you return anywhere in the action then you are fine. A view is only required when an implicit render is called due to execution reaching the end of the action.
No, it's really up to you and it depends on what the action will actually do. Actions can render different types content types: text, json, html, xml... etc. Here's an example:
def show
render xml: #something
end
This action doesn't have a view, but it'll output an xml when called. It can also render different things based on the format of the call:
def show
respond_to do |format|
format.html do
redirect_to '/'
end
format.json do
render xml: #something
end
end
end
The action may also redirect (again, this one doesn't have a file):
def show
redirect_to '/'
end
At the end, it's really up to the programmer to handle how the action behaves, but if you leave it empty, it'll assume there's a file to render.

Calling methods that render views conditionally in a Rails Controller

I am writing a Ruby on Rails application with a controller called "pages_controller" that is responsible for displaying pages to users. There are 3 different types of pages that can be displayed, and different things have to happen on the back end in each case, so I decided to break the functionality out into 3 methods within the controller. When the user requests a page, the "show" method is called, which figures out whether the page:
1. Belongs to the user
2. Belongs to another user, and can be viewed by the user requesting it
3. Belongs to another user, and cannot be viewed by the user requesting it (unauthorized)
The appropriate method is then called from there to display the page. The code looks something like this:
def show
if (something)
showMine
elsif (something else)
showAnother
else
showUnauthorized
end
end
def showUnauthorized
respond_to do |format|
format.html # showUnauthorized.html.erb
end
end
def showMine
respond_to do |format|
format.html # showMine.html.erb
end
end
def showAnother
respond_to do |format|
format.html # showAnother.html.erb
end
end
I am getting a template missing error because rails wants to render a view when "show" is called, but I do not want any views to be rendered when "show" is called. I simply want "show" to call the correct method from there, and the corresponding view for that method (showMine, showAnother, or showUnauthorized) to be rendered. How can I do this? Or am I going about this the wrong way entirely?
You need to declare these new actions that you have created in the routes file, as they don't belong to the RESTful routes.
I sugest to keep only the show action in your controller and create the IFs in the show view using the render method to include the partials(_showMine.html.erb, showAnother.html.erb, showUnauthorized)
example:
show view:
if (something)
<%= render 'showMine' %>
elsif (something else)
<%= render 'showAnother' %>
else
<%= render 'showUnauthorized' %>
end
I hope it helps...
I basically agree with Samy's comment, but here's some background:
The method that tells Rails what view to use is render. If there's no call to that method in your show method, Rails assumes you have a view called show.xxx.xxx, e.g. show.html.erb, that is supposed to be rendered. Note that it doesn't assume template will be prefixed with show because that's the name of the method. It assumes it will be show because that's the name of the action. The name of the action is passed to the controller as part of the request; it's not simply derived from the name of whatever method has a respond_to block in it.
All the respond_to blocks do is specify different view templates based on the MIME type of the request, but since you never call render, all of those extra methods are still trying to call the show view (show.html.erb in every case), because you never told Rails to render any other view, and the action name is show.
So, instead of the respond_to blocks, just call render [some_view] in each of your other methods.
This might not be the clearest answer, but I'd suggest also reading the following:
http://ryanbigg.com/2009/04/how-rails-works-2-mime-types-respond_to/
It describes what respond_to does, in particular how it keys off the action name to determine what view to render.

Call methods from other controllers in rails

I'm trying to call a method from within another controller and getting a no method error.
So I have two controllers Jobs and Admin, I'm trying to call:
<% #jobs.each do |job| %>
I'm putting this command in the admin contoller's view, within the actual controller file for admin I have:
# GET /jobs
# GET /jobs.xml
# GET /admin
def index
#jobs = Job.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #jobs }
end
Which I thought would pull everything I needed over yet I'm still getting a nomethoderror, so how should I include the jobs controller into the admin one?
from the code you posted, it appears that you are not using a query to get the jobs that you want from the database, eg
#jobs = Job.all
Job.all is an activerecord query that gets all the available records from the database. It sounds like you may be confused about how all the MVC pieces are intended to work together.
First, you should not be sharing methods across controllers, in reality your controllers will have very little code.
Rails will route a request, and look for an action defined in the controller, if it doesn't find one, it will go to the views folder and get the template of that action name there, eg a request to Jobs#index will look for an index action in the controller, and then if it isnt there, look for the views/jobs/index.erb template to render. So if you are serving static data and don't need to look up data, you dont even need an action in your controller. Often though, the action is needed because it is in the controller that you do your database and model lookups, and then pass those instance variables to the view. Keep all database queries and model actions out of the views.
def index
#jobs = Job.all
end
and in the view
#jobs.each do |job| ...
The respond_to block is totally unnecessary unless you want to return a differently formatted template than html, like xml or json for a web service.
You have not defined jobs action in Admin controller. You are loading #jobs in index action of Admin controller. If you want this work, rename index action of the Admin controller to jobs and add that route. One way you could add a route is by adding a line in config/routes.rb.
match '/jobs' => 'admin#jobs'
Read through this.

Resources