没有视图的Rails控制器操作

时间:2022-06-25 20:36:57

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,PictureController中的这个动作不需要视图?

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 match call. I cannot tell for sure, but i think it was a problem with the resources :picture in my routes.rb file...

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

But anyway, thank you guys! =)

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

3 个解决方案

#1


23  

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

#2


5  

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 %>

#3


0  

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响应。

#1


23  

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

#2


5  

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 %>

#3


0  

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响应。