How to get request's target controller and action with Rails 3?
class ApplicationController < ActionController::Base
before_filter :authenticate
def authenticate
# How do we know which controller and action was targetted?
params[:controller]
params[:action]
# OR
controller.controller_name
controller.action_name
end
end
AJAX request to Rails, does the target controller action have access to the same session as it would if it was visited in a browser?
It has access to everything.
Beware of one detail: ajax sometimes fails to pass the authorize before filter.
Ruby on Rails: Get the controller and action name based on a path
This is what I ended up doing. It is ugly and there must be a better way, but it works for now. It happens in a before_filter so that I can see if the user has access to the controller / action they are attempting to access.
I chose to use route-based authorization as opposed to model-based authorization.
# Get method of current request
method = options[:method] ? options[:method] : 'get'
# Create a new request - hate this that is required
env = Rack::MockRequest.env_for(url, {:method => method})
request = ActionController::Request.new(env)
# For some reason, calling this fills in the controller / action information for the request
# just using recognize_path doesn't work correctly with resources...
ActionController::Routing::Routes.recognize(request)
Then you access the controller and action with request.params[:controller] and request.params[:action].
All of this would not be necessary if ActionController::Routing::Routes.recognize_path("/permissions/1") returned the correct action.
Getting text field value to controller action using Rails 3
In homes/home/_good.html.erb:
<%= form_for :vendor,:url => {:action =>"payment" } do |f| %>
<div class="totalaligndiv">
<%= hidden_field_tag 'receipt_no', @sdf.Receipt_No %>
</div>
<% end %>
In controller/homes_controller.rb:
def payment
@vendor=AddToPaymentVendor.create([column_name]: params[:receipt_no)
end
where column_name represents the column where you want to save receipt no in AddToPaymentVendor model
Rails request with id
I don't think this is possible from chrome since chrome doesn't send the parameter after the hash in the request.
How do I get the current absolute URL in Ruby on Rails?
For Rails 3.2 or Rails 4+
You should use request.original_url
to get the current URL. Source code on current repo found here.
This method is documented at original_url method, but if you're curious, the implementation is:
def original_url
base_url + original_fullpath
end
For Rails 3:
You can write "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
, since request.url
is now deprecated.
For Rails 2:
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.
RubyAmf and Rails 3
The best suggestion is to try rails3-amf. It currently is severely lacking in features in comparison to RubyAMF, but it does work and I'm adding new features as soon as they are requested or I have time.
Is the name of the target method available in a before_filter
You could simply get the action from the params: params[:action]
Related Topics
Array Attribute for Ruby Model
What Alternatives to Irb Are There
How to Access a Ruby Module Method
Rails Console - Find Where Created at = Certain Day
How to Add Two Weeks to Time.Now
How to Read the Body Text of an Email Using Ruby's Net/Imap Library
Rails 3 Initializes Extremely Slow on Ruby 1.9.2
Ruby 'Each_With_Object' with Index
Rails How to Switch Between Dev and Production Mode
Xcode - Configure: Error: No Acceptable C Compiler Found in $Path
Profile a Rails Controller Action
Rails: How to Show User's "Last Seen At" Time
Unit Test in Rails - Model with Paperclip
Rails Activeadmin - Custom Association Select Box
How to Uninstall Ruby on Rails and Do a Clean Install
How to Use an Actionview::Helper in a Ruby Script, Outside of Rails