Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 331795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:48:26+00:00 2026-05-12T09:48:26+00:00

In my application, these planners (essentially, article ideas) follow predetermined templates, written in Markdown,

  • 0

In my application, these “planners” (essentially, article ideas) follow predetermined templates, written in Markdown, with some specific syntax here:

Please write your answer in the following textbox: [...]

Please write your answer in the following textarea:
...So here, on line, you should write one thing.
...Here, on line 2, you should write another.
...
...
...

Essentially, [...] is a text input, and a group of lines starting with ... are a textarea. That’s not really the issue – it’s just to explain what part of this code is doing.

On actions new and edit, the standard planner form is displayed, with the correct fields based on the template (for new) or current planner body (for edit). On save, the template’s fields are filled in with params[:fields], and the resulting Markdown is saved as the planner’s body. The code, I’d hope, is now possible to follow, knowing this context. Only relevant controller code is provided, and it uses make_resourceful.

class Staff::PlannersController < StaffController

  make_resourceful do
    actions :all

    before :create do
      find_planner_format
      if @planner_format
        current_object.body = fields_in_template @planner_format.body
      else
        flash[:error] = 'Planner format not found!'
        redirect_to staff_planners_path
      end
      current_object.user = @current_user
    end

    before :update do
      current_object.body = fields_in_template(current_object.body)
    end
  end

private

  def fields_in_template(template)
    fields = params[:fields] || {}
    if fields[:inline]
      template.gsub! /\[\.\.\..*\]/ do
        "[...#{fields[:inline].shift}]"
      end
    end
    if fields[:block]
      template.gsub! /^\.{3}.*(\n\.{3}.*)*$/ do
        fields[:block].shift.split("\n").collect { |line|
          "...#{line}"
        }.join("\n")
      end
    end
    current_object.body = template
  end

end

And now, the mystery: in the update action, changes to the body are not saved. After debugging, I’ve determined that the issue does not lie only in current_object.save, since the following before :update code does what you would expect:

before :update do
  current_object.body = 'test string'
end

In fact, even this gets the expected result:

before :update do
  current_object.body = fields_in_template(current_object.body) + 'a'
end

So now, the question: why is Rails so insistent that it not save the result of the function – and even then, only when it comes from update? More debugging showed that the object attribute is set, and even claims to save successfully, but reloading the object after save reverts the changes.

At first it looked like the resulting string was just a “poisoned” variable of sorts, and that rebuilding the string by appending “a” removed that strange state. However, the following code, which ought to add an “a” and remove it again, also failed to save.

before :update do
  new_body = fields_in_template(current_object.body) + 'a'
  new_body.slice! -1
  current_object.body = new_body
end

This is just bizarre to me. What am I doing wrong here, and what can I possibly do to debug further? (Or if you happen to instantly see my mistake, that’d be nice, too…)

EDIT: After checking SQL logs (not sure why I didn’t think to do this earlier), it would seem that Rails doesn’t seem to acknowledge the new body attribute as actually being different, even though checking the string in the debugger confirms that it is. As such, Rails doesn’t even run an UPDATE query unless something else is modified, in which case body is not included.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:48:27+00:00Added an answer on May 12, 2026 at 9:48 am

    Got it! Sometimes it just helps to state the question out loud…

    The deal is, I had forgotten that, when passing current_object.body to fields_in_template, it was being passed by reference. As such, all gsub! methods were running directly on current_object.body, so Rails acknowledged no real “changes” by the time I set body to what had just been set.

    The solution:

    def fields_in_template(template)
      template = template.dup
      # ...
    end
    

    Thanks for letting me talk to myself, and mission accomplished!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How these application allow users to upload their own templates? How is this possible?
i can connect mysql with java using eclipse in a java application with these
We have a Zend application that has these following modules: Users Shop etc... Front
I am looking for something very close to an application server with these features:
I wrote these lines in My Application start event: var mongo = new Mongo();
These days I'm working on a VB.NET application which can be used to edit,
I included these libraries or namespaces in my C# application and published the solution
In my application i added Marionette.sync plugin and override these methods: Backbone.Marionette.TemplateCache.prototype.loadTemplate = function
I'm doing these steps: Create new vs2010 C# console application Project menu -> Add
I have a web application with multiple WARS. These WARS all perform different functions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.