Rails partial how to use controller function - ruby-on-rails

With a Rails index view, you can create a matching controller action. For instance a view called workorders.index2.html.erb,can have the following controller action:
class WorkordersController < ApplicationController
def index2
#workorders = Workorder.order("position")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #workorders }
end
end
But, what if you have a partial called _mygroupswos.html.erb. How can I get the partial to use a matching action in the workorders controller?
Thanks for the help!

Rails Controller renders the view automatically based on the naming convention. But you can always override it by asking the action to render a different view explicitly.
def some_action
render :partial => "mygroupswos", :layout => true
end
partials are generally used for sharing view logic. So I would advice you to create a view template and render the partial there.

Related

nocontent returned from controller action that calls helper rendering a partial

I am basically trying to use a helper method that renders a partial in my controller and getting a nocontent errors or 500 errors "no template" depending on the variations I'm trying. If I just run the code in the helper directly from the controller action, all is well. It seems like the rails guides don't touch on this (using a helper method that does the rendering, as opposed to directly from the controller action). Unfortunately, I don't have the luxury right now to refactor it out because the helper is used in other places, and it seems to me if there is a way to use this helper in the controller action also, that's the better option.
Anyone know how to do this? Nothing I am trying is working for me :S
Helper method
def render_call_orders_filters url_or_path_method, query_params = #query_params
render :partial => 'call_orders/call_orders_filters', :locals => {url_or_path_method: url_or_path_method, query_params: query_params}
end
Controller action
# GET /call_order/filters
def filters
respond_to do |format|
format.html {
# This (of course) works...
# render :partial => 'call_orders/call_orders_filters', \
# :locals => { url_or_path_method: method(:call_orders_path), query_params: #query_params }
# This does not
helpers.render_call_orders_filters(method(:call_orders_path))
# This gives me a "Template is missing" 500 error
renter text: helpers.render_call_orders_filters(method(:call_orders_path))
}
end
end
based on your last example code, there is render :partial => that works
I think it could be refactored like this
# The helper...
def orders_filters_param_to_template(url_or_path_method, query_params = {})
{
partial: 'call_orders/call_orders_filters', locals: { url_or_path_method: url_or_path_method,
query_params: query_params }
}
end
def render_call_orders_filters(url_or_path_method, query_params = #query_params)
template_data = orders_filters_param_to_template(url_or_path_method, query_params)
render partial: template_data[:partial],
locals: template_data[:locals]
end
# The controller action...
def filters
respond_to do |format|
format.html do
template_data = helpers.orders_filters_param_to_template(method(:call_orders_path), #query_params)
render partial: template_data[:partial],
locals: template_data[:locals]
end
end
end

how to render different layout & json data in one action of conroller in rails?

I am new to Rails.
I want to render layout(.html.erb file) as well as json data from action of controller.
How can I perform it?
Can I render both of them in single action? how?
This is exactly what respond_to is for. From the Rails Action Controller guide:
class UsersController < ApplicationController
  def index
    #users = User.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render xml: #users}
      format.json { render json: #users}
    end
  end
end

ruby on rails how to render a page without layout and other head field

else
respond_to do |format|
format.html { render "tabelle/show" }
end
end
I want to render the page ...with only the code in that page....not add <head>...layout and <body> field in ruby on rails.
I only want to show the result of code in the page tabelle/show.html.haml
You can do it like this:
format.html { render "tabelle/show", :layout => false }
Controller:
layout false, only: [:method_name]
this is very useful when you using render_to_string
add
:layout => false
Example:
render "tabelle/show", :layout => false
If you do not want to specify the view to use.
Rails is smart enough to know which view template to use based on the Controller Action you're on.
For example, if you're on the show action of the TabellesController you wouldn't need to specify render "tabelle/show" in your Controller Action because Rails will already assume that and will automatically try to render the file in app/views/tabelles/show.html.erb.
So if you're sticking with all of those defaults then you can just use the following to render without the typical layout template:
def show
# Other stuff in your Controller Action.
render layout: false
end
This will render app/views/tabelles/show.html.erb but without the layout template automatically.
Noice.

rails reusing view templates

A rails newbie here
I have 2 actions in my controller 1) index 2) refine_existing.
Both of them show the results in the same format.
How do I reuse the index.html.erb file?
When I try the following, it complains about refine_existing.erb not being present.
def refine_existing
...
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #results }
end
end
my index action looks like this
def index
#some logic to get #results
#set some session variables etc.
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #results }
end
end
Do I have to refactor my index view to contain partials that
a) make the headers
b) render #results
and reuse them?
Even though, both index.html.erb and refine_existing.html.erb will look exactly the same
Is there any way I can say in my refine_existing action to use index.erb view?
thanks in advance
By convention, if you don't specify a template name Rails looks for one matching the action. You can override this by calling render explicitly with the desired template name. The only wrinkle is that the path is relative to TEMPLATE_ROOT, which is normally app/views:
def refine_existing
...
respond_to do |format|
format.html { render :template => "<table_name>/index.html.erb" }
end
end
replacing table_name with the "tablized" form of the model. E.g. if your controller is PostsController, then posts. So your template would then live in app/views/posts/index.html.erb -- if you've customized paths somehow adjust as necessary.

How can I explicitly declare a view from a Rails controller?

I want to explicitly call a view from my controller.
Right now I have:
def some_action
.. do something ...
respond_to do |format|
format.xml
end
end
... then it calls my some_action.xml.builder view. How can I call some other view? Is there a parameter in respond_to I'm missing?
Thanks,
JP
You could do something like the following using render:
respond_to do |format|
format.html { render :template => "weblog/show" }
end
See the Rendering section of the ActionController::Base documentation for the different ways you can control what to render.
You can tell Rails to render a specific view (template) like this:
# Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
render :template => "weblog/show"
# Renders the template with a local variable
render :template => "weblog/show", :locals => {:customer => Customer.new}
Or even simpler since Rails > 3.0:
render "edit"
You can also pass :action, or :controller if that's more convenient.
respond_to do |format|
format.html { render :action => 'show' }
end
You can modify the internal lookup_context of the controller by doing this in your controller
before_filter do
lookup_context.prefixes << 'view_prefix'
end
and the controller will try to load view/view_prefix/show.html when responding to an show request after looking for all the other view prefixes in the list. The default list is typically application and the name of the current controller.
class MagicController
before_filter do
lookup_context.prefixes << 'secondary'
end
def show
# ...
end
end
app.get '/magic/1`
This GET request will look for a view in the following order:
view/application/show.erb
view/magic/show.erb
view/secondary/show.erb
and use the first found view.
Use render
http://api.rubyonrails.com/classes/ActionController/Base.html#M000474

Resources