Reputation: 412
I have a Rails 4 application which has the following code snippets:
config/routes.rb
concern :messageable do
resources :messages, only: [:create, :destroy]
end
namespace :recruiter do
resources :applications, only: :show, concerns: :messageable
end
In a particular view companies/application/_messages.html.erb
(CompaniesController
with application
acting as a partials folder - The application
variable used here is passed using locals
)
<%= form_for [current_recruiter, application, Message.new], remote: true do |f| %>
generates
<form accept-charset="UTF-8" action="/recruiter/applications/4/messages" class="new_message" data-remote="true" id="new_message" method="post">
Again
<%= form_for recruiter_application_messages_path(application.id), remote: true do |f| %>
generates
<form accept-charset="UTF-8" action="/companies/2" data-remote="true" method="post">
For the record, I feel that the "4" in /recruiter/applications/4/messages
is being taken from the value of current_recruiter.id
. What I am looking for instead is the value of application.id
(in a particular case, 16) in that placeholder. Could anyone help me with debugging this route helper?
Relevant Output of rake routes
:
recruiter_application_messages POST /recruiter/applications/:application_id/messages(.:format) recruiter/messages#create
recruiter_application_message DELETE /recruiter/applications/:application_id/messages/:id(.:format) recruiter/messages#destroy
Upvotes: 0
Views: 79
Reputation: 29599
I was thinking of adding this as a comment but since it's a bit long, I hope it's fine to place this as an answer.
Your first form_for
looks like
form_for [current_recruiter, application, Message.new], remote: true do |f|
I am assuming that current_recruiter
is an ActiveRecord
object so this translates to recruiter_application_path
which conflicts with the defined route. I've made some experiment using the same code in an existing project. In the console, I ran the following
>> app.recruiter_application_messages_path(1)
=> "/recruiter/applications/1/messages"
>> app.recruiter_application_messages_path(1, 2)
=> "/recruiter/applications/1/messages.2"
>> app.recruiter_application_messages_path(1, Application.new)
=> "/recruiter/applications/1/messages"
So my guess is that there's a route conflict here and application
is a new object. It's a lot of ifs but I think that's your current scenario. In order to use the route declared with a namespace, make sure that application
is a persisted object and change the form_for
to
form_for [:recruiter, application, Message.new], remote: true do |f|
For the second form_for
, I think you just forgot to pass the first argument which is the new message object
form_for Message.new, url: recruiter_application_messages_path(application.id)
Upvotes: 1