0

I have a model order and user.

user - has_many: order

order - belongs_to: user

I want to make a check on the order form.

<% if current_user%>

    form where the name of the order form will be recorded from the user

 <% else%>

    regular order creation form

 <% end%>

the task is how to write data of another model to the model?

2 Answers 2

1

You can do this in the controller

for eg.

Class OrdersController < ApplicationController
  def new
    if current_user
      # this will set the user_id field 
      # you can set additional attributes here
      @order = current_user.orders.build({attribute: value})
    else
      @order = Order.new
    end
  end
  ...

in view

= form_for @order do |f|
  ...
Sign up to request clarification or add additional context in comments.

Comments

0

You can Try accepts_nested_attributes_for

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.