Rails 3 为特定父级创建新子级

发布于 2024-11-10 12:07:58 字数 723 浏览 2 评论 0原文

嘿大家!刚刚加入了 stack Overflow,因为在开始学习 Ruby on Rails 3 时,它是一个有用的资源。

不过,我似乎找不到一个特定的答案,也许我所知道的都是错误的树,但希望你们能帮我解决这个问题。首先是有关我正在使用的内容的一些信息:
- 在我的网络应用程序中,我有 2 个模型:一对多关系中的项目和任务。
- 项目有很多任务,任务属于项目
- 任务不是嵌套资源,因为用户需要能够查看所有当前任务,无论它们属于哪个项目。
- 因此,routes.rb 现在看起来像这样:
资源:项目
resources :tasks

在项目显示视图中,我显示与该项目关联的任务列表。下面有一个用于创建新任务的 link_to ,看起来像 <%= link_to 'New Task', new_task_path, :class =>; “新-btn”%>。 link_to 将用户带到新视图以创建新任务。渲染的 _form 视图以以下内容开头: <%= form_for(@task) do |f| %>。

现在,我认为我需要将项目 ID 从项目显示视图传递到新任务视图:但是,这就是我迷失的地方,并且可能有点混乱。

有人可以给我指出正确的方向吗:也许是一个资源,概述了执行此操作所涉及的所有步骤,或者甚至可以在这里提供该过程所涉及的步骤的概述。

非常感谢!

Hey all! Just joined up on stack overflow, as it has been a helpful resource while starting to learn about Ruby on Rails 3.

I can't seem to find one particular answer though, and maybe I'm barking up the wrong tree for all I know, but hopefully you folks can sort me out here. First some info on what I'm working with:
- In my web app I have 2 models: Projects and Tasks in a one-to-many relationship.
- Projects has many Tasks, and Tasks belong to Project
- Tasks IS NOT a nested resource, as users need to be able to see all current tasks, regardless of which project they are for.
- routes.rb therefore looks like this right now:
resources :projects
resources :tasks

In the project show view I display a list of tasks associated with that project. below that there is a link_to for creating a new task that looks like <%= link_to 'New Task', new_task_path, :class => "new-btn" %>. The link_to takes user to the new view for creating a new task. The rendered _form view starts with: <%= form_for(@task) do |f| %>.

Now, I think I need to pass the project id from the project show view, to the new task view: but, this is where I am getting lost and possibly, a bit mixed up.

Could someone please point me in the right direction: maybe to a resource outlining all steps involved in doing this, or maybe even provide an outline of the steps involved in the process here.

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帥小哥 2024-11-17 12:07:58

您可以创建嵌套路线,并且仍然能够显示所有任务,无论项目如何。只需确保稍后定义任务资源,例如

resources :projects do |p|
  resources :tasks
end
resources :tasks

现在在项目索引或显示视图中,您可以创建链接,例如

link_to 'New Task', new_project_task_path(project)

任务索引视图可能有点棘手。一切都取决于您如何对这些任务进行排序。例如,如果您显示它们按项目排序,那么您可以创建一个类似于

link_to 'New Task', new_project_task_path(task.project)

表单的链接。在新操作中,您必须从参数中获取 id 并将其放入任务对象中

if (params[:project_id])
  @task.project_id = params[:project_id]

在表单视图中,您可以创建隐藏字段来保存此值

f.hidden :project_id

这将使其工作,但如果不提供,您将无法转到新任务项目。更好的解决方案是为所有项目创建一个选择字段。为了实现这一点,你应该执行以下操作:

在 models/project.rb

def getProjectsList
  projects = Project.all
  projects.map do |p|
    [project.name, project_id]
  end
end

在controllers/application_controller.rb

def find_projects
  @projects = Project.new.getProjectsList
end

在controllers/tasks_controller.rb 在开头

before_filter :find_projects, :only => [:new, :edit, :update, :create]

在views/tasks/_form

f.select :project_id, @projects

这样你就可以随时选择项目,以防万一有一个给定的项目在参数中它将已经被选择

You may create nested routes and still be able to show all tasks regardless of the project. Just make sure you have task resource defined later, like

resources :projects do |p|
  resources :tasks
end
resources :tasks

Now in projects index or show view you can create link like

link_to 'New Task', new_project_task_path(project)

The task index view may be a little bit tricky. All depends on how you sort these tasks. If, for example, you show them sorted by project then you can create a link like

link_to 'New Task', new_project_task_path(task.project)

As for forms. In new action you have to get the id from params and put it into task object

if (params[:project_id])
  @task.project_id = params[:project_id]

In form view you may create hidden field that will save this value

f.hidden :project_id

This will make it work, but you will not be able to go to the new task without providing project. Better solution it would be to create a select field with all projects. To accomplish that you should do the following:

in models/project.rb

def getProjectsList
  projects = Project.all
  projects.map do |p|
    [project.name, project_id]
  end
end

in controllers/application_controller.rb

def find_projects
  @projects = Project.new.getProjectsList
end

in controllers/tasks_controller.rb at the begining

before_filter :find_projects, :only => [:new, :edit, :update, :create]

in views/tasks/_form

f.select :project_id, @projects

This way you can always select project and in case there is one given in params it will be already selected

此刻的回忆 2024-11-17 12:07:58

如果 current_user 返回 User 对象,那么您应该能够调用

current_user.projects 

以获取所有用户的项目。

定义用户和任务之间的关系可能有效(尽管我不确定这一点)。

#models/user.rb
has_many :projects
has_many :tasks, :through => :projects

在这种情况下,只需

