Reputation: 2169
I can't figure out how to correctly write a url path in form_for for creating a new object. I tried to do it different ways, but no luck. I suppose there is some particular form of url's that can include objects' id's.
views/tasks/new.html
<%= form_for :task, url: [@task.user, @task] do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.hidden_field :user_id, value: params[:user_id] %>
<%= f.submit %>
</p>
<% end %>
rake routes
controllers/tasks_controller.rb
class TasksController < ApplicationController
before_filter :authorize, only: [:edit, :new, :destroy]
def index
@tasks = Task.where(user_id: params[:user_id])
end
def show
@task = Task.find(params[:id])
redirect_to users_path
end
def edit
@task = Task.find(params[:id])
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 update
@task = Task.find(params[:id])
if @task.update(task_params)
redirect_to @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(:title, :description)
end
end
views/tasks/index.html
<p align="right"><%= link_to 'Users', users_path %> <%= link_to 'Tasks', user_tasks_path %></p>
<h3>Tasks database</h3>
<table>
<% @tasks.each do |task| %>
<tr id="task_<%= task.id %>">
<td>
<b>Title:</b>
<i> <%= task.title %></i>
</td>
<td>
<b>Description: </b>
<i><%= task.description %></i>
</td>
<td>
<%= link_to 'Edit', edit_user_task_path(task.user, id: task.id) %>
</td>
<td>
<%= link_to 'Delete', user_task_path(task.user, id: task.id),
data: { confirm: 'Are you sure?' }, :method => :delete, remote: true %>
</td>
</tr>
<% end %>
<tr>
<td><%= link_to 'Create task', new_user_task_path %></td>
<td><%= link_to 'Back to Users', users_path %></td>
</tr>
</table>
Can anybody help?
Upvotes: 2
Views: 418
Reputation: 6398
Seeing the routes it seems that you need to allow either the admin/user to create tasks for any user. So for this first add a before_action
in your controller:
before_action :find_user
def find_user
@user = User.find(params[:user_id])
end
Then in you form do it like:
<%= form_for [@user, @task] do |f| %>
Then in the create
action of your controller do it like:
def create
@task = @user.tasks.build(task_params)
if @task.save
redirect_to @task
else
render 'new'
end
end
The above can be used when you need to save tasks for many user and if you need that only the current_user
tasks should be saved you can go with the above answers.
Upvotes: 1
Reputation: 33542
You have nested_routes
for tasks
and users
, so changing your form_for
like below should solve your problem
<%= form_for [current_user, @task] do |f| %>
Upvotes: 3
Reputation: 1601
Please can you try this:
<%= form_for([current_user, @task]) do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.hidden_field :user_id, value: params[:user_id] %>
<%= f.submit %>
</p>
Upvotes: 1