0
我正在构建一个培训网站,我正尝试根据内容标题(数据库中的一列)生成内容的动态路线。我有很多,但我在最后一部分遇到问题。生成具有特定模型属性的导轨路线
假设内容标题是“商务旅行”,即关于商务旅行的培训课程。如下所示的代码,在模板中的link_to helper方法产生这样的链接:
http://localhost:3000/courses/Business Travel
我需要的是对的话用虚线隔开,像堆栈溢出问题的URL的格式设置。
是否有内置的方法呢?或者我必须编写一个帮助函数,将内容对象的title属性操作为URL友好的?或者...我是否完全错误地做这件事?
contentcontroller.rb
class ContentController < ApplicationController
before_action :signed_in_user
def tracks
end
def courses
@courses = Content.all
end
def show
@course = Content.find_by_title(params[:title])
end
end
content.rb
class Content < ActiveRecord::Base
validates :title, presence: true
validates :description, presence: true
end
courses.html.erb
<% provide(:title, 'Tracks') %>
<div class="container">
<% @courses.each do |course| %>
<div class="row">
<%= link_to course_path(course.title) do %>
<div class="col-md-12 course-box content-link centertext">
<strong><%= course.title %></strong>
<p><%= course.description %></p>
</div>
<% end %>
</div>
<% end %>
</div>
的routes.rb
match '/courses/:title', to: 'content#show', via: 'get', as: 'course'