current_user.tasks 

返回用户的任务

If current_user returns User object then you should be able to call

current_user.projects 

to get all user's projects.

Defining a relation between user and task may be working (though I am not sure this one).

#models/user.rb
has_many :projects
has_many :tasks, :through => :projects

In this case simply

current_user.tasks 

should return user's tasks

梦巷 2024-11-17 12:07:58

有很多方法可以做到这一点。一个简单的方法可能是向链接添加一个参数并在控制器中使用它:

在您的视图中:

<%= link_to 'New Task', new_task_path(:project_id => @project.id), :class => "new-btn" %>

在您的任务控制器中:

def new
  @task = Task.new(:project_id => params[:project_id])
end

There are many ways to do it. An easy one may be to add a parameter to you link and use it in the controller:

In your view:

<%= link_to 'New Task', new_task_path(:project_id => @project.id), :class => "new-btn" %>

In your tasks controller:

def new
  @task = Task.new(:project_id => params[:project_id])
end
与他有关 2024-11-17 12:07:58

将项目 ID 从项目页面传递到“新建任务”页面的一种方法是将其添加到 URL 上的查询字符串中。示例 HTML 如下所示:

<a href="/tasks/new?project_id=123" class="new-btn">New Task</a>

要让 Rails 生成该 HTML,您可以在 ERB 中执行此操作:

<%= link_to 'New Task', new_task_path(:project_id=>@project.id), :class => "new-btn" %>

接下来,您需要将项目 ID 从“新建任务”页面传递到实际创建任务的操作。一种方法是在表单中创建一个包含项目 ID 的隐藏输入,以便在提交表单时将其与其他参数一起传递。 HTML 看起来像:

<input type="hidden" name="project_id" value="<%= params[:project_id] %>" />

要以 Rails 方式执行此操作,您可以在 TasksController 中的新任务操作中设置项目 ID:

@task.project_id = params[:project_id]

然后在 form_for 块内的视图中执行类似的操作(我不是 100% 确定)语法):

<%= f.hidden_field(:project_id) %>

One way to pass the project ID from the project page to the New Task page is to add it to the query string on your url. Example HTML would look like this:

<a href="/tasks/new?project_id=123" class="new-btn">New Task</a>

To get Rails to generate that HTML, you can do this in your ERB:

<%= link_to 'New Task', new_task_path(:project_id=>@project.id), :class => "new-btn" %>

Next, you need to pass the project ID from New Task page to the action that actually creates the Task. One way to do that would be to make a hidden input inside your form that contains the project ID so that it will be passed along with the other parameters when the form is submitted. The HTML would look like:

<input type="hidden" name="project_id" value="<%= params[:project_id] %>" />

To do this the Rails way, you can set the project ID in the new task action in the TasksController:

@task.project_id = params[:project_id]

and then do something like this in your view inside the form_for block (I'm not 100% sure on the syntax):

<%= f.hidden_field(:project_id) %>
假装不在乎 2024-11-17 12:07:58

哇!感谢大家提供的所有重要信息!通过这个过程,我确实学到了一些巧妙的技巧。

这是我现在正在做的事情:

routes.rb

resources :projects do |p|  
    resources :tasks  
end  
resources :tasks

我真的很高兴得知这部分是可能的。现在我可以享受嵌套资源的好处,但也可以使用原始的非嵌套路由来执行任务。

显示项目视图中的 link_to

<%= link_to 'New Task', new_project_task_path(@project), :class => "new-btn" %>

tasks_controller.rb 新操作

if (params[:project_id])
      @task.project_id = params[:project_id]
    end

新任务表单隐藏字段

<%= f.hidden_field :project_id %>

这一切都非常适合向项目添加新任务:但是,显示与所有项目相关且与当前用户相关的所有任务的列表有点棘手,我想知道是否有比我想出的更好的方法:

索引操作中的tasks_controller.rb

@projects = Project.find_all_by_user_id(current_user)
@tasks = Array.new
@projects.each do |p|
    p.tasks.each do |t|
        @tasks << t
    end
end

我正在使用“devise”和“cancan”gems 进行用户管理(两者都有太棒了!)。上面的“current_user”正是您所期望的:当前登录的用户。这是一个合理的解决方案,还是有更好的方法来为用户获取所有任务?

万一:

User has_many Projects, and Project has_many Tasks

Wow! Thanks for all the great info guys! I definitely learned a few neat tricks going through this.

Here is what I have working now:

routes.rb

resources :projects do |p|  
    resources :tasks  
end  
resources :tasks

I'm really happy to learn that that part is possible. Now I can enjoy the benefits of nested resource, but use the original non-nested routes for tasks, as well.

the link_to in show project view

<%= link_to 'New Task', new_project_task_path(@project), :class => "new-btn" %>

tasks_controller.rb new action

if (params[:project_id])
      @task.project_id = params[:project_id]
    end

new task form hidden field

<%= f.hidden_field :project_id %>

That all works great for adding new tasks to projects: but, showing a list of all tasks, related to all projects, that are related to the current user was a little bit trickier, and I wonder if there might be a better way than what I came up with:

tasks_controller.rb in the index action

@projects = Project.find_all_by_user_id(current_user)
@tasks = Array.new
@projects.each do |p|
    p.tasks.each do |t|
        @tasks << t
    end
end

I'm using the "devise" and "cancan" gems for user management(both have been great!). The "current_user" above is simply what you would expect: the currently logged in user. Is this a reasonable solution, or is there a better way of getting all tasks for a user?

Just in case:

User has_many Projects, and Project has_many Tasks
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文