Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何去掉下划线_&引用;从rails生成的url?_Ruby On Rails_Actioncontroller - Fatal编程技术网

Ruby on rails 如何去掉下划线_&引用;从rails生成的url?

Ruby on rails 如何去掉下划线_&引用;从rails生成的url?,ruby-on-rails,actioncontroller,Ruby On Rails,Actioncontroller,我刚刚在rails应用程序的类别模型上实现了友好的\u id。之前,它用于生成如下url: localhost/search?category\u id=208。现在我得到它来生成url,如下所示 localhost:3000/search?category_id=metal-processing-and-machine-tool url生成行为: <a href="<%= search_equipments_path(:category_id => category.slu

我刚刚在rails应用程序的类别模型上实现了友好的\u id。之前,它用于生成如下url:

localhost/search?category\u id=208
。现在我得到它来生成url,如下所示

localhost:3000/search?category_id=metal-processing-and-machine-tool
url生成行为:

<a href="<%= search_equipments_path(:category_id => category.slug ) %>">
由于SEO的原因,我上面的人告诉我要从url中去掉
\uu
类别中的
id
。我尝试在url生成行中更改格式。没有帮助。谁能告诉我这是否可行,我怎样才能做到


如果需要任何额外的信息,请在评论中告诉我。

我很确定您可以将
更改为
,这将设置
参数[:categoryid]
。然后,您需要将所有检查修改为
if params[:categoryid]。present?
等。然后,当您单击链接时,它应该转到
localhost:3000/search?categoryid=
我考虑过它,并且已经尝试过了,但没有成功。但既然你也提到了同样的事情,让我再试一次。在控制台上它变了<代码>参数:{“categoryid”=>“金属加工和机床”}。但没有按预期生成url。当我单击链接时,它仍在同一页上。您的路线是什么样的?这应该通过更改为categoryid来实现,除非您正在对路线执行某些操作。
def search_equipments
    begin
      if (params.keys & ['category_id', 'sub_category', 'manufacturer', 'country', 'state', 'keyword']).present?
        if params[:category_id].present?
          @category = Category.active.find params[:category_id]
        else
          @category = Category.active.find params[:sub_category] if params[:sub_category].present?
        end
        @root_categories = Category.active.roots
        @sub_categories = @category.children.active if params[:category_id].present?
        @sub_categories ||= {}
        @countries = Country.active.all
        @manufacturers = Manufacturer.active.all
        @states = State.active.where("country_id = ?", params[:country]) if params[:country].present?
        @states ||= {} 
        unless params[:category_id].present? && params[:sub_category].present?
          params[:category_id] = @category.id if params[:category_id].present?
          params[:sub_category] = @category.id if params[:sub_category].present?
        end   
        @equipments = Equipment.active.filter(params.slice(:manufacturer, :country, :state, :category_id, :sub_category, :keyword)).order("#{sort_column} #{sort_direction}, created_at desc")             
      else
        redirect_to root_path
      end
    rescue Exception => e
      redirect_to root_path, :notice => "Something went wrong!"
    end
 end