异常错误路由到 http://localhost:3000/statics/edit.1
Unusual error routing to http://localhost:3000/statics/edit.1
伙计们,我是 rails 的新手。我在尝试编辑产品时遇到上述错误。
Index.html.erb
在首页列出产品
<% @products.each do |product| %>
<li><%=link_to product.name, statics_show_path(prod_id: product.id)%>
</li>
<li><%=link_to "Edit", statics_edit_path(product)%></li>
<% end %>
<%= link_to "New Product", statics_new_path %>
静态控制器
class StaticsController < ApplicationController
def index
@products = Product.all
end
def new
@product = Product.new
end
def show
product_value = Product.find(params[:prod_id])
@product_attribute = ProductAttribute.where(value: product_value.value)
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to root_url
else
render 'new'
end
end
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
render 'root_url'
else
render 'edit'
end
end
private
def product_params
params.require(:product).permit(:name,:value)
end
end
请帮帮我。非常欢迎任何帮助。
Routes.rb
Rails.application.routes.draw do
root 'statics#index'
get 'statics/new'
post 'statics/create'
get 'statics/show'
get 'statics/edit'
put 'statics/update'
resources :products
resources :product_attributes
end
您的路线不包含 id
路段。在 Rails 风格的 REST 中,show、edit、update 和 destroy 路由是成员路由,并且必须包含一个 id,它说明应该显示/更改哪条记录。
一个直接的解决方法是添加一个 id 段。
# still smells
get 'statics/new'
post 'statics/create'
get 'statics/:id/show'
get 'statics/:id/edit'
put 'statics/:id/update'
但您应该遵循 the Rails conventions 并使用 HTTP 方法,而不是将 /create
、/update
添加到路径中。
# Don't really do this - use resources instead
# its just for the sake of the example
get 'statics', to: 'statics#index'
get 'statics/new'
post 'statics', to: 'statics#create'
get 'statics/:id', to: 'statics#show'
get 'statics/:id/edit', to: 'statics#edit'
put 'statics/:id', to: 'statics#update'
更好的是使用可以为您生成 CRUD 路由的 resources macro:
resources :statics
尝试:
get 'statics/:id/edit', to: 'statics#edit'
或者您可以使用此方法创建 CRUD 路由
resources :statics
有关更多信息,请使用 RubyDoc
伙计们,我是 rails 的新手。我在尝试编辑产品时遇到上述错误。
Index.html.erb
在首页列出产品
<% @products.each do |product| %>
<li><%=link_to product.name, statics_show_path(prod_id: product.id)%>
</li>
<li><%=link_to "Edit", statics_edit_path(product)%></li>
<% end %>
<%= link_to "New Product", statics_new_path %>
静态控制器
class StaticsController < ApplicationController
def index
@products = Product.all
end
def new
@product = Product.new
end
def show
product_value = Product.find(params[:prod_id])
@product_attribute = ProductAttribute.where(value: product_value.value)
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to root_url
else
render 'new'
end
end
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
render 'root_url'
else
render 'edit'
end
end
private
def product_params
params.require(:product).permit(:name,:value)
end
end
请帮帮我。非常欢迎任何帮助。
Routes.rb
Rails.application.routes.draw do
root 'statics#index'
get 'statics/new'
post 'statics/create'
get 'statics/show'
get 'statics/edit'
put 'statics/update'
resources :products
resources :product_attributes
end
您的路线不包含 id
路段。在 Rails 风格的 REST 中,show、edit、update 和 destroy 路由是成员路由,并且必须包含一个 id,它说明应该显示/更改哪条记录。
一个直接的解决方法是添加一个 id 段。
# still smells
get 'statics/new'
post 'statics/create'
get 'statics/:id/show'
get 'statics/:id/edit'
put 'statics/:id/update'
但您应该遵循 the Rails conventions 并使用 HTTP 方法,而不是将 /create
、/update
添加到路径中。
# Don't really do this - use resources instead
# its just for the sake of the example
get 'statics', to: 'statics#index'
get 'statics/new'
post 'statics', to: 'statics#create'
get 'statics/:id', to: 'statics#show'
get 'statics/:id/edit', to: 'statics#edit'
put 'statics/:id', to: 'statics#update'
更好的是使用可以为您生成 CRUD 路由的 resources macro:
resources :statics
尝试:
get 'statics/:id/edit', to: 'statics#edit'
或者您可以使用此方法创建 CRUD 路由
resources :statics
有关更多信息,请使用 RubyDoc