2010-12-15 54 views
0

进出口工作创造一个入门控制器,通过上传照片,找朋友,邀请人等导轨 - 创建GettingStarted控制器

GettingStarted已经没有模型本身,它只是通过引导用户引导新用户一个巫师。用户可以在不破坏网站的情况下完全绕过这个启动过程。这只是一个指南...

我所做的迄今:

  1. 创建一个路由,控制器和模型:

路线:

resources :getting_started 
    namespace :getting_started do 
    resource :users, :only => [:edit, :update] 
    end 

控制器:

class GettingStartedController < ApplicationController 

    def index 
    @current_step = current_step 
    end 

protected 

    def current_step 
    current_step || steps.first 
    return 1 
    end 

    def steps 
    %w[step1 step2 step3] 
    end 

end 

型号

class GettingStarted < ActiveRecord::Base 

    attr_writer :current_step 
    attr_accessor :current_step 

    def current_step 
    #current_step || steps.first 
    return 1 
    end 

    def steps 
    %w[step1 step2 step3] 
    end 

    def next_step 
    self.current_step = steps[steps.index(current_step)+1] 
    end 

    def previous_step 
    self.current_step = steps[steps.index(current_step)-1] 
    end 

    def first_step? 
    current_step == steps.first 
    end 

    def last_step? 
    current_step == steps.last 
    end 

end 

查看:

<%= @current_step.inspect %> 
     <% form_for @gettingstarted do |f| %> 
     <table> 
      <tbody> 
       <tr> 
        <td> 
         <%= link_to image_tag current_user.profile_pic.url(:large), :class => 'getting-started-profile-pic' %> 
        </td> 
        <td> 
         <a href="" class="getting-started-link">Upload a photo</a> 
        </td> 
       </tr> 
      <table> 
     <tbody> 
     <% end %> 

现在我卡上,我需要GettingStarted通过现有模型引导用户的问题,不是一个模型本身。我得到未定义的方法`model_name'为NilClass:类

建议,对上述想法?

感谢

回答

0

GettingStarted模型中没有从ActiveRecord::Base继承,但它确实,你得到错误,因为基地期待一个表中您的数据库称为GettingStarteds,或什么的。如果你想保持内容的动态性,也就是说把它保存在数据库中,这样你就可以改变它,那么你非常接近,你可以使用像'steps'这样的自然语言模型,并且这些步骤有一个与它们相关的顺序,那么你可以根据其在入门控制器中的顺序来查找步骤。你也可以使用一个步骤,控制器香草工作流,然后用:as =>选项

重命名路由途径,如果步骤是静态的,你可能要探讨一些静态页面模型库提供如高电压https://github.com/thoughtbot/high_voltage