Search code examples
ruby-on-railsruby-on-rails-3formsfeedbackuser-feedback

Rails feedback form: How to get the url for the current page?


I am working with David Francisco's rails feedback plugin. The plugin works fine, except for one thing - I would need the feedback form to submit to the database the url for the page where the feedback form was used. Does anyone know how to do this?

The view that would need to send the current url, in addition to the currently sent information:

<h4>Feedback</h4>
<p>Please leave us feedback, it's really appreciated.</p>

<%= form_for @feedback, :as => :feedback, :url => feedback_index_path, :html => { :id => "feedback_form" } do |f| -%>
  <%= f.hidden_field 'user_id', :value => current_user.id %>
  <% unless @error_message.blank? %>
  <p class="error">
    <%=h @error_message %>
  </p>
  <% end %>
  <p>
    <%= f.label 'subject' %>
    <%= f.select 'subject', ['Problem', 'Suggestion', 'Question', 'Other'] %>
  </p>
  <p>
    <%= f.label 'comment' %><br />
    <%= f.text_area 'comment', :rows => 10, :cols => 30 %>
  </p>
  <p><%= f.submit 'Send' %></p>
<% end -%>

Currently, feedback_index_path is always '/feedback', the url for the form.

Thank you, Alexandra


Solution

  • You can use request.referer in your controller to get the path of the page that called your action. request.referer returns a full url but you can parse it with the URI module:

    URI(request.referer).path
    

    I see some people have suggested request.fullpath but this is actually the path of the action that's processing the request (in your case /feedbacks/new) and not the path where the form was submitted from.