Que tal!
Estoy empezando a desarrollar con Rails…por lo que me compre un libro
llamado Simply Rails. Siguiendo los pasos del libro me encontre con un
error que no me cabe porque puede estar pasando:
undefined method `name’ for nil:NilClass
Les dejo el controlador:
#Controlador empieza aqui#
class StoriesController < ApplicationController
before_filter :login_required, :only => [ :new, :create]
def index
get_stories ‘votes_count >= 5’
end
def bin
get_stories ‘’
render :action => ‘index’
end
protected
def get_stories(conditions)
@stories = Story.find :all, :order => ‘id DESC’, :conditions =>
conditions
end
def new
@story = Story.new
end
def create
#@story = Story.new(params[:story])
@story = @current_user.stories.build params[:story]
if @story.save
flash[:notice] = “La historia a sido agregada correctamente!”
redirect_to stories_path
else
render :action => ‘new’
end
end
def show
@story = Story.find_by_id(params[:id])
end
end
#Controlador termina aqui#
#Vista empieza aqui#
<%= @story.name %>
Shoveadas => <%= @story.votes.size %>
Agregada por: <%= @story.user.login %>
<%= link_to @story.link, @story.link%>
-
<% if @story.votes.empty? %>
Sin shoveadas aun
<% else %>
<%= render :partial => 'votes/vote', :collection => @story.votes %>
<% end %>
La clase Story:
#Clase Story empieza aqui#
class Story < ActiveRecord::Base
belongs_to :user
validates_presence_of :name, :link
has_many :votes
def to_param
“#{id}-#{name.gsub(/\W/, ‘-’).downcase}”
end
has_many :votes do
def lastest
find :all, :order => ‘id DESC’
end
end
end
#Clase Story termina aqui#
La migracion:
#Migracion empieza aqui#
class CreateStories < ActiveRecord::Migration
def self.up
create_table :stories do |t|
t.string :name
t.string :link
t.timestamps
end
end
def self.down
drop_table :stories
end
end
#Migracion termina aqui#
De antemano gracias!