For generating PDF from HTML, i need to fill a variable with output from another controller action output (HTML). Is there any elegant way, how to get this HTML?
Thanks
You can use:
def print
output = render_to_string(:action => :index)
end
in your controller.
You may abstract the code in that action into a shared method which would be called within your pdf-generating action.
After calling the shared method, you would get the html page content like this:
pdf_content = ERB.new(File.read("/path/to/that/erb.file")).result
Related
In my jobsController, I have a method named getEmployee(). This method renders view named employee.gsp.
render(view : "employee")
When my view is displayed, the url is generated as given below.
http://localhost:8080/test/jobs/getEmployee
Now in this URL I want to append a parameter pagination=false. So my new url should look like:
http://localhost:8080/test/jobs/getEmployee?pagination=false.
How can I do this? Is there any way to append parameters in the generated URL from the controller method getEmployee?
there is a trick to do so, by redirecting to getEmployee action with params.pagination = false
redirect action:'getEmployee', params:[pagination:false]
for example
assume I want 'x' parameter in my /cont/checkGet?x=false like this, so I will redirect from 'check' action to 'checkGet' action with x params
def check(){
redirect action:"checkGet",params: [x:false]
}
def checkGet(){
render "Anshul"
}
hope this helps, Thanks
I'm having some trouble realising how the helper methods should be used in views. For example, take these parts of code:
Mycontrollers_helper.rb
module MycontrollersHelper
def destroy_everything
Model.destroy_all
redirect_to root_path
end
end
How should it be used in the view then ? Let's say adding the method to a button in the view:
<%= button_to 'Destroy all', destroy_everything, method => :post %>
Is just writing a method in the helper.rb file enough or does it require some additional lines in the controller it refers to ? Is this even the correct syntax for something like this ?
Helpers in rails actually view helpers. So they are meant to provide some help to render your views.
If you want to delete something, and then redirect to some action, just use a controller action for that.
I think you are taking about view helper, which you want to call from your view template.
You can call your view helper with the name of the method.
Calling destroy_everything will works fine if this helper is included in your controller.
Update:
If you write your helper method in application helper then you don't need to worry about load/ include the helper.
I have a helper with a method named search_form like this:
module Admin::BaseHelper
def search_form(*args)
# my great code here
end
end
To call this method in my HAML code, I can do this:
= search_form
= search_form()
= search_form(param1: "value1", param2: "value2"...)
My problem is with this first call. When I do this in any HAML file, it renders my helper. Except if my file name is _search_form.html.haml. Is that case, it returns nil.
If I put a raise error in the helper, I notice that my method isn't being called, but I am not able to find what is being called and why.
If I use the syntax on the second and third lines, it works as expected by calling my helper method.
So my question is: is this standard Rails behavior or a bug?
By default, Rails will look for a local variable with the same name as your partial, which may conflict with existing method names.
One way to get around this is to simply redefine the method inside your partial:
<% search_form = self.search_form %>
# Rest of the partial's code
I have a controller method say
def my_method
#gibbon = <data needed>
render :something
end
and i have a URL say
http://exampleforme.com/example1
which returns a certain text, how to get this text inside my controller method from the URL?
I am using rails 2.8
You could use Net::HTTP to get that URL as a string:
#gibbon = Net::HTTP.get(URI.parse("http://exampleforme.com/example1"))
See the API docs for more information.
Note: that this could impact your response time if that URL is slow to respond.
can you show me your route file ?
but I think if you want "example1" text in url you can call params[:id] in your controller
#text = params[:id]
I am building my first app with ROR and stumbled upon a couple of problems due to my understanding of the MVC
I have a page to add a new item, and this works fine, rails magically hooks it up to the items controller and somehow by magic it knows to look in the method 'new' as the page is called new.
But this layer is confusing me, as i need to now create a different version of new, same functionality but with a different look so to use a different layout to application.html.erb
So i attempt to create a copy of new.html.erb and create bookmarklet.html.erb - both contain exactly the same code: a link to a form. but of course bookmarklet will error on me because it does not have that link in the controller - how do i 'wire' up bookmarklet so that i can call the new method and so that it can behave in a similar way to the identical new.html.erb
Also, how can i tell the new bookmarklet.html.erb to ignore the application.html.erb and get its layout from another file?
thanks in advance
The magic happens in the routes. Rails uses something called RESTful routes, which is taking HTTP verbs and assigning standard actions to it. the new action is a GET request in HTTP speak, and if you are using scaffolding or following REST, will have the ruby call to build a new object in the controller, as an instance variable so you can use it in your view.
You have to tell rails routes that you want to BREAK this arrangement and to let /items/bookmarklet be used in the controller.
In your routes.rb file replace resources :items with
resources items do
member do
get 'bookmarklet'
end
end
In your controller put:
def bookmarklet
#item = Item.new
render :template => "bookmarklet", :layout => "different_layout" # or you can put this on the top of the controller, which is my style, but whatevs.
end
You should look into partials, if you are doing this as they clean up your code immensely.
A better way to think of things is to start with the controller instead of the view html.erb files. So each public method in your controller is effectively a page or action in the site. When you need a new action or page, add the method to the controller first. Then create the relevant view files.
So in your controller you'll need something like:
def bookmarklet
#item = Item.new(params[:item])
#item.save
render :template => "items/bookmarklet.html.erb", :layout => "different_layout.html.erb"
end
Normally you don't need to call render manually in the controller, but since you want a different layout than the default you need to specify it using render.