rails import csv data from dynamic url - ruby-on-rails

I'm completely newbie with RoR. What I'm trying to do is to import data from an asp url:
I've a ticket that passed to this url it returns with some informations like time_created, time_updated, owner, notes. I've tried putting this code in my view:
<% file = open("http://xxx.xxx.xxx/utility/ticket_dettagli.asp?TT="+#Ticket.ticket) %>
<%= CSV.new(file, col_sep: ',').readlines do |row| %>
<%= Ticket.create! row.to_hash %>
<% end %>
without success...any suggestion?

Why are you trying to do this in view? I think, there should be rake task for import tickets form csv or class method in your model ...
See: http://railscasts.com/episodes/396-importing-csv-and-excel
#Ticket - is this a variable? How you declare it?

Related

How can I use parameter from rails config file in a coffeescript file

In my custom rails config file I have "MyApp::Application.config.x.retries = 3"
Is there any way to call it in coffesscript like we do in controller or view with "Rails.configuration.x.retries" ?
If you export those config values (from ruby) to javascript, then you should be able to use them in your coffee script file.
For example, in your layout file (ex: application.html.erb) add the following:
<%= javascript_tag do %>
window.retries = <%= MyApp::Application.config.x.retries %>;
<% end %>
Then in your coffee script file, you can access it through window.retries.
I have used solution presented in RailsCast number 324
<%= content_tag "div", id: "link", data: { url: Rails.configuration.x.link } do %><% end %>
I notice you found a solution but for the record, another alternative is to embed ruby into your coffeescript file with erb - returning your config data as json.
For example..
my_app.coffee.erb
class MyApp
#config = <%= MyApp::Application.configuration.to_json %>
Notice I use a coffeescript class which functions more as a namespace. I can call something along the lines of MyApp.config['x']['retries']

Uploading and Prasing XML into ruby on rails. Using importer or another such as Nokugiri

Hello i have a question about active record and importing XML.
I was asked if i could import a xml file into are website to add orders.
So say a client has a bulk order.
His system outputs already a XML file and i need to import it. I found a gem called importer.
It seems very good, as it does both XML and CSV imports ideal for what i need. However in the DOC's it says for imports
Product.import(path_to_file)
But i would like to make it into a button were they may select a file to import. I think i would use something like paperclip to do this. However if you have any method to do it. Even if it does not include importer. Maybe Nokugiri. Then please help.
Thank you for your time, I will be monitoring this post closely.
Not really. Unless you want to save the file, all you need is a tool that reads it and converts the results to something that can be inserted into the database. Since you already found one, you just need to import it:
Add to your view a form for file insertion:
<%= form_tag import_products_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
Add a custom route action:
resources :product do
collection { post :import }
end
Put the import method in the controller:
def import
Product.import(params[:file])
redirect_to root_url, notice: "Products imported."
end
You do the processing in the model. For example:
require 'nokogiri'
def self.import(file)
doc = Nokogiri::XML.parse(file)
#your processing code goes here
end

Add arbitrary string to URLs in Rails/Middleman app

I'm using middleman to generate a static webpage. I need to add a consistent but understandable string to all urls so I can understand how users navigate on the page.
Now i do it like this
<% link_to '/'+?button=navigation , class: 'logotype', itemprop: 'url' do %>
...
<% end %>
I would prefer not having to manually add all the parameters but rather just use something that's already there, like a scope or something. I was thinking about using the name of the template file for example. The url is not unique enough.
Any suggestions?
The standard way of doing this would be to write a helper method that encapsulates your functionality:
<%= link_to_as_nav('/', class: 'logotype', ...) do %>
...
<% end %>
Then write a helper method:
def link_to_as_nav(url, options)
link_to(url + '?button=navigation', options)
end
This is the naïve approach and won't account for a url argument that already has parameters added, but that's something you can work to fix.

Rails Controller - Cant load such file (CSV)

I have the following in my view:
<%= form_tag import_list_path, multipart: true do %>
<%= file_field_tag(:file) %>
<%= submit_tag(:Submit) %>
<% end %>
I have this in my controller:
def import
require 'csv'
csv = CSV.load params[:file].tempfile.read
CSV.new(csv.tempfile, :col_sep => ",", :return_headers => false).each do |column|
name_array << column[5]
end
redirect_to(:index)
end
I'm just trying to store a temporary CSV file in memory and do some actions on it, essentially using it to pull in information to be used in consuming a web service later.
This is the error I receive:
cannot load such file -- Column1,Column2,Column3,Column4,Column5,Column6,Column7,etc....
How can I change my controller to not throw this error?
That should do it.
def import
require 'csv'
CSV.new(params[:file].tempfile, :col_sep => ",", :return_headers => false).each do |column|
name_array << column[5]
end
redirect_to(:index)
end
Another notice: Dont put your logic in your controller it belongs in the model ;)
//
That menas you should write a method in your model that deals with the data and only refere the path to the csv file as a parameter of the method. The model is there as an interface between you app and the database and for the things done in the app. The View is there to display your stuff and the controller is the thing that connects both.

