如何在 if 和 else 语句中引用特定控制器

ART*_*Loe 2 ruby-on-rails

如何识别特定控制器以在 if 和 else 语句中执行特定操作?

if [controller.rb] == [controller name]
 do something
end
Run Code Online (Sandbox Code Playgroud)

我知道与控制器中的特定操作有关,可以这样做:

<% if action_name == 'new'  %>
          <span>create event</span>
        <% else %>
          <span>update event</span>
        <% end %>
Run Code Online (Sandbox Code Playgroud)

但例如。在我的操作名称“创建”的评论控制器中,我想声明如下内容:

      def create
        @comment = @commentable.comments.new(comment_params)
        @comment.user = current_user

        if [controller name]
         @discussion = Discussion.friendly.find(params[:discussion_id])
        elsif [controller name]
         @pratical = Pratical.friendly.find(params[:pratical_id])
        end

        respond_to do |format|
          if @comment.save
            format.html { redirect_to [@commentable], notice: 'Comment was successfully posted.' }
            format.json { render :show, status: :created, location: @comment }
          else
            format.html { redirect_to :back, alert: 'Comment was not posted.' }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
          end
        end
      end
Run Code Online (Sandbox Code Playgroud)

我如何引用特定的控制器?

Tho*_*oll 5

controller_name == 'posts' 就是你要找的。