Simple code...please help me in debugging

12 views
Skip to first unread message

Libber

unread,
Jan 7, 2012, 1:40:12 PM1/7/12
to rubyonra...@googlegroups.com
Dear all,
    In my web application, The client has to enter a set of values and I want to write them to a text file. This is what I am doing to achieve that. 

class SollusController < ApplicationController
  def index
  end
  def new
        @s=Sollu.new
  end
  def create
        @s=Sollu.new
        @s.save
        afile=File.open("network.txt",'w') 
        afile.write(@s.input)
        afile.close
        redirect_to sollus_path
     end

  def show    
  end
end


But after I enter the values from the the browser, nothing gets saved in the file "network.txt". On the contrary, If I do this whole thing in ruby console as below,

s=Sollu.new
s.input="I need to write this to text file"
afile=File.open("network.txt",'w')
afile.write(s.input)
afile.close()

After i run the above lines, I can see the text in the intended file. 

Can you please help me in debugging this part?

Hassan Schroeder

unread,
Jan 7, 2012, 2:03:02 PM1/7/12
to rubyonra...@googlegroups.com
On Sat, Jan 7, 2012 at 10:40 AM, Libber <amarnath...@gmail.com> wrote:
>     In my web application, The client has to enter a set of values and I
> want to write them to a text file. This is what I am doing to achieve that.

>   def create


>         @s=Sollu.new
>         @s.save
>         afile=File.open("network.txt",'w')
>         afile.write(@s.input)
>         afile.close
>         redirect_to sollus_path
>      end

> But after I enter the values from the the browser, nothing gets saved in the
> file "network.txt".

No kidding. Where above do you imagine that `@s.input` is magically
acquiring a value?

You should run through a basic Rails tutorial on using forms...

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Colin Law

unread,
Jan 7, 2012, 3:37:01 PM1/7/12
to rubyonra...@googlegroups.com
On 7 January 2012 19:03, Hassan Schroeder <hassan.s...@gmail.com> wrote:
> On Sat, Jan 7, 2012 at 10:40 AM, Libber <amarnath...@gmail.com> wrote:
>>     In my web application, The client has to enter a set of values and I
>> want to write them to a text file. This is what I am doing to achieve that.
>
>>   def create
>>         @s=Sollu.new
>>         @s.save
>>         afile=File.open("network.txt",'w')
>>         afile.write(@s.input)
>>         afile.close
>>         redirect_to sollus_path
>>      end
>
>> But after I enter the values from the the browser, nothing gets saved in the
>> file "network.txt".
>
> No kidding. Where above do you imagine that `@s.input` is magically
> acquiring a value?
>
> You should run through a basic Rails tutorial on using forms...

Also have a look at the Rails Guide on Debugging. That will show you
how to use ruby-debug to break into your code and inspect data and
follow the flow. Then you can break in before afile.write(@s.input)
and you would see whether @s.input has any content.

Colin

Libber

unread,
Jan 8, 2012, 1:10:48 AM1/8/12
to rubyonra...@googlegroups.com
Thanks for the reply guys...I figured out my error. I modified the code accordingly. 

Now I have a new problem..... In the create method, I call a system command to run a python program using the input from the user. The program executes perfectly but once the program executed, the server also stops. 

Here is the code,

def create
        @s=Sollu.new(params[:sollu])
        @s.save
        afile=File.open("network.txt",'w') 
        afile.write(@s.input)
        afile.close
        exec 'python netx.py' ## The server stops after executing this command. 
        image_tag "network_input.png" 
        redirect_to sollus_path
     end

Basically what I want to do is, The client has to enter a data and using that I generate a image from the python code. I have to display that Image.  If he is satisfied with the image, then he can choose to go further and save the data. Otherwise It has to redirect back to the new Sollu page and he should be given a option to change the previously entered data!.  I am beginner and so all this questions. Any help on how should I write the code? 

Ahmy Yulrizka

unread,
Jan 8, 2012, 1:20:26 AM1/8/12
to rubyonra...@googlegroups.com
If im not mistaken , calling kernel#exec will replace current process (server). 

maybe this will help http://mentalized.net/journal/2010/03/08/5_ways_to_run_commands_from_ruby/

also I agree with hassan you should try to get familiar with rails first to get the feel of it.

Ahmy Yulrizka

Libber

unread,
Jan 8, 2012, 1:29:59 AM1/8/12
to rubyonra...@googlegroups.com
@all: 
Can you please suggest a good rails tutorial on using forms?


 

Ahmy Yulrizka

unread,
Jan 8, 2012, 1:49:27 AM1/8/12
to rubyonra...@googlegroups.com

Colin Law

unread,
Jan 8, 2012, 3:20:20 AM1/8/12
to rubyonra...@googlegroups.com
On 8 January 2012 06:29, Libber <amarnath...@gmail.com> wrote:
> @all:
> Can you please suggest a good rails tutorial on using forms?

railstutorial.org covers most basic features of Rails and is free to
use online. Make sure that any tutorial you use is for the version of
Rails that you are using.

Colin

Reply all
Reply to author
Forward
0 new messages