ruby on rails:将子记录添加到现有父级而不访问父级

时间:2021-07-24 07:15:12

How would you handle allowing users to add child objects to parents without requiring them to navigate to the parent?

您将如何处理允许用户将子对象添加到父级而不需要他们导航到父级?

For example adding a journal article without having to navigate to the journal and issue first? The parent also may not exist yet.

例如,添加期刊文章而不必导航到期刊并首先发行?父母也可能不存在。

I'd prefer not to make data entry to onerous for users so making them find a journal or create it and then find or create an issue seems a bit much. I would rather just have a single form with journal and issue fields. The logic would simple if were not for the fact that users are interested in the article and not the journal or issue.

我宁愿不让数据输入繁琐的用户,因此让他们找到一个日志或创建它然后找到或创建一个问题似乎有点多。我宁愿只有一个包含日记和问题字段的表单。如果不是因为用户对文章而不是期刊或问题感兴趣,那么逻辑就很简单了。

Would you just modify a create for the child so it finds or creates the parents ?

您是否只需修改子项的创建,以便找到或创建父项?

1 个解决方案

#1


I'm assuming you have a journal model that has many articles and an article model that belongs to a journal. First, you should create routes to the articles controller that go through journals and not through journals:

我假设您有一个期刊模型,其中包含许多文章和属于期刊的文章模型。首先,您应该创建通过期刊而不是通过期刊的文章控制器的路线:

ActionController::Routing::Routes.draw do |map|
  map.resources :journals, :has_many => :articles
  map.resources :articles
end

Now you can get to the new action of your articles controller with the URL /articles/new or /journals/1/articles/new. Then in the new action in your articles controller, you do this:

现在,您可以使用URL / articles / new或/ journals / 1 / articles / new访问文章控制器的新操作。然后在文章控制器中的新操作中,执行以下操作:

@article = Article.new(:journal_id => params[:journal_id])

Which set the journal_id of the article to whatever parameter is passed in. If no parameter is passed, the journal_id will be nil.

将文章的journal_id设置为传入的参数。如果没有传递参数,则journal_id将为nil。

In the ERB template, you just do this to create the drop down:

在ERB模板中,您只需执行此操作即可创建下拉列表:

<%= f.collection_select :journal_id, Journal.all(:order => "name"), :id, :name, :include_blank => true %>

And then a user can select a journal, but if one was passed in, it will be pre-selected to the correct value.

然后用户可以选择日记,但如果传入日记,则会预先选择正确的值。

#1


I'm assuming you have a journal model that has many articles and an article model that belongs to a journal. First, you should create routes to the articles controller that go through journals and not through journals:

我假设您有一个期刊模型,其中包含许多文章和属于期刊的文章模型。首先,您应该创建通过期刊而不是通过期刊的文章控制器的路线:

ActionController::Routing::Routes.draw do |map|
  map.resources :journals, :has_many => :articles
  map.resources :articles
end

Now you can get to the new action of your articles controller with the URL /articles/new or /journals/1/articles/new. Then in the new action in your articles controller, you do this:

现在,您可以使用URL / articles / new或/ journals / 1 / articles / new访问文章控制器的新操作。然后在文章控制器中的新操作中,执行以下操作:

@article = Article.new(:journal_id => params[:journal_id])

Which set the journal_id of the article to whatever parameter is passed in. If no parameter is passed, the journal_id will be nil.

将文章的journal_id设置为传入的参数。如果没有传递参数,则journal_id将为nil。

In the ERB template, you just do this to create the drop down:

在ERB模板中,您只需执行此操作即可创建下拉列表:

<%= f.collection_select :journal_id, Journal.all(:order => "name"), :id, :name, :include_blank => true %>

And then a user can select a journal, but if one was passed in, it will be pre-selected to the correct value.

然后用户可以选择日记,但如果传入日记,则会预先选择正确的值。