Get paperclip full url in model - ruby-on-rails

I publish some data in redis after saving my model and I need send full url of attachment to Redis, but I'm stuck to get rails root url with port or full attachment url.
How I can get full url of paperclip attachments or get rails app root url in model file?

Models don't know how you deploy the application. However controllers do via request object:
"#{request.protocol}#{request.host}"
You could pass it to the model level, however it breaks the abstraction badly.
I would consider changing the design.

The use case is also in web sockets. Use #{request.host} or #{request.port} or #{request.protocol} or #{request.params} to get the necessary request information. #Danil answer is the right string interpolation for the question.

Related

Get Rails application domain name without request

Is there a way to get Rails` app domain name without use of the request?
I know how to get URL from the request, but what if there is no request and Rails is just running a delayed job task, can i get a domain name of the server where the Rails app is hosted?
There isn't a per se way of doing this due to how Rails is built but if it is a big enough project it might me stored somewhere.
So you could look for a place where it is being set and use that elsewhere.
For example, if the project uses request_store you could search for something like:
RequestStore.store[:host] = SomeTable.stored_host_name
And use SomeTable.stored_host_name in your worker or migration or the place you want to use it which doesn't have a request at hand.
Have a great day!

Control Public File Response Code in Rails

Let's say that I have a POST endpoint in my Rails app, in which it gets a param called state, which will be an integer of either 200 or 503.
How can I make the Robots.txt file respond with the given state from that POST endpoint, I mean I need a way to control the response code of that only file (Robots.txt) depending on that POST endpoint.
BTW, question is not about how to store that state or something, it's only about how to change the response code of a public file?
Is that possible?
What I have in mind for this and trying now is to have a controller action matching the robots.txt route, but I feel this is so silly to do.
Yes, if you want Rails to be involved in deciding the response for a given URL, then you're going to want to define a controller action to handle those requests.
You can use send_file to actually do the file-sending part.
Depending on your web server's configuration, it's likely you'll need the actual robots.txt file to be stored somewhere other than public/ -- otherwise it might get served without Rails even having a chance to get involved.
You could instead arrange to rewrite your nginx (say) configuration file at runtime, based on what response code you want... but I think that would be silly to do.
A more practical middle-ground would be to have Rails create or delete a marker file, and then use a conditional in the nginx configuration based on whether that file exists. That would be an nginx question though... and would get complicated if you have more than one server.

Handling large URL query parameters for SPA

So, I've recently finished my SPA and published it online. The application allows you to create content and share your content by providing a permalink. The permalink is generated by stringifying the object, encrypting it, making it URL safe, and tacking it onto the base url as a query parameter.
The problem I'm facing, is that when the user creates content that causes the JS object to be large, the URL of course becomes large as well. I want the application to be able to handle any size, but my site crashes with a Request-URI Too Long error.
The alternative I've considered is setting up a back-end that can take the data and provide an id of some kind to use in the url instead, so my application can just call the back-end with the id to fetch the data.
I'd like to avoid doing that if possible though, as I don't really feel like paying for the server onto of already paying for my site hosting. I'm hosting the site on my GoDaddy account, but have seen other sites handle obscenely large URLs through NameCheap, not sure if that has something to do with it.
Hash the content with a hash such as SHA-256, Base64 encode the hash, URL encode it and use that as the permalink or at least part of it.

POST via json in Ruby on Rails

I'm trying to create record in db via json. The problem is that i don't know how to compose http request in URL bar:
It should be something like:
http://localhost:3000/addnewpost.json?content=sometexthere
Or this is not correct?
It's not correct. Creating a record should be a POST command, not a GET. As such, the data content should go in the POST headers, not in the URL.
Are you sending this POST directly from Ruby? (if so, you'll want to look at Net::HTTP or some other ruby HTTP client). Show us some code and we'll help you improve it.

AngularJS - How to fetch PDFs (or other binary files) from rails backend?

I'm writing a toy application to get acquainted with AngularJS. This application has a Rails backend.
I don't know how to make the client side angularjs app, deal with a PDF that the backend sends when hitting a particular URL (http://localhost:3000/contacts.pdf)
When typing the above mentioned URL straight in the browser, the server replies with a PDF and the browser asks what to do with it (download or open).
When the same thing is done via the angularjs app, I can see the file gets returned in the response. And that's where I'm stuck at.
How can I replicate the same behaviour within client side app?
Thanks for your help
One way is just use an anchor tag in HTML and put the link as a controller variable ie. Download and in controller put $scope.link = "http://localhost..."; (or array if requiring multiple links).
If this is not what you want, please add further clarification.

Resources