【问题标题】:How to call method of the controller to the view in rails?如何将控制器的方法调用到rails中的视图?
【发布时间】:2014-11-30 14:44:01
【问题描述】:

在控制器中

def test
  command = 'pwd'
  result = `#{command}`

   respond_to do |format| 
     format.html { redirect_to(users_url) } 
     format.js { render :text => "$('#teste').html('#{result}');" } 
   end 
end

我要显示变量结果

在视图中

<%= link_to 'Testar', test_users_path, :method => :get, :remote => true  %> 

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    声明一个 helper_method。

    在您的控制器中:

    private
    def test
      ...
    end
    helper_method :test
    
    In your view:
    
    <% test -%>
    

    【讨论】:

    • 我只想在点击按钮后显示返回。
    猜你喜欢
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多