0

I created a small application using scaffold. I'm posting data via the chrome "PostMan" extension.

This is the default create method in my controller:

# POST /settings
  # POST /settings.json
  def create
    @setting = Setting.new(setting_params)

    respond_to do |format|
      if @setting.save
        format.html { redirect_to @setting, notice: 'Setting was successfully created.' }
        format.json { render :show, status: :created, location: @setting }
        puts Setting.new(setting_params).to_yaml 

      else
        format.html { render :new }
        format.json { render json: @setting.errors, status: :unprocessable_entity }
      end
    end
  end

This is the log in Rails' console:

(0.2ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "settings" ("DetectionOnly", "SecRequestBodyAccess", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["DetectionOnly", "f"], ["SecRequestBodyAccess", "f"], ["created_at", "2016-03-23 18:04:42.642492"], ["updated_at", "2016-03-23 18:04:42.642492"]

I'm trying to only log this transaction into a file and save output in YAML format .

I tried adding this right after @setting.save.

   puts Setting.new(setting_params).to_yaml 

What am I doing wrong here?

4
  • Do you mean, you want to save the yaml into a separate file and not the log file? Commented Mar 23, 2016 at 18:19
  • correct, I would like to store values selected by user and store them into a config file. Commented Mar 23, 2016 at 18:20
  • Are you trying to keep a log of transactions, or are you trying to keep a user-configuration file? They're very different sorts of files. Do you intend to reload the user-config and need to manually edit it or view it, then YAML is good. If you need to reload it and people don't need to edit/view the file's contents then JSON is better, especially if other languages will need to read it. But why not have a table of user configurations instead of files? Commented Mar 23, 2016 at 19:01
  • @theTinMan, the settings table is already there to hold the configuration isn't there? Commented Mar 23, 2016 at 19:08

1 Answer 1

1

If you want to save the yaml to a separate file, you could open the file in append mode and append the yaml as follows in your create action where you have puts...:

...
settings_param_file = ... # File path
if @setting.save
  File.open(settings_param_file, 'a') do |file|
    file << Setting.new(setting_params).to_yaml
  end
end

Also, using puts in your controller action should be considered invalid. Instead you should use Rails.logger or just logger in your controller. To log a debug message use:

logger.debug { Setting.new(setting_params).to_yaml }
Sign up to request clarification or add additional context in comments.

3 Comments

Be careful doing this. Appending like that will generate multiple YAML documents inside one file, which can result in unexpected results when reloading the content: doc = {'foo' => 1}.to_yaml + {'bar' => 2}.to_yaml # => "---\nfoo: 1\n---\nbar: 2\n". YAML.load(doc) # => {"foo"=>1}
Spot on @theTinMan, you know I was answering the OPs question and how to save the YAML into a file. Formatting of the content is out of scope for this question and I did not want to comment on that part.
We can answer only the question asked, or we can also point out the folly in doing something to help the person understand there are dragons out there. They might not know the scope should include possible pitfalls to what they want to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.