Jean-Francois Labbe
2012-Jun-25 11:04 UTC
[Associations] create a task for a project from project index view
Hello, Let say, i have an association between a project and a task. A project has_many taks and a task belongs_to a project. My model is working great. I can create tasks from project and so on but my problem comes when i want to add a button on the project index view to add task to a project. What i want is something like that (project index view) Project 1 Show, edit, destroy, add_task Project 2 Show, edit, destroy, add_task So that when i click add_task of project1 i''m redirected to a new task form to create a task for that project. i''ve added <%= link_to ''add_task'', new_task_path(:project_id => project) %> in the project index view so when i click on add_task, it redirects me to /task/new?project_id=1 but when i submit the task, the task is not linked to the project. How can i get the project_id? Does anyone has a tutorial or an example of how should i do? Thanks, jeff -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2012-Jun-26 15:42 UTC
Re: create a task for a project from project index view
On Jun 25, 12:04 pm, Jean-Francois Labbe <jeff...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > Let say, i have an association between a project and a task. > A project has_many taks and a task belongs_to a project. > My model is working great. > I can create tasks from project and so on but my problem comes when i > want to add a button on the project index view to add task to a > project. > > What i want is something like that > (project index view) > > Project 1 Show, edit, destroy, add_task > Project 2 Show, edit, destroy, add_task > > So that when i click add_task of project1 i''m redirected to a new task > form to create a task for that project. > > i''ve added <%= link_to ''add_task'', new_task_path(:project_id => > project) %> in the project index view > > so when i click on add_task, it redirects me to /task/new?project_id=1 > > but when i submit the task, the task is not linked to the project. > How can i get the project_id? >Short answer: your form needs to submit it. Typically one does this by having routes that look like resources projects do resources :tasks end then new_project_task_path(some_project) will be /projects/123/tasks/ new and you''ll get params[:project_id] set to 123 if you set @project based on this and make your form something like form_for [@project, Task.new] do |f| ... then the form will be posted to /projects/123/tasks and you will once again have params[:project_id] set to 123 for you. Fred> Does anyone has a tutorial or an example of how should i do? > > Thanks, > > jeff-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.