lundi 13 avril 2015

Undefined method error on class attributes in rails

Hi I'm new to Ruby on Rails so please forgive me if the question is somehow stupid.


I'm currently following the guide here.(It's written in Traditional Chinese).


And in that chapter, the author is talking about setting the status of a resource.


First, it edited the app/model/event.rb file



def closed?
self.status == "CLOSED"
end

def open?
!self.closed?
end

def open!
self.status = "OPEN"
self.save!
end

def close!
self.status = "CLOSED"
self.save!
end


Then the config/routes.rb file.



resources :events do
resource :state, :controller => 'event_states'
# ...
end


Then the app/view/events/show.html.erb file and insert the following code.



<p>Status: <%= @event.status %></p>

<p>
<% if @event.closed? %>
<%= link_to '[Open]', event_state_path(@event ), :method => :post %>
<% else %>
<%= link_to '[Close]', event_state_path(@event), :method => :delete %>
<% end %>
</p>


I understand that it's trying to toggle the status of an event through the POST and DELETE http request, but I get the "NoMethod" error on the show page. Which is understandable(?) cause there's no such variable in the class "event".



undefined method `status' for #<Event:0x007f5e59b3b988>

<% end %>
</p>
<p>Status : <%= @event.status %></p>

<p>
<% if @event.closed? %>


Any thoughts on this problem? (Please tell me if you need other information)


Aucun commentaire:

Enregistrer un commentaire