Refactoring link in Rails 3.2.12

Refactoring an ugly URL

I'm linking to a job for a new app-app, but the url generated for the app looks messy and I'm wondering if there is a way to clean it up.

Is there a way to navigate from this not-so-pretty URL ?:

http://localhost:3000/applications/new?application%5Bjob_id%5D=1

      

For this cleaning device ?:

http://localhost:3000/applications/new

      

Thanks in advance!


Code:

Workstation controller:

def show
  @job = Job.find(params[:id])
end

      

Ad:

<%= link_to "New Application", new_application_path(:application => { :job_id => @job.id }) %>

      

Application controller

def new
  @application = Application.new(params[:application])
end

      

New app

<%= 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 %>

      

EDIT

(added rake routes and .rb routes)

Route routes

    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

      

routes.rb

resources :applications

resources :jobs

      

+3


source to share


1 answer


If you don't want to use nested routes

(I recommend using nested routes). But there are many solutions to solve the problem. To avoid parameters in URL

(i.e.) http://localhost:3000/applications/new?application%5Bjob_id%5D=1

before http://localhost:3000/applications/new

. You need to use form

on the show page, not link

. You can do the following:



  <%= form_tag url_for(new_application_path) do %>
    <%= hidden_field_tag "application['job_id']", @job_id %> 
    <%= submit_tag "New application" %>
  <% end %>

      

+1


source







All Articles