Topic: virtual attributes

Is it possible to have a method define several virtual attributes?  (Trying to learn how to get new things working as a beginner...)

eg...

Class Color

define combined_colors
  new_color_1 = [color1, color2].join(' ')
  new_color_2 = [color2, color3].join(' ')

new_colors ={ new_1: new_color_1, new_2: new_color_2}
end

if so, can they then be accessed individually using a form (form_for :color) by accessing the returned hash?

Thanks,

Dave

Last edited by dcastellano1 (2012-02-08 14:06:56)

Re: virtual attributes

Probably

I'd recommend form_tag,  it is easier that form_for when you're not using an ActiveRecord based form

You still need a color_controller,  but nowhere in the controller would you ever treat Color as an ActiveRecord based 'model',  you just treat it like the simple class that it is, i.e. you could do
config/routes.rb:

match '/colors/new/:carray' => 'colors#new'

You'd have to define the Color class,  I'd recommend you to that in an initializer

config/initializers/color.rb:

class Color
  def initializer(carray)
    attr :rhash
    @rhash => {:set1=> [], :set2=>[] }
    carray[:set1].each do |s|
      rhash[:set1] << s
    end
    carray[:set2].each do |s|
      rhash[:set2] << s
   end
end

controllers/colors_controller.rb:

def create 
   @new_colors = Color.new(params[:carray])
end

views/colors/new.html.erb:

<%= form_tag '/colors/new' %>
   <%= text_field_tag :set1color1, name=>'carray[set1][0]' %>
   <%= text_field_tag :set1color2, name=>'carray[set1][1]' %>
   <%= text_field_tag :set2color1, name=>'carray[set2][0]' %>
   <%= text_field_tag :set2color2, name=>'carray[set2][1]' %>
   <%= submit_tag 'Create Colors' %>
<% end %>

views/colors/show.html.erb

  <%= "Color set 1" %>
  <% @new_colors.rhash[:set1].each do |c| %>
      <%= "color #{c}" %>
  <% end %>
  <%= "Color set 2" %>
  <% @new_colors.rhash[:set2].each do |c| %>
      <%= "color #{c}" %>
  <% end %>
<% end %>

Please keep in mind that off he top of my head, I'm sure there are bugs or whatever,  I'm just trying to illustrate the basic idea,  I'm sure there's a lot of changes to make it really usable.

Last edited by BradHodges (2012-02-08 14:42:39)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: virtual attributes

I currently have in controller...

@color = Color.find(params[:id])
@formatted_color = @color.combined_colors

So should I have access in view thru
@formatted_color

Trying to get it into ckeditor text field in format...

<%= ckeditor_textarea("object", "field", :width => '100%', :height =>
'200px') %>

but can't figure out how to format "object", "field",

Re: virtual attributes

Is ckeitor_textarea in a helper?

Anyway you can pass :value=> to text_field_tag

That will populate the text_area with the text you pass

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: virtual attributes

#gem 'rails',     :git => 'git://github.com/rails/rails.git'
gem 'gravatar_image_tag', '1.0.0.pre2'
gem 'will_paginate'
gem 'ckeditor'

Re: virtual attributes

OK, I took a quick look at the ckeditor gem,  but I can't find ckeditor_textarea,  an you post a link to the page that describes it?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: virtual attributes

These are the two I used:

https://github.com/jeremy6d/rails-ckeditor

https://github.com/galetahub/ckeditor


This is how I use it in my edit and new forms with no virtual attributes:
<%= f.cktext_area :correct_anno,
                 :toolbar=>'Full',
                 :width=>'950px',
                 :height=>'250px' %></p>

and works fine

Last edited by dcastellano1 (2012-02-08 17:31:23)

Re: virtual attributes

what type of attribute is :correct_anno,  string or text I assume?

what kind of attribute is @formatted_color

Are you trying to just get the contents @formatted_color INTO the text_area so you can edit it?

Or are somehow trying to apply those colors in some kind of markup, i.e.  You want to render some text using the colors defined in @formatted_color?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: virtual attributes

-text

Used the colors as an example. The actual code:

Class Question
define set_format_for_presentation
....
....
formatted = {
            answer_a:              answer_list[0][0],
            answer_b:              answer_list[1][0],
            answer_c:               answer_list[2][0],
            annotation_a:         answer_list[0][1],
            annotation_b:         answer_list[1][1],
            annotation_c:         answer_list[2][1],
            }
          formatted
end

Then in controller:
@question = Question.find(params[:id])
@formatted_question = @question.set_format_for_presentation

then in view:
<%= form_for :question, :url => { :controller => "questions", :action => "answer" } do |f| %>

DOES NOT WORK:
<%= f.cktext_area @formatted_question[:annotation_a],
                 :toolbar=>'Full',
                 :width=>'950px',
                 :height=>'250px' %>

Last edited by dcastellano1 (2012-02-08 19:16:56)

Re: virtual attributes

Post the error you get from your development log

Always do that when you post,  it helps greatly in finding the problem.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: virtual attributes

undefined method `<p>
    Correct. &nbsp;The answer is red.</p>
' for #<Question:0x007fc3947ec9d0>

98:         <%= f.cktext_area @formatted_question[:annotation_a],
99:                  :toolbar=>'Full',
100:                  :width=>'950px',
101:                  :height=>'250px' %>

Didn't post it as I realize its not even close to correct.  Its the problem of how to make  @formatted_question[:annotation_a] a virtual attribute so it can be used by f.cktext_area

Last edited by dcastellano1 (2012-02-08 19:18:25)

Re: virtual attributes

OK, I'm starting to understant,

You have
<%= form_for @question do |f| %>
  <%=  f.cktext_area @formatted_question[:annotation] ,... %>

I'm going to assume ckedit stuff behaves like all other rails forms helpers

the f.  tells the helper what model your dealing with,  the first argument to the helper is method to call on the model.

So in your question model,  you have to define BOTH a setter and getter

class Question << ActiveRecord::Base

  def formatted_question
  end
  def formatted_question=
  end
end

You have to define the setter, either that, or you have to manually delete the params[:question][:formatted_question] from the params hash before you try and create a new question, or update an existing question.

So make formatted_question do what ever you need and return the string you want.

You can leave the setter formatted_question= blank,  that's probably easier than deleting params[:question][:formatted_question] from the hash before you create or update a @question instance variable.

Then you can do:

<%= form_for @question do |f| %>
  <%=  f.cktext_area :formatted_question ,... %>

Last edited by BradHodges (2012-02-08 20:30:22)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: virtual attributes

Do I need a setter if I am not writing anything TO the database?

You wrote: Then you can do:
<%= form_for @question do |f| %>
  <%=  f.cktext_area :formatted_question ,... %>

but I need :
<%=  f.cktext_area :annotation_x ,... %>
which gets back to :formatted_question being a hash also (so I need to access :annotation_x in the @formatted_question hash)

This may drive me crazy, and you have sure hung in there, but don't feel like you are stuck with this one.  I appreciate all your help!!!

Last edited by dcastellano1 (2012-02-08 21:59:24)

Re: virtual attributes

Got it!

<p><%= cktext_area_tag  @formatted_question, @formatted_question[:anno_a],
         :toolbar=>'Full',
         :width=>'950px',
         :height=>'250px' %></p>

Thank you!

Dave