how to connect my model to my app

Hey all,(im a beginner in rails)
i've created a controller that look like that:
class HomeController < ApplicationController
def homepage
end
def showmsg
#postword = params[:p]
end
end
the showmsg view looks like that:
<%= #postword %>
and my homepage view looks like that:
<%= form_tag( {:controller => 'home', :action => 'showmsg'}, :method => "post") do %>
<%= text_field_tag(:p,#postword) %>
<%= submit_tag("post") %>
<% end %>
now i have a form that i can write something in it and it will show on the showmsg view.
i created a model with the param :posts with a :description "text" field too.
MY QUESTION is how do i implement the model in the code so any thing i write will be in a list with the things i wrote before, because now (obviously) anything if i write something its deleting the one i wrote before.
thank you all!
I would argue that you're approach is not very rail's like... so if you're learning rails... you're learning it wrong.
Make a Model. Call it "Message":
rails generate model Message content:string
remember to migrate (hopefully you have your databases setup properly):
rake db:migrate
Then in your controller, when you post, you can create message like this:
def create #instead of showmsg... 'create' is the standard name for this
Message.create(params[:message])
#messages = Message.all
end
This will create the message in the database, and then it will get all the messages out of the database and put them into #messages.
You need to edit your form so that it uses form_for. You need to pass it #message, which is an instance of Message.new that your first controller action created. You should call this new
In your create.erb.html file, you show all the messages like this:
<ul>
<% #messages.each do |message| %>
<li><%= message.content %></li>
<% end %>
</ul>
I actually wouldn't recommend showing all the messages in the create action - it should really happen in the index action and you should redirect... but we need to keep this simple. Just google this or watch some of Ryan's screencasts and you'll get it.
And you're done. This is the "Rails Way" to do things. It's best to learn it the way they want you to learn it.
I would also commend that you format your code properly by indenting, and start naming your methods to be real english. For example, showmsg is bad and show_message is a lot better.
If all of this is totally confusing, then just create a new project, and then type:
rails generate scaffold message content:string
It will basically build the application you want and a lot more. You can just read the code and see how they did it.
Hope it helps.
Your approach is not really rails like so some tweaks and fixes are needed. Suggestions: check rails approach to REST. The following code will work it is a little more rails like, but still not all the way there.
Generate a model
rails generate model Message postword:string
this will generate the model and create the migration necessary to create the table in the database.
Create the table
rake db:migrate
Define a post action
It will save the postword in the database. In your controller:
def create
#message = Message.create!(params[:message])
if #message.save
redirect_to "/home/showmsg"
else
render :action => "/home/homepage"
end
end
Create and instance of Message to use in your form
def homepage
#message = Message.new
end
Fix your form tag
<%= form_for #message, :url => "/home/create" do |f| %>
<%= f.label :postword %>
<%= f.text_field :postword %>
<%= f.submit "Create" %>
<% end %>
Now let's show the words in the showmsg page
In the controller select the postwords from the database:
def showmsg
#postwords = Message.all
end
Showing them: /showmsg.html.erb
<H1>postwords list</H1>
<ul>
<% #postwords.each do |p| %>
<li><%= p.postword %></li>
<% end %>
</ul>
Your routes.rb file will have this routes:
get "home/homepage"
get "home/showmsg"
post "home/create"
Define an attribute :new_text in a way similar to this:
class TheModel
# Virtual writer - everything assigned to this attribute
# will be added to self.text
#
def new_text=(v)
self.text += v.to_s
end
def new_text
"" # This is write-only attribute
end
end
Now, use the field 'new_text' in your form.
Of course, this is a very simple example. You should decide whether you want to add the content on every call to :new_text=, maybe some validation would help, the read accessor may need some care, and so on.
For some good guides which may help you start, see the site http://guides.rubyonrails.org/

Resources