ruby-on-rails - 如何在 if 和 else 语句中引用特定 Controller

标签 ruby-on-rails

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

if [controller.rb] == [controller name]
 do something
end

我知道与 Controller 中的特定操作相关,可以执行以下操作:

<% if action_name == 'new'  %>
          <span>create event</span>
        <% else %>
          <span>update event</span>
        <% end %>

但例如。在我的评论 Controller 中,操作名称“create”我想声明如下:

      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

我如何引用特定 Controller ?

最佳答案

controller_name == 'posts' 就是您要查找的内容。

关于ruby-on-rails - 如何在 if 和 else 语句中引用特定 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50658996/

相关文章:

javascript - Heroku bootstrap 导航栏不渲染

ruby-on-rails - Rails 路由逻辑

javascript - 在另一个模板 rails 内成型

python - 如何在 Rails 服务器上运行 python 脚本?

ruby-on-rails - 为什么 ctrl + z 和 ctrl + c 在 Rails 控制台上不起作用?

ruby-on-rails - Ruby on Rails link_to 内部 ID

ruby-on-rails - 如何将参数从我的新方法传递到 Rails 中的创建方法?

ruby-on-rails - 更正 HAML 以将图像链接到我的 Ruby on Rails 应用程序中的另一个页面

ruby-on-rails - 如何在 Heroku 中进行并行 HTTP 请求?

ruby-on-rails - Rails 如何在 textarea 上禁用 tinymce?