Cat Named Dog
Cat Named Dog

Reputation: 1698

Rails: render not updating page, but it's being returning in response

When calling render The view is not being updated, however, I do notice in the network tab, after the POST request is made to me POST route, it's returning HTML as the response, and the response has my rendered error message. It's just not updating the page. I don't know what to make of that.

In my POST action, I'm forcing this to be called

    flash.now[:notice] = responseMessage
    render :deactivate_show

Which renders the action:

  def deactivate_show
    @user = User.find(params[:id])
    authorize! :deactivate_user, @user

    if current_user.role == "admin"

      if @user.broker?

        @companyUsers = User.where(parent_id: @user.id)
        @properties = Property.where(user_id: @user.id)
        .or(Property.where(user_id: @companyUsers.ids))

        elsif @user.broker_manager?

          # get all agents in company
          # exclude current user
          # get broker
          @companyUsers = User.where(parent_id: @user.parent_id)
          .where.not(id: @user.id)
          .or(User.where(id: @user.parent_id))

          @properties = Property.where(user_id: @companyUsers.ids)
          .or(Property.where(user_id: @user.parent_id))

        else @user.agent?

          @properties = @user.properties

          if @user.parent_id?
          @companyUsers = User.where(parent_id: @user.parent_id)
          .where.not(id: @user.id)
          .or(User.where(id: @user.parent_id) )
        else
          # dealing with owner.
          @companyUsers = User.where('role IN (2,4,1)')
          .where.not(id: @user.id)
        end

      end


    else

    end


  end

which renders a view I have that displays the form.

I added to my show view:

 <div class="messages">
<% flash.each do |key, value| %>

  <div class="hello"><%= value %></div>
<% end %>
</div>  

Note the class of "hello".

When I make a POST request, the response in the network tab has the notice:

Rendered from POST request response:

<div class="messages">

  <div class="hello">testing errors</div>
</div>

Edit: here's the form:

    <div class="form-wrap">
        <%= form_with do %>
            <%= button_tag( id: 'button--submit', class: 'button button--secondary') do %>
                <i class="icon-arrow-right"></i>
                <span>Deactivate</span>
            <% end %>
        <% end %>
    </div>

Upvotes: 1

Views: 1369

Answers (2)

Cat Named Dog
Cat Named Dog

Reputation: 1698

Well the actual answer was something no one could have provided since I didn't share my form. I was using form_with, which apparently does AJAX by default, thus not rendering the view.

Thanks for the help from this question: Rails render not showing in browser, despite positive server reply

I've changed

<%= form_with do %>

to

<%= form_with local: true do %>

and now it works.

Upvotes: 4

Dijkie85
Dijkie85

Reputation: 1106

The Flash is only rendered upon a new request, which you don't make with render. Instead, you should use flash.now[:notice] to have it display on render.

Upvotes: 2

Related Questions