How to get data from a URL inside a rails controller? - ruby-on-rails

I have a controller method say
def my_method
#gibbon = <data needed>
render :something
end
and i have a URL say
http://exampleforme.com/example1
which returns a certain text, how to get this text inside my controller method from the URL?
I am using rails 2.8

You could use Net::HTTP to get that URL as a string:
#gibbon = Net::HTTP.get(URI.parse("http://exampleforme.com/example1"))
See the API docs for more information.
Note: that this could impact your response time if that URL is slow to respond.

can you show me your route file ?
but I think if you want "example1" text in url you can call params[:id] in your controller
#text = params[:id]

Related

Access old get parameters in URL after a post request

I have a form in RoR with a controller action that looks up a record via the get parameter.
def respond
if request.post?
# Submit logic here...
# cannot lookup this way to fill the form out again
# #current_message = Saved_message.find_by_id(params[:msg_id])
elsif request.get?
#current_message = Saved_message.find_by_id(params[:msg_id])
end
end
I can't use the params[:msg_id] to lookup the message again because it's a post request and I don't resend the get parameters. However, the get parameters remain in the url such as .../messages/respond?msg_id=2. I can get around this by passing in a hidden field with a different parameter name like <%= form.hidden_field :msg_id_2, value: params[:msg_id] %>. Then I can lookup the #current_message via the params[:msg_id_2]. However, I don't like this solution. Any advice to access the now inaccessible get parameter?
you should use RESTful routes so that you do not have to care about such issues.
since you are not posting much about the actual code or problem you are trying to solve, i can just assume what might be the issue here and how to solve it.

Prevent redirect when calling url_for in a rails app

I'm trying to pass a url string to a view from one of my controller methods, like so:
def index
#locals = {table_cols: $config['global_fields'],
table_title: $config['main_page']['table_title'],
ajax: url_for(action: index_data)}} # HERE
end
index_data is a function in my controller that returns some data in JSON format. However, when I navigate to the index page of my application, the browser simply displays the JSON output of index_data.
I'm assuming calling url_for within index is redirecting that page to index_data - is there a way to prevent this and just have url_for return the string, e.g. '/controller/index_data'?
Thanks
It's probably because you are calling the index_data action when you attempt to get a url to it, and calling it is tricking Rails into thinking you want to render from that action.
If you change your url_for to point to a symbol (or string) instead of the return value of the action, things will probably start working as you expect:
#locals = { ..., ajax: url_for(action: :index_data)}}

How to render a different controller / action from within an action in Rails 4.2?

I know this is possible but I want to render a controller / action in response to a different action like in my routes.rb, I have:
get ':handle' => 'handler#index'
and in handler_controller, I have:
def index
handle=Handle.where('handle=?',params[:handle]).first
if handle.handle_type=='Location'
# call location / show with id from handle
else
render text: "not found"
end
end
How would I do this? I should mention, I don't want to do a redirect.
Maybe this can help:
def action_that_calls_one_from_another_controller
controller_you_want = ControllerYouWant.new
controller_you_want.request = request
controller_you_want.response = response
controller_you_want.action_you_want
end
You can solve this by using an "internal redirect" approach.
Inside rails this can be done via middleware that calls #app again and tracks redirect loops. See tutorial here
Also can be done by utilizing nginx's X-Accel-Redirect: make a regular routes for referenced resources, but instead of regular redirect issue a X-Accel-Redirect header, for user it will be like your page rendered without redirect.

How to append Query parameter in the URL from the controller method in Grails?

In my jobsController, I have a method named getEmployee(). This method renders view named employee.gsp.
render(view : "employee")
When my view is displayed, the url is generated as given below.
http://localhost:8080/test/jobs/getEmployee
Now in this URL I want to append a parameter pagination=false. So my new url should look like:
http://localhost:8080/test/jobs/getEmployee?pagination=false.
How can I do this? Is there any way to append parameters in the generated URL from the controller method getEmployee?
there is a trick to do so, by redirecting to getEmployee action with params.pagination = false
redirect action:'getEmployee', params:[pagination:false]
for example
assume I want 'x' parameter in my /cont/checkGet?x=false like this, so I will redirect from 'check' action to 'checkGet' action with x params
def check(){
redirect action:"checkGet",params: [x:false]
}
def checkGet(){
render "Anshul"
}
hope this helps, Thanks

Get output of different controller action in rails3

For generating PDF from HTML, i need to fill a variable with output from another controller action output (HTML). Is there any elegant way, how to get this HTML?
Thanks
You can use:
def print
output = render_to_string(:action => :index)
end
in your controller.
You may abstract the code in that action into a shared method which would be called within your pdf-generating action.
After calling the shared method, you would get the html page content like this:
pdf_content = ERB.new(File.read("/path/to/that/erb.file")).result

Resources