Ruby-on-rails 没有视图的 Rails 控制器动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12919631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:53:00  来源:igfitidea点击:

Rails controller action without a view

ruby-on-railsrails-routing

提问by Crystian Le?o

I just want to do a rails action without a view.

我只想在没有视图的情况下执行 rails 操作。

In my 'routes.rb'

在我的“routes.rb”中

resources :pictures do
    member do 
        post 'dislike'  
    end  
end

In my 'PictureController.rb'
this does not work

在我的“PictureController.rb”中,
这不起作用

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    respond_to do |format|
        format.html { render :action => :show, :id => params[:id], notice: 'You don\'t  like this picture anymore.' }
        format.json { render json: @picture }
    end
end

neither do this

也不这样做

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    respond_to do |format|
        format.html { redirect_to @picture, notice: 'You don\'t  like this picture anymore.' }
        format.json { render json: @picture }
    end
end

or even this (but this is not the case for me, i want a feedback to the user via json and via html)

甚至这个(但对我来说不是这种情况,我想通过 json 和 html 向用户提供反馈)

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    render :nothing => true
end

But i keep getting this error message:

但我不断收到此错误消息:

ActionView::MissingTemplate: Missing template pictures/dislike, application/like with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.

How should i tell rails that this action in PicturesController does not needs a view?

我应该如何告诉 rails PicturesController 中的这个操作不需要视图?

Solved!

解决了!

I didn't really solved the problem of telling rails i did not need a view, i just created another controller, put the method in it, and told rails routing to match the dislike action with a matchcall. I cannot tell for sure, but i think it was a problem with the resources :picturein my routes.rbfile...

我并没有真正解决告诉 rails 我不需要视图的问题,我只是创建了另一个控制器,将方法放入其中,并告诉 rails 路由将不喜欢的操作与match调用相匹配。我不能肯定,但我认为这是resources :picture我的routes.rb文件中的问题...

But anyway, thank you guys! =)

但无论如何,谢谢你们!=)

采纳答案by Crystian Le?o

Just created another controller with the dislike action:

刚刚创建了另一个带有不喜欢动作的控制器:

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    respond_to do |format|
        format.html { redirect_to :back, notice: 'You don\'t  like this picture anymore.' }
        format.json { render json: @picture }
    end
end

and modified my routes.rb to match this action:

并修改了我的 routes.rb 以匹配此操作:

match 'pictures/:id/dislike' => "likes#dislike", :via => :post

and my link to dislike now is

我现在不喜欢的链接是

<%= link_to 'Dislike!', {:action => :dislike, :controller => :likes}, :method => :post %>

回答by iouri

Something like this?

像这样的东西?

def dislike
    @picture = Picture.find(params[:id]
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    render :nothing => true
end

回答by gregates

Most likely, the problem is that you're hitting this action via an ajax request. So the controller is looking for format.js, but you haven't specified a response for that format in your block. Thus it's falling through to the default.

最有可能的问题是您通过 ajax 请求执行此操作。因此控制器正在寻找format.js,但您尚未在块中指定该格式的响应。因此它陷入了默认状态。

Try

尝试

format.js { render json: @picture }

You may also need to tell the ajax request to expect a json response.

您可能还需要告诉 ajax 请求期待 json 响应。