Ok so I have a helper method in the application controller:
def run_test(test_name)
#computation stuff
render :partial => test_name
end
And I call it like so in views:
<%= run_test("testpartial") %>
and it renders ok with only 1 (although... the render partial seems to be returning an array instead of just the partial content?), but if I put the run_test helper call in the view twice I get a double render error, which shouldn't be happening with partials.
Any ideas?
render in a controller versus render in a view are different methods. The controller eventually calls render on a view, but the controller's render method itself expects to be called only once. It looks like this:
# Check for double render errors and set the content_type after rendering.
def render(*args) #:nodoc:
raise ::AbstractController::DoubleRenderError if response_body
super
self.content_type ||= Mime[formats.first].to_s
response_body
end
Note how it raises if called more than once?
When you call helper_method you give the view a proxy to the controller's version of render, which is not intended to be used in the same way as ActionView's, which is, unlike the controller's, expected to be called repeated to render partials and whatnot.
Looks like in Rails 3.2 this just works:
# application_helper.rb
def render_my_partial
render "my_partial"
end
You could try using render_to_string method in the view helper
render_to_string :partial => test_name, :layout => false
Related
Is it possible to override Rails' render behavior for :html responses? I'd like to always render the same template (ignoring the magic view finding).
I'm writing a single page app, and this seems like it should be possible...basically if it's requested as :json it should render JSON, but if it's requested as :html it should pass the data on to the same view no matter what (where it will be rendered as JSON in the body).
Try to delete the yield part on your application.html.erb, then you will alway get the application.html.erb without any partials.
What if you define one single view and then after every action on every controller you render that view? Like this:
app/controllers/home_controller.rb
class HomeController < ApplicationController
def home
end
end
app/views/home/home.html.erb
<!-- Whatever html code and script tags here -->
app/controllers/another_controller.rb
class AnotherController < ApplicationController
def action
render "home/home"
end
end
You could even define an after_filter
Edit
I tried this and it works. The after filter doesn't seem to work though.
Why not pass the JSON data as an instance variable?
controller
#json_data = whatever_model.to_json
application.html.erb
<script>
<%= #json_data %>
</script>
In my controller I want to render one of the HTML partials into a string variable, then send it with the Pusher and finalize the action with a regular rendering of the JS file (this is AJAX request).
I'm making an AJAX call and my action looks like this:
def create
#my_Object = ...
#html_content = escape_javascript(render_to_string :partial => 'my_partial', :object => #my_object )
Pusher.trigger(
'my-channel',
'my-event',
{ message: #html_content }
)
end
Given my understanding I would expect to see the "create.js" file being rendered at the end of this action, but this is not the case.
If in the "create.js" I put:
alert("test");
I do not see the dialog even though in rails logs I do see that the "create.js" partial is rendered.
When I remove the render_to_string part all works fine and the "create.js" gets properly rendered.
Can you help? What am I missing?
It seems that the fix is to explicitly call
respond_to do |format|
format.js
end
after the render_to_string.
This is strange as without the render_to_string call you do no thave to do that...
I'm just starting with rails.
I'm trying to render partial view in my controller's method, but I'm gettin with brackets.
My appplication_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def inline_error(field, messages_bag)
if msg = messages_bag.errors.full_messages_for(field).first
msg = '<small class="error">'+msg+'</small>'
return msg.html_safe
end
end
helper_method :inline_error
#nevermind on param
def take_flash(type)
render "class/flash", :layout => false
end
helper_method :take_flash
end
my class/_flash.html.rb file:
hello
and I'm gettin
["hello"]
Could anybody help me?
In fact there are two different render methods, one is defined on ActionView, and the other one is defined on ActionController - they slightly differ in what they do. What you need here is render of the view:
def take_flash(type)
view_context.render "class/flash"
end
I think you're confusing the usage of helpers and partials. They are both used to generate segments of view code but you don't need to use both at once.
In your case you can either pass your param to the partial directly, and remove the helper.
Your view:
<%= render 'flash', locals: {my_param: 'blahblah'} %>
Your _flash partial:
Hello <%= blahblah %>
Or you can pass it to the helper and remove the partial.
Your view:
<%= take_flash('blahblah') %>
Your helper method (define this in a helper file, not your controller):
def take_flash(my_param)
"Hello #{blahblah}"
end
Both approaches have the same outcome.
I am currently trying to add some parsing methods to a controller method in a Rails 3 application.
I have a controller action as follows:
def control
#device = Device.find(params[:id])
<do things>
parse_return(#returned_data)
end
and I added a custom method to the controller as below (this method would not have any routes and would only be accessible to controller actions):
def parse_return
<parse data>
end
but this does not appear to allow the parse_return method to be used. Is there somewhere else in the Rails app that I can put re-usable methods?
Thanks!
At a first glance it seems that you fail to render a response. Is it true that control action doesn't have an associated view?
In this case you have to manually call render in your action. For example, to render JSON response you can do this:
def control
# ...
render :json => parse_return(#returned_data),
:content_type => 'application/json',
:layout => false
end
You should include what the errors are.
What happens if you try this?
def parse_return(returned_data)
<parse data>
end
Perhaps the method is not expecting an parameter to be passed along with it.
When try I following code in a controller, the view renders without using the layout
def xyz
render :partial => 'platinum_home', :layout => 'platinum_layout'
end
But If I do the following inside the partial
<% render(:layout => "platinum_layout") do %>
blah blah blah
<% end %>
It works just fine, is the first example not possible using rails?
In your controller at the top add the following:
class SomeController < ApplicationController
layout "platinum_layout", :only => :xyz
Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.
So to use current layout for your just used.
def xyz
render :partial => 'platinum_home', :layout => true
end