My helper methods in controller

后端 未结 3 489
难免孤独
难免孤独 2021-01-02 08:57

My app should render html, to answer when a user clicks ajax-link.

My controller:

def create_user
  @user = User.new(params)
  if @user.save
    stat         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-02 09:16

    helpers are just ruby modules which you can include in any controller just like any module.

    module UserHelper
        def link_to_profile(user)
            link = link_to(user.login, {:controller => "users", :action => "profile", :id => user.login}, :class => "profile-link")
            return(image_tag("/images/users/profile.png") + " " + link)
        end
    end
    

    And, in your controller :

    class UserController < ApplicationController
        include UserHelper
    
        def create
            redirect_to link_to_profile(User.first)
        end
    end
    

提交回复
热议问题