I have an issue with the interpolation of value in my strings, i ask for the related_links strings and i receive a hash with the title and the url.
this is my .yml
user:
subject: "New project created: %{title}"
related_links:
- title: Project Created
url: 'projects/%{id}'
When i ask for I18n.t("user.releated_links", id: xx) I get
{title: "Project Created", url: "projects/%{id}"}
DO any of you know how i can pass my parameters to related_links and it can interpolate the strings in the hash ?
I think you meant to use I18n.t("user.releated_links.url", id: xx).
You are missing the last key.
As a side note: Are you sure you want to generate URL from the localization files? Assuming those are links within your application, why don't you better use the rails helpers?
For anyone who still want the bellow output
{title: "Project Created", url: "projects/%{id}"}
Use deep_interpolate option
I18n.t("user.releated_links", deep_interpolation: true, id: xx)
Output => {title: "Project Created", url: "projects/xx"}
Related
I'm adding a Rails API using Grape where the success type should be String. When I do this the swagger_doc no longer generates as there is no parser registered for String. How do I register a String parser? Thanks to anyone that can help!
Below is the description that is being used to generate the swagger_doc
desc 'generate pdf',
detail: '...',
body_name: 'pdf',
success: String,
consumes: %w[application/x-www-form-urlencoded],
failure: BaseAPI.document_errors([401, 403, 404])
I am trying to build an API in rails using JSON API specification
My User model has change objects associated with them. For example, user can have the following change:
{
id: 10,
type: 'changes',
diffs: [
{
subject: 'email',
from: 'old#example.org',
to: 'new#example.org'
},
{
subject: 'tags',
from: []
to: [{ id: 1, type: 'tags', label: 'Ruby' }]
}
]
}
As you can see the diffs property contains an array of change differences and the second difference is an array of objects difference. I would want to modify this format (if possible) so it will conform JSON API specification. Like this:
{
subject: 'tags',
from: []
to: [{ id: 1, type: 'tags' }]
}
And put the tag's attributes into the included section:
included: [
{
id: 1,
type: 'tags',
attributes: { label: 'Ruby' }
}
]
Question: differences is an array of objects (not records, they do not have ID) which contain fields with records. Is possible to format the response so deeply nested records (like tags in my case) will be reference to the records in the included section?
I would want to use fast_jsonapi gem
I don't think you can do this without violating the spec.
A resource object must contain an id:
“Resource objects” appear in a JSON API document to represent resources.
A resource object MUST contain at least the following top-level members:
id
type
There is only one exception allowed by spec:
Exception: The id member is not required when the resource object originates at the client and represents a new resource to be created on the server.
So if your diffs don't have an ID you can't model them as a resource object.
But you are not allowed to include a resource that is not referenced by a resource object:
Compound documents require “full linkage”, meaning that every included resource MUST be identified by at least one resource identifier object in the same document. (source)
There is only one exception that does not apply to the case discussed here:
The only exception to the full linkage requirement is when relationship fields that would otherwise contain linkage data are excluded via sparse fieldsets.
I want to use first_or_initialize on a table having a json type column as :
A.where("name=? and email=? and address->>'city'=? and address->>'state'=?", "John", "john#gmail.com", "New York", "New York").first_or_initialize
But, this doesn't seem to work. Any idea if we can use this on such a table?
Please try this
A.where("'name'=? and 'email'=? and 'address.city'=? and 'address.state'=?", "John", "john#gmail.com", "New York", "New York").first_or_initialize
I think this should help you..
It seems your request is correct according Rails guide
As they say :
Query based on JSON document
The -> operator returns the original JSON type (which might be an object), whereas ->> returns text
What is the return of the query (without .first_or_initialize)?
An error, an empty array ?
These posts may help you to build the query you want : Post1, Post2, PostgresqlHelp
Edit : Sorry, I misunderstood the question
––––––––––––––––––––––––––––––––
If the request is the problem, you can try this: (from here Rails guide)
A.includes(:address).where(name: "John", email: "john#gmail.com", address: {city: "New York", state: "New York"})
If the request is not the problem, could you give us more information on the error ?
I'm using RoR for a couple of time. But after read many json specification for example jsonapi.org and json-schema.org I have the next question: What is the default JSON specification used in RoR ?
Because when you render a json in RoR you get this for example:
post: {
id: 1,
title: 'Stackoverflow rised 1 billion of alien money',
description: 'blablabla'
}
Is it a good practice if I used the default response in RoR when I'm creating an API ?
One specific thing that may or may not be helpful...
One thing that bothers me about the default rendering of JSON w/ Rails is that it leaves the key names unquoted when serializing a Hash, which is (technically) not valid JSON. The way to fix this is to add
ActiveSupport::JSON.unquote_hash_key_identifiers = false
to a configuration file like environment.rb. Once you've done that, serializing
my_hash = { post: { id: 1, title: 'Stackoverflow rised 1 billion of alien money', description: 'blablabla' } }
to JSON would change to
post: {
"id": 1,
"title": 'Stackoverflow rised 1 billion of alien money',
"description": 'blablabla'
}
vs. what you have above without the quotes.
I'm working to have Rails 3 respond with a JSON request which will then let the app output the search results with the jQuery template plugin...
For the plugin to work, it needs this type of structure:
[
{ title: "The Red Violin", url: "/adadad/123/ads", desc: "blah yada" },
{ title: "Eyes Wide Shut", url: "/adadad/123/ads", desc: "blah yada" },
{ title: "The Inheritance", url: "/adadad/123/ads", desc: "blah yada" }
]
In my Rails 3 controller, I'm getting the search results which come back as #searchresults, which contain either 0 , 1 , or more objects from the class searched.
My question is how to convert that to the above structure (JSON)...
Thank you!
Update
Forgot to mention. The front-end search page will need to work for multiple models which have different db columns. That's why I'd like to learn how to convert that to the above to normalize the results, and send back to the user.
I am not really sure what is the problem here, since you can always call ".to_json" on every instance or collection of instances or hash, etc.
You can use .select to limit the number of fields you need, ie:
Object.select(:title, :url, :desc).to_json
I am guessing that the #searchresults is ActiveRecord::Relation, so you probably can use:
#searchresults.select(:title, :url, :desc).to_json