简体   繁体   English

Rails:如何在控制器中呈现其他动作,在js中不在html中?

[英]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. 问题在于我首先调用find动作,然后调用result动作但是采用html格式。 So the 'format.js` is never triggered. 所以'format.js`永远不会被触发。

How can render, at the end of the find action, the result action in the js format ? 如何在find操作结束时以js格式呈现result操作?

Thanks a lot ! 非常感谢 !

在find方法中尝试这个。

render 'result', :id => @item.id, :format => :js
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>

render方法只渲染视图,不调用另一个动作

The bigger issue here is that you are trying to call another action, which breaks MVC pattern. 这里更大的问题是你试图调用另一个动作,这打破了MVC模式。 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. 另外,请回复您正在调用/触发这些操作的视图中的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM