新手问题 问一下 route socpe

tdseed · 2013年11月08日 · 最后由 tdseed 回复于 2013年11月08日 · 3705 次阅读

writings 中的路由

scope '/~:space_id', :module => 'dashboard', :as => 'dashboard' do
   root :to => 'articles#index'

   resource :billing, :only => [:show]
   resources :orders, :only => [:index, :new, :create, :show, :destroy] do
     collection do
       post :alipay_notify
     end
   end

我把控制器简单写成

class SpacesController < ApplicationController

  def new
    @space = Space.new
  end

  def create
    @space = Space.new(params[:space])
    if @space.save
      redirect_to dashboard_root_path(@space)
    else
      render :action => "new"
    end
  end

end

错误

wrong constant name ~527c3dfd1d41c84144000003Controller

rails3 的环境 如何做到像 writings 的路由分配效果?@rei

把控制器简单写成 太跳跃了,不知道简化了什么。

#1 楼 @Rei 最简单的写法要如何达到这种分配,求最小化 demo

最简单的写法要如何达到 这种 分配

不理解在问什么,建议看看提问的智慧。

#3 楼 @Rei 就是一个用户申请了个域名,比如是 demo,然后进入后台编辑,比如要设置空间的时候类似于这种路由 writing.io/~demo/settings,现在的情况是redirect_to dashboard_root_path(@space),出现这个错误 wrong constant name ~527c3dfd1d41c84144000003Controller

#4 楼 @tdseed 那你写的路由是什么,你贴 writings 的路由和你自己的代码我看不出来什么问题啊。

#5 楼 @Rei space_id 为什么变成了一串字符?反正很多不明白。。。不知道关键点在哪

Space model 里面定义了 to_param 方法,这是转换 space_id 时用的。

#7 楼 @Rei 好的,加了这个方法,为啥 route 写成 scope '/:', 冒号后随便加东西 也都是 wrong constant name demoController 这个错误 加不加 space_id 都是这样,重启服务器也一样

#8 楼 @tdseed show me the code.

#9 楼 @Rei model

class Space
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name
  field :domain
  field :full_name
  field :description
  field :storage_used, :default => 0


  has_many :mobile_articles, :dependent => :delete
  # has_many :mobile_attachments, :dependent => :destroy
  belongs_to :shop

  index({ :shop_id => 1 })
  index({ :name => 1 }, { :unique => true })
  index({ :domain => 1 }, { :unique => true, :sparse => true})

  def to_param
    name.to_s
  end

end

view space/new.html.erb

<div>空间设置</div>
<%= form_for @space, :url => '/mobile_spaces/create', :validate => true, :html => { :data => { :remote => true } } do |f| %>
  <%= f.label "URL" %>
  <%= f.text_field :name, :tabindex => 1, :class => "field", :placeholder => 'space-name'
       %>.localhost
       <br>
  <%= f.label "全名" %>
  <%= f.text_field :full_name, :class => "field", :tabindex => 2 %>
      <br>
  <%= f.label "描述" %>
  <%= f.text_area :description, :class => "field", :tabindex => 3 %>
      <br>
  <%= f.submit "提交", :tabindex => 4%>

<% end %>

route 和 controller 就是和最上面的一样。。

抱歉,我还是看不懂问题是什么。可能你觉得我写过 writings 对这个问题了如指掌,实际是这个项目我有 1 个月没碰了,跟陌生人的代码没什么两样。所以你用“这个”,“上面一样”这种模棱两空的词语我是无法猜测你想要做什么的。

请整理你的问题:

  1. 你想要完成什么功能
  2. 你写了哪些代码
  3. 结果出现什么错误,你觉得哪段代码没有按预期工作

我相信这篇文章对你有用:提问的智慧 http://www.wapm.cn/smart-questions/smart-questions-zh.html

#11 楼 @Rei 好吧,可能让你痛苦了

功能:用户注册完后,进入输入域名的流程,输入域名 demo 后点提交,然后就会有 localhost/~demo/ 这个时候就出错 wrong constant name ~demoController

预期:

错误:

#12 楼 @tdseed 贴 routes 代码和 redirect 的代码

- -。我觉得你应该把 route 里相关部分的代码也晒一下。

15 楼 已删除

如果是顶楼代码,改成

redirect_to dashboard_root_path(:space_id => @space)

看看。我好像看明白在做什么了,是把 js.erb 的内容移到 controller。

#14 楼 @edgar_wang_cn 就是问题里的代码,跟 writings 中的一样

#17 楼 @tdseed 你是直接用的 writings 的代码?

#18 楼 @edgar_wang_cn 用的是最上面的那段 route

#19 楼 @tdseed 你写了类似 writings 里的 app/controllers/dashboard/base_controller.rb 的 controller 了么?

#16 楼 @Rei 是的 rails3 环境,没用 Turbolinks,当然不写 Turbolinks.visit('<%= dashboard_root_path(@space) %>'),routes 就用的顶楼的代码

#20 楼 @edgar_wang_cn 没有呢,还得研究一下,多交流

需要 登录 后方可回复, 如果你还没有账号请 注册新账号