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.
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.
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? **
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.