I have home_controller and there are [:index, :process_img] actions within.
I need to get the whole html source of :index action from the action :process_img. Need to access that code in controller.
class HomeController < ActionController::Base
def index
end
def process_img
index_html_code = "the html source of index action should be here"
end
end
How can I achieve that? Thanks in advance!
You can use render_to_string (renders according to the same rules as render, but returns the result in a string instead of sending it as the response body to the browser):
render_to_string :index
Although I think render_to_string is an idiomatic option here's a way to do it which would work in plain ruby as well:
ERB.new(File.read "app/views/home/index.html.erb").result binding
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>
I'm trying to develop an app where I only want one controller, and use anything in the url as an argument to that controller.
I've read that normally the url is parsed as GET controller/action, as in example.com/controller/action/params, but I would like to use a specific controller and a specific action.
Is there any way essentially to parse a url as example.com/params, always handled by the same action and controller?
There are a couple of examples of this in the Rails guide on routing. The file config/routes.rb is where you set up the mapping from URL's to controllers.
One specific example is this:
match '/:username' => 'users#show'
Any request that doesn't match further up will get routed to the UsersController's show method, with the string passed in the params hash as the value of the username key.
Yes, there is. In your config/routes.rb file define the route:
match "catchall/*page", :to => "catchall#respond"
Then in your controller:
class CatchallController < ApplicationController
def respond
end
end
I think this is bad idea, but you can do this, using render action:
def index
if self.class.instance_methods(false).include? params[:page].to_sym
render action: params[:page]
else
#error_404
end
end
I have started to learn rails and javascript.
How i canmake properly ajax request to rails controller with jquery?
I write in js file
$.get('http://localhost:3000/take/show',{},function(s){
alert(s);
},'text');
in controller:
class FirstController < ApplicationController
def show
render :json => "Hi!"
end
end
but instead i see only blank alert dialog. What i have done wrong? In all tutorials i see thatthe URL in $.get should be like this "take/show" but it would not work in my case, so why?
or perhaps a work around?
Basically, my goal is, once the action has been completed, on my ApplicationController after_filter method, I want to modify/show something on the template with a javascript call that gets sent to the user's browser.
I'm okay with other solutions, need not be using the after_filter if there is no way you can do that in after_filter.
So a scenarion would be, after the controller action/all actions/methods have been processed, I'd do this:
document.getElementById("#errors").innerHTML = "Some error".
Thanks in advance.
If you want to render something for all actions then put this in your layouts/application.html.erb file:
<%= javascript_tag "document.getElementById('#errors').innerHTML = '#{#error_message}'" unless #error_message.blank? %>
Then in ApplicationContoller:
def after_filter
#error_message = "Some error"
end
I have written a simple jquery response in my rails 3 controller, I just want to test to see the result this returns (not automated testing).
My code looks like this:
class AppointmentsController < ApplicationController
def jsonlist
#appointments = Appointment.find(:all)
render :json => #appointments
end
Can I access this from a URL like: http://localhost:3000/appointments/jsonlist?
When I try, I get this error:
Couldn't find Appointment with
ID=jsonlist
The error you're getting does not appear to come from that action, it appears to be a conflict in your routes for whatever you have defined as your show action. Try defining your route for jsonlist before you define your route for show.
to debug a json response from your controller
render json: JSON.pretty_generate(JSON.parse(#appointments.to_json))