友好的Id定制S ..

时间:2016-04-06 16:29:03

标签: ruby-on-rails ruby friendly-id

我有一个Post模型

#post.rb   
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :history]

我正在尝试为每个帖子自定义网址

#post.rb 
def slug_candidates
 "#tutorial-#{user.display_name}-#{title}"
end

友好的ID一直在生成UUID slug,而我希望它能够生成一个全面的URL

通常我会得到\ tutorial-john-49c9938b-ece5-4175-a4a4-0bb2b0f26a27 而不是\ tutorial-john-some-comprehensive-title

谢谢

2 个答案:

答案 0 :(得分:2)

我编辑了回复,对不起。 似乎friendly_id期待一系列的候选人。正如您所见here

请试试这个:

#post.rb 
def slug_candidates
  [ "#tutorial-#{user.display_name}-#{title}" ]
end

答案 1 :(得分:1)

解读。在我保存标题之前生成了slug。因此它以零标题生成。我不得不重写should_generate_new_friendly_id方法

    if($activity == "upload"){

     $image_name = $_FILES['image']['name'];
     $image_type = $_FILES['image']['type'];
     $image_size = $_FILES['image']['size'];
     $image_tmp_name = $_FILES['image']['tmp_name'];

    if($image_name==''){
        echo "<script>alert('Please Select an Image')</script>";
        exit();
    }
    else{
        move_uploaded_file($image_tmp_name,"photos/$image_name");
        echo "Image uploaded successfully";
        echo "<img src='photos/$image_name'>";


    }
}