Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 239
AndraD Avatar asked Sep 20 '12 02:09

AndraD


People also ask

What is the best way to get the current request URL in Rails?

What is the best way to get the current request URL in Rails? You should use request. original_url to get the current URL.

How do I get the current URL in Ruby?

You should use request. original_url to get the current URL. For Rails 3: You can write "#{request.

How can you get the current absolute URL in your Ruby on Rails view?

You can write request. url instead of request. request_uri . This combines the protocol (usually http://) with the host, and request_uri to give you the full address.

What is request referer in rails?

request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link)


2 Answers

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.

like image 69
nzifnab Avatar answered Oct 27 '22 00:10

nzifnab


feedback_index_path is a routes helper method that will always return the same thing. In your case /feedback.

Look here for info on accessing the current URL in both Rails 2 and 3.

like image 24
jdl Avatar answered Oct 26 '22 23:10

jdl