我的范围不适用于某个控制器

时间:2014-08-04 19:34:47

标签: ruby-on-rails ruby scope

我的ruby版本是1.9.3,我的rails版本是3.2.2,我使用的是Windows 7。

我创建了一个控制器和一个带有时间轴名称的模型,用于注册创建另一个名为"问题"的表,这是我的提示命令

rails g controller timeline index
rails g model timeline content timelineable_type timelineable_id:integer
rake db:migrate

一切都运行正常,但后来我把一个pt.yml作为语言环境,并尝试在我的routes.rb上为这个地址添加一个范围.rb:

  scope "/:locale" do

    get "/timeline/index"

    resources :projects

    resources :issues
  end

但是当我尝试访问页面" localhost:3000 / en / timeline / index"时,我收到了以下错误:

Routing Error

wrong constant name :locale
Try running rake routes for more information on available routes.

有人可以帮助我吗?

这是时间轴/ index.html.erb:

<h1>Timeline</h1>

<div class="content2">
    <div id="fundo">
    <div class="row table-row">
        <div class="col-md-4 oi" align="left">Message</div>
        <div class="col-md-4 oi" align="left">Issue </div>
        <div class="col-md-4 oi" align="left">Created at</div>
    </div>
    </div>
    <% @timelines.each do |timeline| %>
    <div class = "row" id="baixo">
        <div class="col-md-4"><%=timeline.content %></div>
        <div class="col-md-4"><%=link_to timeline.timelineable.title, issue_path(id: timeline.timelineable_id) %></div>
        <div class="col-md-4"><%=time_ago_in_words timeline.created_at %></div>
    </div>
    <%-end%>
</div>

这是时间轴控制器:

class TimelineController < ApplicationController
  def index
    @timelines = Timeline.all
  end
end

这是时间轴模型:

class Timeline < ActiveRecord::Base
  belongs_to :timelineable, polymorphic: true
end

在我的问题模型上:

  after_create :add_to_timeline
  before_save :strip_spaces_from_tags

  private

  def add_to_timeline
    Timeline.create!({content: "An issue was created!", timelineable_id: id, timelineable_type: self.class.to_s})
  end

  def strip_spaces_from_tags
    self.tags.gsub! ", ", ","

  end

这是应用程序控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery

   before_filter :set_locale

   def set_locale
     I18n.locale = params[:locale] || I18n.default_locale
   end
end

我的佣金路线:

timeline_index GET    /:locale/timeline/index(.:format)    :locale/timeline#index
      projects GET    /:locale/projects(.:format)          projects#index
               POST   /:locale/projects(.:format)          projects#create
   new_project GET    /:locale/projects/new(.:format)      projects#new
  edit_project GET    /:locale/projects/:id/edit(.:format) projects#edit
       project GET    /:locale/projects/:id(.:format)      projects#show
               PUT    /:locale/projects/:id(.:format)      projects#update
               DELETE /:locale/projects/:id(.:format)      projects#destroy
        issues GET    /:locale/issues(.:format)            issues#index
               POST   /:locale/issues(.:format)            issues#create
     new_issue GET    /:locale/issues/new(.:format)        issues#new
    edit_issue GET    /:locale/issues/:id/edit(.:format)   issues#edit
         issue GET    /:locale/issues/:id(.:format)        issues#show
               PUT    /:locale/issues/:id(.:format)        issues#update
               DELETE /:locale/issues/:id(.:format)        issues#destroy

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

timeline_index路由到:locale/timeline#index,实际应该路由到timeline#index,不应该吗?

尝试在您的指定中指定控制器和操作,例如

get "timeline/index", to: "timeline#index"

或者您可以尝试使用resources实现相同功能,但请注意将控制器重命名为复数形式,即TimelinesController

resources :timelines, only: [:index]

如果您对这种方法感到不舒服,因为您可能只有一个时间轴,那么您可以尝试

resource :timeline, only: [:show]