开发者

How to create a custom POST Action in Rails3

I am trying to create a custom POST action for my article object.

In my routes.rb, I have set the action in the following way:

resources :article开发者_开发知识库s do
  member do
    post 'update_assigned_video'
  end
end

In my articles_controller.rb I have:

def update_assigned_video
  @article = Articles.find(params[:id])
  @video = Video.find(:id => params[:chosenVideo])
  respond_to do |format|
  if !@video.nil?
    @article.video = @video
    format.html { redirect_to(@article, :notice => t('article.updated')) }
  else
    format.html { render :action => "assign_video" }
  end
end

Then in my view I make a form like this:

<%= form_for @article, :url => update_assigned_video_article_path(@article) do |f|%>
  [...]
  <%= f.submit t('general.save') %>

The view renders (so I think he knows the route). But clicking on the submit button brings the following error message:

No route matches "/articles/28/update_assigned_video"

rake routes knows it also:

update_assigned_video_article POST   /articles/:id/update_assigned_video(.:format)    {:action=>"update_assigned_video", :controller=>"articles"}

What am I doing wrong? Is this the wrong approach to do this?


Your form_for will do a PUT request rather than a POST request, because it's acting on an existing object. I would recommend changing the line in your routes file from this:

post 'update_assigned_video'

To this:

put 'update_assigned_video'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