重构丑陋的 URL
我从一个工作链接到一个新的工作应用程序,但是为应用程序生成的 URL 感觉有点乱,我想知道是否有办法清理它。
有没有办法摆脱这个不那么漂亮的网址?:
http://localhost:3000/applications/new?application%5Bjob_id%5D=1
对于这个更清洁的人?:
http://localhost:3000/applications/new
提前致谢!
编码:
作业控制器:
def show
@job = Job.find(params[:id])
end
工作展示:
<%= link_to "New Application", new_application_path(:application => { :job_id => @job.id }) %>
应用控制器
def new
@application = Application.new(params[:application])
end
新申请
<%= form_for @application do |f| %>
<%= f.hidden_field :job_id, :value => @application.job_id %>
<%= f.label :email %>:
<%= f.text_field :email %>
<br />
<%= f.submit "Submit" %>
<% end %>
编辑
(添加了 rake 路由和 routes.rb)耙路线
applications GET /applications(.:format) applications#index
POST /applications(.:format) applications#create
new_application GET /applications/new(.:format) applications#new
edit_application GET /applications/:id/edit(.:format) applications#edit
application GET /applications/:id(.:format) applications#show
PUT /applications/:id(.:format) applications#update
DELETE /applications/:id(.:format) applications#destroy
jobs GET /jobs(.:format) jobs#index
POST /jobs(.:format) jobs#create
new_job GET /jobs/new(.:format) jobs#new
edit_job GET /jobs/:id/edit(.:format) jobs#edit
job GET /jobs/:id(.:format) jobs#show
PUT /jobs/:id(.:format) jobs#update
DELETE /jobs/:id(.:format) jobs#destroy
路由.rb
resources :applications
resources :jobs