如何识别特定 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/