A
A
Alinka2002017-08-06 05:14:12
Ruby on Rails
Alinka200, 2017-08-06 05:14:12

How and what parameters should be passed to the form if the route is nested?

Good evening.
There is a user, a comment model has been added to the user, all associations are spelled out correctly, the route is nested (comments are nested in the jureza).
When creating a form - writes the method does not exist.
I write like this:

<%= form_for ([@user, @user.comments.build])  do |f| %>
  <%= f.label :body %>
  <%= f.text_field :body %>
  <%= f.text_field :user_id %>
  <%= f.submit' %>
<% end %>

In the comment controller, this is the code:
@user = User.find(params[:user_id])
    @comment = @user.comments.create(send_params)

I understand that in the form we need to pass the user as the first parameter and the second as a way to indicate that this user has an association with comments.
Am I wrong somewhere?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artur Bordenyuk, 2017-08-06
@HighQuality

What mistake? What's in the new comment controller? How defined routes? Why does the submit button have an extra quote in the form? )
<%= f.submit' %>

S
sumskoydmitry, 2017-08-12
@sumskoydmitry

Can be in the form to specify simply the user and object of the comment?
Because in the controller you still create it directly through the user.
Well, this is just a guess, I myself don’t shine with experience yet, but on such questions, additional experience is filled just right.
And what for in the form this field? If to get the user in the controller, then he will come in the same way in the parameters. <%= f.text_field :user_id %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question