Search code examples
ruby-on-railsrubyattributesvirtual

virtual attribute problems with undefined methods


I've used Virtual attributes in the past but I can't seem to get past this, and I know the answer is probably staring me in the face.

I have a model like so:

model Confirmation.rb

class Confirmation < ActiveRecord::Base

  #attr_accessible :confirmation, :confirmation_token
  #attr_accessible :confirmation_token

  def confirmation_token
    confirmation.confirmation_token if confirmation
  end

  def confirmation_token=(token)
    self.confirmation = Booking.find_by_confirmation_token(token)
  end

end

Your average scaffold controller for

confirmations_controller.rb

  def new
    @confirmation = Confirmation.new(:confirmation_token => params[:confirmation_token])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @confirmation }
    end
  end

new.html.erb

<h1>New confirmation</h1>

<% form_for(@confirmation) do |f| %>
  <%= f.error_messages %>

    <%= f.hidden_field :confirmation_token %>

...

routes.rb

  map.confirmation "confirmation/:confirmation_token", :controller => "confirmations", :action => "new"
  map.resources :confirmations

error

undefined method `confirmation=' for #

In the console Booking.find_by_confirmation_token(token) with a given token works perfectly fine.

Any ideas? suggestions?


Solution

  • I think it should be either:

    def confirmation_token=(token)
        @confirmation = Booking.find_by_confirmation_token(token)
    end
    

    Or you should uncomment attr_accessible :confirmation or define #confirmation and #confirmation=.