Maxxx
Maxxx

Reputation: 1179

Rails : how to render an other action in the controller, in js not in html?

I have two actions in my controller :

def find
  @item = Item.find(params[:id])
  render 'result', :id => @item.id
end

def result
  @item = Item.find(params[:id])
  respond_to do |format|
    format.js
  end
end

The issue in that I call first the find action, then it calls the result action but in the html format. So the 'format.js` is never triggered.

How can render, at the end of the find action, the result action in the js format ?

Thanks a lot !

Upvotes: 5

Views: 3915

Answers (4)

tharindu Muhandiram
tharindu Muhandiram

Reputation: 41

Try this

<%= render 'components/activity-slider', populer_packages: @populer_packages %>

    components/activity-slider

    <div class="slider sliderSecondary activitiesSlider">
    <% populer_packages.each do |package| %>
        <a href="#" class="sliderGrid ">
          <div class="sliderCont">
            <div class="sliderGradient"></div>
            <div class="sliderThumb">
              <div class="sliderImgWrap">
                <%= image_tag "#{package.image}", class: "img-fluid sliderImg" %>
                <div class="overlayCaptionCont">
                  <div class="overlayCaptionWrap">
                    <div class="overlayCaption">
                      <ul class="list-unstyled captionList">
                        <li>test</li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </a>
      <% end %>
    </div>

Upvotes: 1

kvirani
kvirani

Reputation: 76

The bigger issue here is that you are trying to call another action, which breaks MVC pattern. Even though actions are defined as methods, think of them as something else, that cannot call other actions.

Also, please reply with code from your views that are calling/trigger these actions.

Upvotes: 1

Dia Kharrat
Dia Kharrat

Reputation: 6016

Try this in your find method.

render 'result', :id => @item.id, :format => :js

Upvotes: 6

mikdiet
mikdiet

Reputation: 10038

render method only renders views, doesn't call another action

Upvotes: 1

Related Questions