I am trying to log into a ftp account with rails, but the authentification fails and i don't know why. My html page calls a method :
test(site.host,site.ftp_user,site.ftp_pw)
My helper defines this method :
def test(host_ip,user,pass)
ftp = Net::FTP.new(host_ip)
ftp.login('user','pass')
ftp.system
ftp.close
end
The information is working with FileZilla so the problem is somewhere else. Any idea ?
I'm getting confused with what should be between quotes or not. I mean site.ftp_user and site.ftp_pw are strings so i don't know why i have to use quotes. But if i don't use them, i get a gettaddrinfo error...
Here is the SocketError i get when removing the quotes :
getaddrinfo: Name or service not known
Kinda lost here :/
Yeah, you want to remove the quote for user and password in your helper.
Then, what line is the getaddrinfo error on? maybe you can share it here.
Is it on the ftp.system line...if so it looks like system may not be the method call that you want.
If you want to open a file or something checkout the examples here http://stdlib.rubyonrails.org/libdoc/net/ftp/rdoc/classes/Net/FTP.html
Related
I'm trying to create an "asset controller" shim which will filter static asset requests so only authorized users can get retrieve certain assets. I wanted to continue to use the asset pipeline so I setup a route like this
get 'assets/*assetfile' => 'assets#sendfile'
Then I created an AssetsController with one method "sendfile". Stripping it down to only the stuff that matters, it looks like this:
class AssetsController < ApplicationController
def sendfile
# Basically the following function forces the file
# path to be Rails.root/public/assets/basename
assetfilename=sanitize_filename(params[:assetfile] + '.' + params[:format])
send_file(assetfilename)
end
end
It looks like I have to run this in production mode as rails by-passes my route for assets in development. So I precompile my assets and I can verify in the controller that the files exist where they are expected to be.
However, now the problem is that I'm getting a "ActionController::InvalidCrossOriginRequest" when the Javascript asset is requested (just using the default application.* assets for now). I've read about this error and I understand that as of Rails 4.1 there are special cross-origin protections for Javascript assets. Sounds good to me, but I don't understand where the "cross-origin" part is coming from. Using firebug, I can see that the asset requests are being requested from the same domain as the original page.
I am certain that this is the problem because I can solve it by putting "skip_before_action :verify_authenticity_token" in the beginning of my controller. However, I really don't want to do this (I don't fully understand why this check is necessary, but I'm sure there are very good reasons).
The application.html.erb file is unchanged from the default generated file so I assume it's sending the CSRF token when the request is made, just as it would if I didn't have my own controller for assets.
So what am I missing?
Ok, I think I answered my own question (unsatisfactorily). Again, long post, so bear with me. I mistakenly forgot to add this to my original questions, but I'm using Ruby 2.2.0 and Rails 4.2.4.
From looking at the code in "actionpack-4.2.4/lib/action_controller/metal/request_forgery_protection.rb", it looks like Rails is doing two checks. The first check is the "verify_authenticity_token" method which does the expected validation of the authenticity token for POST requests. For GET requests, it ALSO sets a flag which causes a second check on the formed computed response to the request.
The check on the response simply says that if the request was NOT an XHR (AJAX) request AND the MIME Type of the response is "text/javascript", then raise an "ActionController::InvalidCrossOriginRequest", which was the error I was getting.
I verified this by setting the type to "application/javascript" for ".js" files in "send_file". Here's the code:
if request.format.js?
send_file(assetfilename, type: 'application/javascript')
else
send_file(assetfilename)
end
I can skip the response check all together by just adding the following line to the top of my controller class:
skip_after_action :verify_same_origin_request
The check on the response seems pretty weak to me and it's not clear how this really provides further protection against CSRF. But I'll post that in another question.
I couldn't see 500 error when I accessed a URL for which there is no Data, rather it was showing the 'Template missing' error. At the same time, when I ran it at the server, it had shown the 500 error. I need to do testing at local machine. So please tell me how I can create such a situation at localhost?
Thanks & Regards,
Rajesh
You can create such situation in localhost if you run the server in production mode: rails s -e production (Of course, if the error is still present)
If you are getting a template missing error is most probably because you are missing the view file for a given controller action
Ex: if you have a controller called users
class UsersController < ApplicatationController
def index
end
end
by default rails is expecting a view in
app/views/users/index.html.erb (or haml)
But if you could post the error log you are getting we might be able to help you more
regards
sameera
It's a simple problem with your corresponding view not being present. Open the control file which corresponds to your url. Then see the action which is being called and then, see if the corresponding view is available in the app/views/ folder.
The reason for 500 error is the same 500 says that there was an internal error at the server side.
Also, don't go about changing the url's charecter and stuff. IT WONT WORK!
I'm sending emails from rails, and using the *_url helper to generate urls.
However, these links are starting:
https://admin.test.website.co.uk/
(which is what the URL used to start with), whereas they should start:
https://production.test.website.co.uk/
Anyone know what could be going wrong..?
What I think is going on is that you send the e-mails from your admin-interface. When using a generated _url helper, the host is generated from your current host, if present. If you want to override this, you can specify the host yourself using a variable or even hard-code it.
So, for example, instead of using page_url, you could use page_url(:host => 'https://production.test.website.co.uk/')
Good luck!
In the action class, I have this one at the end of the function:
$this->redirect("/myproject_dev.php/login/");
In which I hard code the development project name in the url. of course it works while I'm doing development but when it comes to production, I need to remove that.
so, how do I find out the project name ("myproject_dev.php") in the code? so that I can do this:
$this->redirect($myProjectDevName . "login/");
or.. is there a smart way to handle this?
thanks.
------ ADDition ---
so, I used the public_path as suggested. but it only returns the root path.
I'm wondering if there is a function that when I run in development mode:
http://mydomain.com/myproject_dev.php/whatever
in its action class:
$this->redirect(public_path('login'));
and result in
http://mydomain.com/myproject_dev.php/login
while if I run in production:
http://mydomain.com/whatever
the action class is unchanged:
$this->redirect(public_path('login'));
it will return this:
http://mydomain.com/login
or.. how to get the application name and symfony environment name?
:)
You should use the symfony routing system and call a routing rule from your routing.yml:
$this->redirect('#login');
This way it becomes separate of your environment and you wan't need to change anything when you're ready to move to production.
you could try
public_path()
http://www.symfony-project.org/api/1_4/UrlHelper#method_public_path
try this put this in your function
$this->redirect("MODULENAME/ACTION");
ex: $this->redirect("pim/admin");
this will redirect you to the pim module admin action...
I'm trying to work through this guide to Rails routing, but I got stuck in section 3.3:
Creating a RESTful route will also make available a pile of helpers within your application
and then they list some helpers like photos_url, photos_path, etc.
My questions:
Where can I find the complete list of helpers that is "made available?"
Is there a way to call the helpers in the console? I created an app, then opened up the console with script/console. I tried to call one of the helpers on the console like this:
>> entries_url
But got:
NameError: undefined local variable or method `entries_url' for #<Object:0x349a4>
from (irb):8
You have several questions in there, most of which have already been answered by people below.
The answer to one that wasn't fully addressed however, is: yes you can use the script/console to see where your routes go. Just type in app.[route_helper] and it will respond with the path. For example app.users_path will return /users/
So for your example type app.entries_url for the full URL - or app.entries_path for its relative path within the console.
rake routes at the command line should get you that list.
I think this may be what you are looking for ... http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf
You can access other helpers in the console by prepending "helper."; ie. helper.progress_box (assuming #progress_box exists of course)
From memory, you can't call url/path helpers from the console for some reason.