【发布时间】:2015-07-26 03:14:15
【问题描述】:
请帮助解决问题。我有嵌套路线:
resources :users do
resources :albums
end
型号:
class User < ActiveRecord::Base
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable
has_many :albums
end
class Album < ActiveRecord::Base
belongs_to :user
end
相册控制器:
def new
@album = Album.new
end
def create
@album = Album.new(album_params)
respond_to do |format|
if @album.save
format.html { redirect_to @album, notice: 'Album was successfully created.' }
format.json { render :show, status: :created, location: @album }
else
format.html { render :new }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
def set_album
@album = Album.find(params[:id])
end
视图/相册/new.html.erb:
<%= form_for [@user, @album] do |f| %>
<%= f.text_area :album %>
<%= f.submit %>
<% end %>
路线:
........
..........
user_albums GET /users/:user_id/albums(.:format) albums#index
POST /users/:user_id/albums(.:format) albums#create
new_user_album GET /users/:user_id/albums/new(.:format) albums#new
edit_user_album GET /users/:user_id/albums/:id/edit(.:format) albums#edit
user_album GET /users/:user_id/albums/:id(.:format) albums#show
PATCH /users/:user_id/albums/:id(.:format) albums#update
PUT /users/:user_id/albums/:id(.:format) albums#update
DELETE /users/:user_id/albums/:id(.:format) albums#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
............
..........
结果我得到以下错误消息:
相册中的 NoMethodError#new 显示 /home/kalinin/rails/phs/app/views/albums/_form.html.erb 其中第 1 行 提出:未定义的方法 `albums_path' 用于
请帮忙制作新表格
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4