0

My web app follows the structure as described in previous questions here and here.

See the picture below of the interface.

What I want is, when the user edits/updates verification through the switch icon (submit button automatically), also described here, the div containing the verify/patch icon should reload/refresh to update that a translation is verified.

I could possibly have all these elements in one field, but only moderator: true users get to create and only moderators who moderated specific translations can edit and update the review.

What I tried is having

# entries/index.html.erb
<% @entries.each do |entry| %>
  ...

  <span id="show-review-<%= entry.id %>">
    <%= render 'reviews/show', entry: entry %>
  </span>

  ...

  <% if current_user.moderator %> # and other conditions
    <div id="edit-review-form-<%= entry.id %>">
      <%= render 'reviews/edit', entry: entry %>
    </div>
  <% end %>

  ...
<% end %>
# reviews/_show.html.erb
# Simply if the review exists and if its true
<% if entry.review and entry.review.verified == true %>
  # blue verify icon
<% else %>
  # gray vefify icon
<% end %>
# reviews/_edit.html.erb
<%= form_for([entry, entry.review], remote: true, :authenticity_token => true) do |f| %>
   ...
<% end %>
# reviews/edit.js.erb
$('show-review-<%= entry.id %>')[0].html("<%=j render 'reviews/show', entry: entry %>")

Problem: When I submit the edit form, and update action in the controller is executed, I would like to reload the section of show. If I have my controller in this way...

def update
  if @entry.review.update!(review_params)
    respond_to do |format|
      format.html { render :partial => "reviews/show", :layout => false }
      format.js { render :template => "reviews/edit", :layout => false }
    end
  end
end

Having this, on update, my site instead redirects to a reviews URL http://localhost:3000/entries/entry:id/reviews/review:id

This is a nested, kinda, model. Why does is redirect to show when it's just a partial?

# rake routes
entry_reviews     GET    /entries/:entry_id/reviews(.:format)             reviews#index
                  POST   /entries/:entry_id/reviews(.:format)             reviews#create
new_entry_review  GET    /entries/:entry_id/reviews/new(.:format)         reviews#new
edit_entry_review GET    /entries/:entry_id/reviews/:id/edit(.:format)    reviews#edit
entry_review      GET    /entries/:entry_id/reviews/:id(.:format)         reviews#show

enter image description here

axelmukwena
  • 779
  • 7
  • 24
  • 1
    Probably a typo, and `reviews/edit/js.erb` is meant to be `reviews/edit.js.erb`. – Jussi Hirvi Feb 21 '21 at 16:23
  • Have you tried `reviews/update.js.erb`and have it render your partials just like your `edit.js.erb` does? If you try that, you can probably simplify your `respond_to` section. – Jussi Hirvi Feb 21 '21 at 16:24

1 Answers1

0

So far the temporary solution, not probably recommended but still does the job programming practice, is hiding and unhiding certain elements on specific conditions.

Upon user interaction (e.g onClick), unhide and hide elements while updating values that resemble the controller update. Simply simulate a refresh and update the form paths/controller actions they point to.

This will mostly work better with boolean check-box forms, likes or dislikes where variables will only alternate between two instances at max. Hence you don't have to write much javascript to handle pseudo outputs.

But still, I'm sure there's a better method for example WebSockets through Action Cable. That can probably be overkill but the closest to realtime updates. respond_to just doesn't seem to work

axelmukwena
  • 779
  • 7
  • 24