ActionController::MethodNotAllowed(仅允许 get 和 post 请求。):

发布于 2024-09-12 08:14:15 字数 814 浏览 2 评论 0原文

不知道发生了什么事。我使用了以下代码来尝试编辑类别的名称,但收到上面的错误消息。我的表单和提交编辑代码是: -

<% form_for :category, :url => categories_url(@category),:html => { :method => :put } do |f| -%>
<p>Name: <br /><%= f.text_field :name, :size => 60 %></p>
<%= submit_tag 'Save' %> or <%= link_to 'cancel', admin_categories_url%>

非常简单的东西。我的控制器代码是:- 默认编辑 @category = Category.find(params[:id]) 结束

# PUT /类别/1 # PUT /categories/1.xml 默认更新 @category = Category.find(params[:id]) @category.update_attributes(params[:category])

respond_to do |wants|
  wants.html { redirect_to admin_categories_url }
  wants.xml  { render :xml => @category.to_xm }
end  

end

这段代码适用于其他事情 - 例如博客文章,所以我不确定我哪里出错了。帮助??

Not sure what's going on. I've used the following bit of code to try and edit the name of a category, but I'm getting the error message above. My code for the form and submit for the edit is: -

<% form_for :category, :url => categories_url(@category),:html => { :method => :put } do |f| -%>
<p>Name: <br /><%= f.text_field :name, :size => 60 %></p>
<%= submit_tag 'Save' %> or <%= link_to 'cancel', admin_categories_url%>

So pretty straight forward stuff. My controller code is: -
def edit
@category = Category.find(params[:id])
end

# PUT /categories/1
# PUT /categories/1.xml
def update
@category = Category.find(params[:id])
@category.update_attributes(params[:category])

respond_to do |wants|
  wants.html { redirect_to admin_categories_url }
  wants.xml  { render :xml => @category.to_xm }
end  

end

This code has worked for other things - such as blog articles, so I'm not sure where I{"m going wrong. Help??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2024-09-19 08:14:15

我想你想要 :url => Category_url(@category)(非复数)。

I think you want :url => category_url(@category) (non-plural).

浮生面具三千个 2024-09-19 08:14:15

这往往会更干净一些...让路由系统找出如何最好地保存@category。

/app/controllers/admin_categories_controller.rb(猜测)

def new
  @category = Category.new
end

/app/views/admin_categories/new.html.erb

<% form_for @category do |f| %>
<p>
<%= f.label :name%>: <%= f.text_field :name, :size=>60%>
</p>
<%= f.submit :save%> or <%= link_to 'cancel', admin_categories_url%>

This tends to be a bit cleaner... Let the Routing system figure out how best to save the @category.

/app/controllers/admin_categories_controller.rb (guessed at this)

def new
  @category = Category.new
end

/app/views/admin_categories/new.html.erb

<% form_for @category do |f| %>
<p>
<%= f.label :name%>: <%= f.text_field :name, :size=>60%>
</p>
<%= f.submit :save%> or <%= link_to 'cancel', admin_categories_url%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文