I am trying to use an external API from eniro http://api.eniro.com/ for my rails application.
They have provided a code snippet to use for ruby as a working example, but I need further assist on how I can use it. The code below is my controller.
My Controller
class ListanController < ApplicationController
require 'open-uri'
require 'rubygems'
require 'json'
def get_json
#json = JSON.parse( open('http://api.eniro.com/partnerapi/cs/search/basic?profile=[*******]&key=[**********]&country=se&version=1.1.3&search_word=pizza').read )
#json['adverts'].each do |advert|
puts advert['companyInfo']['companyName']
end
end
def index
#json = JSON.parse(open('http://api.eniro.com/cs/search/basic?profile=mrshawn191&key=5582158511111471396&country=se&version=1.1.3&search_word=pizza').read )
end
end
My view
<h1><center>Test site</center></h1>
<center>
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag :index, params[:index] %>
<%= submit_tag("Search") %>
<% end %>
</center>
<% #json['adverts'].each do |advert| %>
<%= advert['companyInfo']['companyName'] %>
<% end %>
How do I interact with this controller in my view?
I want simply to create a search form in my view that is connected to the external API, so whenever I search for something it will redirect me to a new page and display the result from the api.I don't know much but what I'm guessing is that I have to
In my controller, specify a method where I parse url into json object
In my view puts that json objekt? depending on what the keyword is?
Configure routes.rb to match the search path
I'm lost, can anyone give any guidelines where to start? How would the code look in my view?
The code parts
#json['adverts'].each do |advert|
puts advert['companyInfo']['companyName']
end
merely print it to the console. Since you want it showing in your view, you can just remove those.
In your view (if you're using ERB), add
<% #json['adverts'].each do |advert| %>
<%= advert['companyInfo']['companyName'] %>
<% end %>
which should then do the same as your putscode, just for the HTML rendered on page. Format as appropriate.
Related
My View index.html.erb looks like this:
<p><% #filelist.each do |d|%>
<p><%= link_to d, download_file(loc:"/lib/",file:"del.docx")%></p>
<% end%>
</p>
My Controller class
def index
#filelist = Dir.entries(".").select {|f| !File.directory? f}
end
def download(loc,file)
send_file path:loc, filename:file
end
But doesn't work. Can any one suggest any right way of doing this?
I want to pass the file location to the method and the method should return file at the location and user should be able to download it.
Thanks
Try this in your index.html.erb:
<p>
<% #filelist.each do |d|%>
<%= link_to "#{d}", '/lib/del.docx' %>
</p>
I have started a new app and I'm setting up all the basic CRUD operations. I have a Event table and I'm trying to render all the events to an index page. All I want to render is the event name and description. Right now the name and description render but so does the entire event object? That's strange to me because I'm going through each object a picking out what I want to display. I'll show my code for clarity.
VIEW:
<%= #event.each do |e| %>
<%= e.name %>
<%= e.description %>
<% end %>
CONTROLLER:
class EventsController < ApplicationController
def index
#event = Event.all
end
end
SCREENSHOT:
FYI: I'm using the rails_admin gem. Any ideas on why the entire object is rendering would be greate thanks!
You have syntax error
Just remove the <%= from your first line and only <% then problem solved.
Now you should have:
<% #event.each do |e| %>
<%= e.name %>
<%= e.description %>
<% end %>
Meaning of <%= is to print and you printed entire object.
Edit: I've rewritten this question to be more specific.
So I have a log in box and what I'd like to do, is when the user presses the log in button, pass the value from text_field_tag to my application controller and set a variable there. I'm not sure how to do this, so I guess I'm asking how to link the view and controller together and then how to pass the value using parameters. Here's my view:
<% content_for :title, "Home" %>
<h2>MailChimp API Login</h2>
<%= form_tag do %>
<p>
<%= text_field_tag(:apikey) %>
</p>
<p><%= submit_tag "Log In", :name => "apikey" %></p>
<% end %>
and my application_controller:
require 'mailchimp'
class ApplicationController < ActionController::Base
before_action :setup_mcapi
def setup_mcapi
#mc = Mailchimp::API.new(params[:apikey])
end
end
You can follow below links for mailchimp integration and reading mail list in Ruby on rails.
rails on mailchimp by gem
Rails Plugin to access MailChimp and read list
But, If you want me to explain in C#, then i can tell you point by point.
I have most of the functionality done for a site. Now I am trying to make it look nice. I have a _form.html.erb that works great.
<%= form_for(#card) do |f| %>
<% if #card.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#card.errors.count, "error") %> prohibited this card from being saved:</h2>
<ul>
<% #card.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :event %><br />
<%= f.text_field :event %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Files
view
- cards
-- new.html.erb
-- index.html.erb
-- show.html.erb
- layouts
-- application.html.erb
- pages
-- index.html.erb
I make a call for the form from new.html.erb and it works sends it to show.html.erb, just as I want. I'm using bootstrap and decided to make use of the nav bar. I have placed the nav bar code into the application.html.erb. It works just fine, well kind of. I want what would normally be a search function to be the add a new card.
When I add the form call it does not work, when I add it directly to the application page it does not work. I'm not sure, I have spent hours on this. I got it to work only on the show.html.erb page, both index pages would error out. I honestly don't remember how I did this though.
I'm trying to learn by doing, but I am stuck and need some help.
Thank you,
Ian
I guess that when you say that its working in your new.html.erb you have a new action inside your cards_controller, and inside this action you have something like: #card = Card.new
Well, if you want to put this form in another view, like in the application.html.erb you need to set first your #card variable, so you can do something like:
# application_controller:
before_filter :new_card
def new_card
#card = Card.new
end
be aware that all the controller that inherits from application controller will set this #card variable
#instance_variable
The underlying problem here is that you're calling a partial - by design, these are meant to give you the ability to call the functionality the file contains anywhere in your application
The problem you have is you're referencing an #instance_variable directly in your partial.
This isn't normally an issue - if you're using partials like you were originally (to modularize views), it should be okay. The problems arise when you try and use the partials in a more generalized way, as you are doing now:
#app/views/controller/_form.html.erb
<%= form_for(#card) do |f| %>
This relies on the #card instance variable being made available, which won't be if you're loading the partial in any other controller than the cards_controller.
--
Fix
The way to fix this is to either populate the #card instance variable in the application controller (as described by edymerchk), or to pass the raw value through the locals hash of the partial call:
This will allow you to use the card local variable in your partial:
#app/views/controller/_form.html.erb
<%= form_for card do |f| %>
-
Alternatively, you could also set the #card instance variable, as recommended in another answer:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
before_action :set_card
private
def set_card
#card = Card.new
end
end
I've searched all over the web for decent explanations of how to do what I want to do, but cannot find any.
What I want to do is have the user be able to search through the yummly api and return some results back...
Here is some code.
index.html.erb
<% #results.each do |r| %>
<%= r.name %>
<% end %>
home_controller.rb
class HomeController < ApplicationController
def index
#results = Yummly.search('Onion')
#recipe = #results.map(&:to_s)
end
end
I've installed the Yummly gem which allows me to call Yummly.search
How can I allow the user to search for the term instead of hard coding it? It returns just fine hard coded but I cannot figure out how to allow the user to search.
Thank you!
Ideally, you would have a form that allows the user to enter a search term, and then pass that term to the Yummly API.
Something like (substitute your own route name):
<%= form_tag({controller: 'home', action: 'index'}, {method: :get}) do %>
<%= text_field_tag 'search' %>
<% end %>
And in your controller:
Yummly.search(params[:search])