How to Get Request's Target Controller and Action with Rails 3

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



Leave a reply



Submit