我正在尝试使用嵌套路由执行 form_for,但无法正常工作。
这是routes.rb
Rails.application.routes.draw do
resources :users do
resources :tasks
end
查看/任务/编辑
<h1>Editing task</h1>
<%= form_for [@task.user, :task], method: :put do |f| %>
<p>
<%= f.label :Title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :Description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<%= link_to 'Back to List', user_tasks_path %>
<% end %>
耙路线
Prefix Verb URI Pattern Controller#Action
root GET / users#index
user_tasks GET /users/:user_id/tasks(.:format) tasks#index
POST /users/:user_id/tasks(.:format) tasks#create
new_user_task GET /users/:user_id/tasks/new(.:format) tasks#new
edit_user_task GET /users/:user_id/tasks/:id/edit(.:format) tasks#edit
user_task GET /users/:user_id/tasks/:id(.:format) tasks#show
PATCH /users/:user_id/tasks/:id(.:format) tasks#update
PUT /users/:user_id/tasks/:id(.:format) tasks#update
DELETE /users/:user_id/tasks/:id(.:format) tasks#destroy
Rails 给出一个错误:未定义的方法 `model_name' for :task:Symbol
任务控制器.rb
class TasksController < ApplicationController
def index
@tasks = Task.where(user_id: params[:user_id])
end
def show
@task = Task.find(params[:id])
redirect_to users_path
end
def new
@task = Task.new
end
def create
@task = Task.new(task_params)
if @task.save
redirect_to @task
else
render 'new'
end
end
def edit
@task = Task.find(params[:id])
end
def update
@task = Task.find(params[:id])
if @task.update(task_params)
redirect_to [@task.user, @task]
else
render 'edit'
end
end
def destroy
@task = Task.find(params[:id])
@task.destroy
respond_to {|format| format.js }
end
private
def task_params
params.require(:task).permit!
end
end
有什么帮助吗?