0

Newbie Here!

I'm unsuccessfully trying to submit a form of a nested model.

Aim:

Verify a translation by submitting a boolean to a reviews model to associate whether a translation is verified or not, with translation entry and user model associated references.

# routes.rb
resources :entries do
   resources :reviews
end


# entry.rb
belongs_to :user
has_one :review
accepts_nested_attributes_for :review


# user.rb
has_many :entries
has_many :reviews


# review.rb
belongs_to :user
belongs_to :entry

From entry index, pass the entry instance to partial, works perfect

# /entries/index.html.erb
<% @entries.each do |entry| %>
   ...
   <%= render 'reviews/new', entry: entry %>
   ...
<% end %>

Unsuccessfully setting up new/create form. What happens is that the entry instance is well received, but I am failing to create a new model instance for review belonging to entry.

entry.review raises an nil error for review, while entry is fine when testing through browser console

First argument in form cannot contain nil or be empty

# reviews/_new.html.erb
<span>
  <%= form_for entry.review do |f| %>
    <div class="form-check form-switch">
      <%= f.check_box :verified, class: "form-check-input" %>
    </div>
    <%= f.submit class: "btn btn-primary"%>
  <% end %>
</span>

Another attempt was also to use just @review from the controller but that doesn't obey nested routes.

My controller looks like this

# reviews_controller.rb

def create
    @entry = Entry.find(params[:entry_id])
    @review = @entry.review.build(review_params)
    @review.user_id = current_user.id
    @review.save
end

private

def review_params
   params.require(:review).permit(:verified, user: current_user, entry: @entry)
end

Am I suppose to implement my actions in the entries_controller?

I have also found the tutorial here useful but replication was unsuccessful.

Another StackOverflow reference here

I still get the error entry.review.build that review is nil.

First argument in form cannot contain nil or be empty

axelmukwena
  • 779
  • 7
  • 24

1 Answers1

1

When building an associated record over a has_one relation, instead of

@review = entry.review.build(review_params)

you need to use the following:

@review = entry.build_review(review_params)

See the documentation for more details.

Am I suppose to implement my actions in the entries_controller?

It depends on what you're after. If you have a dedicated form for adding a new review and it is not embedded in another form for creating or updating an entry then implementing a create action in ReviewsController is the straightforward solution – in this case you should also not need accepts_nested_attributes_for in Entry.

If, however, you want to be able to create or update an entry as well as its review using the same form, then you should nest the review form in the form of the entry, keep accepts_nested_attributes_for, and use actions in EntriesController. The documentation should get you started there.

Mate Solymosi
  • 5,699
  • 23
  • 30