В данный момент у меня функционирует создание зависимого объекта из объекта-родителя: Функция создания Job:
class JobsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@job = @article.jobs.create(job_params)
redirect_to article_path(@article)
end
Job привязан к Article:
class Job < ApplicationRecord
belongs_to :article
end
И в данный момент у меня создается Job прямиком из артикля:
<%= form_with(model: [ @article, @article.jobs.build ]) do |form| %>
<p>
<%= form.label :Job %><br>
<%= form.text_field :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
Мне необходимо создать этот объект из списка артиклей на подобие этого, где идет не наследование из родителя, а выбор этого самого родителя (это по форме похоже, но на деле не работает):
<%= form_with scope: :job do |form| %>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<select name="article_id" id="article_id">
<%= options_from_collection_for_select(Article.all, :id, :title) %>
</select>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
Я так понимаю что мне необходимо сам метод создания в контроллере изменить и саму функцию, а вот как, даже не догадываюсь.
Лог создания через стандартную форму:
Started POST "/articles/7/jobs" for ::1 at 2019-11-09 11:11:10 +0300
Processing by JobsController#create as JS
Parameters: {"authenticity_token"=>"51pyqQ1vpzbQwlqHcEsL2uQ4TBHl654RSSUYL4IY4E
zoFXPtmtpa7OB0TlDX4+AkDlqaLpbjMbHDzq89IXgQNQ==", "job"=>{"text"=>"444"}, "commit
"=>"Создать Job", "article_id"=>"7"}
Article Load (0.3ms) SELECT "articles".* FROM "articles" WHERE "articles"."id
" = $1 LIMIT $2 [["id", 7], ["LIMIT", 1]]
? app/controllers/jobs_controller.rb:4:in `create'
(0.2ms) BEGIN
? app/controllers/jobs_controller.rb:5:in `create'
Job Create (0.4ms) INSERT INTO "jobs" ("text", "article_id", "created_at", "u
pdated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["text", "444"], ["article_
id", 7], ["created_at", "2019-11-09 08:11:10.499077"], ["updated_at", "2019-11-0
9 08:11:10.499077"]]
? app/controllers/jobs_controller.rb:5:in `create'
(0.4ms) COMMIT
? app/controllers/jobs_controller.rb:5:in `create'
Redirected to http://localhost:3000/articles/7
Completed 200 OK in 9ms (ActiveRecord: 1.3ms | Allocations: 3656)
Started GET "/articles/7" for ::1 at 2019-11-09 11:11:10 +0300
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"7"}
Article Load (0.3ms) SELECT "articles".* FROM "articles" WHERE "articles"."id
" = $1 LIMIT $2 [["id", 7], ["LIMIT", 1]]
? app/controllers/articles_controller.rb:11:in `show'
Rendering articles/show.html.erb within layouts/application
Job Load (0.2ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."article_id" = $1
[["article_id", 7]]
? app/views/articles/show.html.erb:7
Rendered collection of jobs/_job.html.erb [3 times] (Duration: 0.5ms | Allocat
ions: 343)
Rendered articles/show.html.erb within layouts/application (Duration: 3.8ms |
Allocations: 2005)
Completed 200 OK in 39ms (Views: 36.1ms | ActiveRecord: 0.5ms | Allocations: 716
2)
Лог добавления через другую форму:
Started POST "/articles" for ::1 at 2019-11-09 11:15:19 +0300
Processing by ArticlesController#create as JS
Parameters: {"authenticity_token"=>"IFFYrRiqaM+GXP0K+mCclOPn03ptxUjXERFYIq1kSA
NADfC46RtU5+KME6m4CXgQqtS7scskoTArxBlDwonXag==", "job"=>{"text"=>"444"}, "articl
e_id"=>"7", "commit"=>"Сохранить Job"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms | Allocations: 639)
ActionController::ParameterMissing (param is missing or the value is empty: arti
cle):
app/controllers/articles_controller.rb:31:in `article_params'
app/controllers/articles_controller.rb:16:in `create'
Routes:
Prefix Verb URI Pattern
Controller#Action
welcome_index GET /welcome/index(.:format)
welcome#index
article_jobs GET /articles/:article_id/jobs(.:format
) jobs#index
POST /articles/:article_id/jobs(.:format
) jobs#create
new_article_job GET /articles/:article_id/jobs/new(.:fo
rmat) jobs#new
edit_article_job GET /articles/:article_id/jobs/:id/edit
(.:format) jobs#edit
article_job GET /articles/:article_id/jobs/:id(.:fo
rmat) jobs#show
PATCH /articles/:article_id/jobs/:id(.:fo
rmat) jobs#update
PUT /articles/:article_id/jobs/:id(.:fo
rmat) jobs#update
DELETE /articles/:article_id/jobs/:id(.:fo
rmat) jobs#destroy
articles GET /articles(.:format)
articles#index
POST /articles(.:format)
articles#create
new_article GET /articles/new(.:format)
articles#new
edit_article GET /articles/:id/edit(.:format)
articles#edit
article GET /articles/:id(.:format)
articles#show
PATCH /articles/:id(.:format)
articles#update
PUT /articles/:id(.:format)
articles#update
DELETE /articles/:id(.:format)
articles#destroy
root GET /
welcome#index
parameters
, которые прилетают в контроллер от первой формы и от второй, вы сами скорее всего сможете написать всё верно.