由于没有ID匹配,路由到模型内容失败

时间:2013-03-19 20:44:49

标签: ruby-on-rails ruby-on-rails-3 url url-rewriting

我对Rails相对较新,并且最近设法打破了我的某个模型内容的链接......

之前在stackoverflow上发布了一个问题,我调整了模型中的to_param函数,以便将产品名称附加到产品ID。

我所做的改变是:

products.rb

def to_param
  "#{id}-#{product_name.parameterize}"  
end

routes.rb

match '/:id' => 'uniquewetsuits#show'

这成功创建了我希望/products/ID-product-name的地址,但是,我收到一条错误消息,指出没有ID=ID-product-name的产品。

如果我导航到/products/ID,我可以正常查看该页面。

有人可以告诉我如何重新连接东西,以便我找到更长的ID字符串匹配吗?

感谢您的时间

修改

控制器的内容是:

 def index

#@search = Uniquewetsuit.search(params[:q])
#@findwetsuits = @search.result(:distinct => true)

#if @findwetsuits.count > 0
 # @uniquewetsuits = @findwetsuits.all
#else
  @uniquewetsuits = Uniquewetsuit.all
#end

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @uniquewetsuits }
end
end

# GET /uniquewetsuits/1
# GET /uniquewetsuits/1.xml
def show
@uniquewetsuit = Uniquewetsuit.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @uniquewetsuit }
end
end

# GET /uniquewetsuits/new
# GET /uniquewetsuits/new.xml
def new
@uniquewetsuit = Uniquewetsuit.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @uniquewetsuit }
end
end

# GET /uniquewetsuits/1/edit
def edit
@uniquewetsuit = Uniquewetsuit.find(params[:id])
end

# POST /uniquewetsuits
# POST /uniquewetsuits.xml
def create
@uniquewetsuit = Uniquewetsuit.new(params[:uniquewetsuit])

respond_to do |format|
  if @uniquewetsuit.save
    format.html { redirect_to(@uniquewetsuit, :notice => 'Uniquewetsuit was successfully created.') }
    format.xml  { render :xml => @uniquewetsuit, :status => :created, :location => @uniquewetsuit }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @uniquewetsuit.errors, :status => :unprocessable_entity }
  end
end
end

# PUT /uniquewetsuits/1
# PUT /uniquewetsuits/1.xml
def update
@uniquewetsuit = Uniquewetsuit.find(params[:id])

respond_to do |format|
  if @uniquewetsuit.update_attributes(params[:uniquewetsuit])
    format.html { redirect_to(@uniquewetsuit, :notice => 'Uniquewetsuit was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @uniquewetsuit.errors, :status => :unprocessable_entity }
  end
end
end

# DELETE /uniquewetsuits/1
# DELETE /uniquewetsuits/1.xml
def destroy
@uniquewetsuit = Uniquewetsuit.find(params[:id])
@uniquewetsuit.destroy

respond_to do |format|
  format.html { redirect_to(uniquewetsuits_url) }
  format.xml  { head :ok }
end
end

0 个答案:

没有答案