0

In my Rails 6 app, I have a Product and Order model.

On my products#show page, I have some fields and a button. What I need to do is send the info to the orders#new page so that this data is shown on the orders#new page.

I have tried to write some code based on Pass variables without model associations in Rails and how to pass parameters in params using form_tag method in rails, but my code seems completely wrong.

on products#show:

<%= form_tag(new_order_path do |form| %>
  <%= form.input_field :comments %>
  <%= form.button %>
<% end %>

With this code I get undefined method text_field' for nil:NilClass`.

I have tried adding attr_accessor :comments to both the Product and Order model, but it doesn't help.

I don't think that my approach or what I am trying to code is right. I was just trying to piece together parts from these answers.

Can someone please help me figure out the best way I can pass this data to Orders#new to show in that view?

1
  • 1
    You shouldn't be doing a POST to the new action of OrdersController, use method: :get as indicated by @h4ppyr0gu3. To initialise a new Order object with the sent values you can access the params. Commented Sep 2, 2021 at 9:23

1 Answer 1

1
<%= form_with url: "/search", method: :get do |form| %>
  <%= form.label :query, "Search for:" %>
  <%= form.text_field :query %>
  <%= form.submit "Search" %>
<% end %>

from the docs

https://guides.rubyonrails.org/form_helpers.html

and the show path also requires an id

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.