请帮助解决问题。我有嵌套路线:
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 Showing /home/kalinin/rails/phs/app/views/albums/_form.html.erb其中第1行 提出:未定义的方法`albums_path&#39;对
&LT;#:0x007fdbe06745a8&GT;
请帮助制作新表格
答案 0 :(得分:1)
未定义的方法`albums_path'
错误是因为您没有在控制器new
方法中初始化@user
,因此<%= form_for @album %>
nil 。因此 Rails 会将其视为albums_path
,因为没有<%= form_for [current_user, @album] do |f| %>
而触发该错误。
尝试
public function store()
{
$rows = Input::get('rows');
$userId = Auth::id();
for ($i = 0; $i < (count($rows) - 1); $i += 2) {
$item = new Multiple([
'user_id' => $userId,
'link' => $rows[$i]['link'],
'store' => $rows[$i + 1]['store'],
]);
$item->save();
}
}