新手问题 rails3 的 controller 只能对应同名的 model 吗?

MIx · 2013年10月27日 · 最后由 edgar_wang_cn 回复于 2013年10月27日 · 2068 次阅读

例如我现在 model 定义为 UserAccount,controller 代码为:

class UsersController < ApplicationController
  def home
  end

  def new
    @user = UserAccount.new
  end
end

这时在 new.html.erb 中用

<%= form_for @user do |f| %>
...
<% end %>

时,就会出现

undefined method `user_accounts_path'

的错误。

这里你要手工指定 url,

<%= form_for @user, :url => [自己指定] do |f| %>
  ...
<% end %>

xxx_path 这种有没有要看你的 config/route.rb 里有没有配置,和模型名没关系,和控制器名字有关,可以通过在命令行里 rake routes 查看

@edgar_wang_cn 你的头像…你的 id…难道我们同名…

Convention over configuration

可以在 form_for 中使用 as: 'user', 不想每次都写 as 的话也可以这样:

class UserAccount < ActiveRecord::Base
  @_model_name = "user"
end

#2 楼 @hegwin 我头像上的字和我的名字没有关系的╮( ̄▽ ̄)╭

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