0

When creating a new entry for my "question" object, my code works fine.

However, using various methods, I can't get the edit route to submit to def update correctly.

After a lot of head-scratching, I realized my create method performs a split on a named object like this:

def create
  @question = Question.new(params[:question])
  ...
  @question.options = @question.options.split(',').collect(&:strip)

In my update method,

def update
  @question = Question.find(params[:id])

I need to somehow format the options of params[:question] in this line:

  if @question.update_attributes(params[:question]) 

Otherwise, update_attributes fails. I cannot figure out how to do this. Any help appreciated.

Output of @question.options after formatting with split:

["options", "---\n- Extremely unlikely\n- Unlikely\n- Neutral\n- Likely\n- Extremely likely\n-"]
4
  • please tell us the output of @question.options. Commented Jun 17, 2013 at 21:13
  • can you please show your the form you are using to update @question. Commented Jun 17, 2013 at 21:34
  • What are the options doing in params[:question]? How does the update_attributes fail? Does the update action fire? Throw an error? Not update an attribute? If so, which attribute? Commented Jun 18, 2013 at 3:47
  • The options are in params[:question] because they are being passed in to #update from #edit. Commented Jun 18, 2013 at 13:46

1 Answer 1

2

If I understand your problem correctly, you should move your formatting code to a callback in your Question class:

class Question
  before_save :format_options
  def format_options
    write_attribute :options, read_attribute(:options).split(',').collect(&:strip)
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

This worked so damn well. I've been spending HOURS on this. What an elegant solution. THANK YOU.

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.