XMLHttprequest post method in rails - ruby-on-rails

how to send file through xmlhttprequest.send(file) in rails , post method not calling respective action(method) in the controller , get method calling the same action which specified in the post method and how to get the request(body) content in the controller ?
Any idea?

You should be able to get it from your request object. Maybe something like this:
path = "public/"
File.open(path, "wb") { |f|
f.write(request.body.read)
}

Related

How to use send method to post, get, update route?

I'm trying to DRY some request specs.
I need to pass to a shared example the following params:
method, action, params
shared_example:
shared_context "blah" do |method, action, params|
let(:url_path) { send("api_cars_path") }
end
How can I execute a route with send using the method that is being passed as parameter ? Something like this:
send(:get, "api_cars_path")
So I can can pass :get, :post, etc dynamically.
The route api_cars_path is really a method which returns a string like /api/cars. So you need to invoke the route helper and then pass it to the get method as the first argument.
send(:get, send(:api_cars_path))

Rails - How To Get Full URL in Model

In my Rails 5 app, I'm trying to get the full URL for my instance, but when I tried to use url_for as suggested in this SO question, I get an error, NoMethodError: undefined method 'url_for' for #<Product:0x8646fc0>.
Full URL with url_for in Rails
def get_url
url = url_for(self)
end
I also tried product_url(self) and received a similar error.
What's the proper way to get the URL for my instance? Thanks!
helper method is only available in view or decorator.
it's better to define url in the decorator.
if you want to use helper method in the model, try this
def url
Rails.application.routes.url_helpers.product_url(self)
end

How to append Query parameter in the URL from the controller method in Grails?

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

How to get data from a URL inside a rails controller?

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]

Get output of different controller action in rails3

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

Resources