我从作业链接到新的作业应用程序,但为应用程序生成的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 %>
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
resources :applications
resources :jobs
答案 0 :(得分:1)
如果您不想使用nested routes
(我建议使用嵌套路由)。但解决问题有很多解决方案。避免URL
(即)http://localhost:3000/applications/new?application%5Bjob_id%5D=1
到http://localhost:3000/applications/new
中的参数。您需要在展示页面中使用form
而不是link
。您可以执行以下操作:
<%= form_tag url_for(new_application_path) do %>
<%= hidden_field_tag "application['job_id']", @job_id %>
<%= submit_tag "New application" %>
<% end %>