I am trying to autocomplete a search, the only issues is i am fetching two information. I am following the tutorial here http://railscasts.com/episodes/102-auto-complete-association-revised
Here the issues
def index
#titles = Event.order(:title).where("title like ?", "%#{params[:term]}%")
#customers = Customer.order(:first_name).where("first_name like ?", "%#{params[:term]}%")
render json: #titles.map(&:title)
render json: #customers.map(&:title)
end
If both contain something how can i return both objects.
You have to create a single JSON object which contains both information, like this:
respond_to do |format|
format.json { render json: {titles: #titles.map(&:title), customers: #customers.map(&:title)} }
end
Haven't tested it, but it should work.
Related
I have a product model and a kit model through a KitProducts join table.
Product model
has and belongs to many kits
Kit model
has and belongs to many products
How can I get an output of my products with their kit_id in my JSON?
#products = Product.all
respond_to do |format|
format.html { render 'new', layout: "builder" }
format.json do
render json: [
products: #products
]
end
This will give me only the product itself, I need to be able to show their kit info
Desired JSON output:
products":[{"id":1,"name":"Test", "kit_id": 1}]
I want to be able to do kit.products in my JS file
You can use the include option in your render call:
Example:
render json: #products, include: :kit
This will give you the following output:
[{"id":1,"name":"Test", "kit_id": 1}]
If you want to include more than one association, you can pass an array:
render json: #products, include: [:kit, :other_association]
I have the following code that responds to GET /something.json:
def index
#something = Something.all
respond_to do |format|
format.json { render json: #something }
end
end
That runs a SELECT * FROM something in the database, formats the result into a JSON, and responds with it.
The request might ask for another field through a query parameter, which is in a different table than something. I managed to retrieve the desired field doing this:
def index
#something = Something.all
if params[:get_field_from_some_other_table] == "true"
#something.each do |i|
some_other_table = SomeOtherTable.find(i.some_other_table_id)
the_field_i_want = some_other_table.the_field
end
end
respond_to do |format|
format.json { render json: #something }
end
end
But I haven't found a way to add the field to the JSON string. How can I do that? Or is there a better way to retrieve the field with the contents of the something table through a JOIN or something like that?
something and other_table should be related at active_record somehow... maybe a has_one?
Try that and then just use #something.all.includes(:other_table_attribute)
Apart from that, please post your code properly with some readable examples, that helps a lot and will give you faster responses :)
I have rails api with simple paperclip model:
def create
#photo = Photo.new(photo_params)
if #photo.save
render json: #photo, status: :created, location: #photo
else
render json: #photo.errors, status: :unprocessable_entity
end
end
private
def photo_params
params.require(:photo).permit(:image, :title)
end
My frontend framework send get like this
{"title"=>"simpletitletext", "photo"=>{"image"=>......}}
But it wrong, because rails waits following
{"photo"=>{"title"=>"simpletitle", "image"=>#...}}
I had been trying for different ways to fix angular for many hours, before wrote . May be it will be able to fix in rails
If your server has an incoming request that looks like this:
{"title"=>"simpletitletext", "photo"=>{"image"=>......}}
you can make it look like this:
{"photo"=>{"title"=>"simpletitle", "image"=>#...}}
The only difference between the two is that in the first, the title key is outside of the photo nested hash. but you want it to be inside.
So in your Rails controller,. you could write:
def photo_params
hash = params[:photo]
hash.merge(title: params[:title])
end
I'm trying to collect all the clients of my current group_id in json (/groups/1/clients.json)
I'm using a custom method because I want all the groups of the current_user aswell.
Anyway; I have a method that checks in a loop of all the groups of current_user if the params[:group_id] equals the group.id of my loop. And if it is; Output my clients in a JSON file.
Now I'm 100% positive that params[:group_id] and group.id exist(I've both been able to output them into the JSON if I escape the if params[:group_id] == group.id.
Nonetheless, when I'm using the IF statement I don't get the output I need. Does anyone have any clue why my IF statement doesn't go as I was expecting?
#usersGroups = current_user.groups
if params[:group_id]
#usersGroups.each do |group|
if group.id == params[:group_id]
respond_to do |format|
format.json { render json: group.clients, status: :unprocessable_entity }
end
end
end
end
group.id.to_s == params[:group_id]
Also you can clean up your code like this:
group = Group.where(user_id: current_user.id).find(params[:group_id])
respond_to do |format|
format.json { render json: group.clients, status: :unprocessable_entity }
end
I have a "recipes" table and an "ingredients" table. Each recipe "has_and_belong_to_many" ingredients and each ingredient "has_and_belong_to_many" recipes.
I want to add a link to the ingredient page: "show all recipes which contain this ingredient".
I wrote the following code in my ingredient controller:
def recipes
#ingredient = Ingredient.find(params[:id])
#recipes = #ingredient.recipes
respond_to do |format|
format.html # index.html.erb
format.json { render json: #recipes }
end
end
My problem is that now it expects me to have a "recipes.html.erb" file under the "ingredients" view.
I don't want to create a new view for this, I just want to use the same code I use in the "recipes" view (recipes/index.html.erb).
How can I direct rails to this view?
(I'm using rails 3.x)
Thanks,
Li
Like this:
def recipes
#ingredient = Ingredient.find(params[:id])
#recipes = #ingredient.recipes
respond_to do |format|
format.html { render "recipes/index" }
format.json { render json: #recipes }
end
end
For details, please take a look at the rails guide: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render