Hi im working on a Rails app, i didnt write the whole app, there is a part where i need to Echo a meesage if the user is Elite.. i know that in the URL i have the info
sign_up?locale=en&t=elite
Is there any chance i can use that in view to do something like you would do a GET condition pn PHP?
i mean if (t=elite) {echo this} else {}
Hope anyone can give me an easy solution for this, that not requiere wirte a hole method just for echoing 1 little message just for that kind of users.
thanks, also just as note im a noob on rails, but im doing just HTML/CSS integration
Query parameters are available in the params Hash on each request:
<% if "elite" == params[:t] %>Check it out<% end %>
Related
I'm a newb hobbyist developer. Can I just throw this repo of their ACRCloud's ruby example code into a controller? I'd like to use an audio fingerprinting song recognition database as a name validation for songs users are uploading using paperclip. Not sure if it's possible, just starting to research it, any hints or suggestions would be appreciated.
Obviously I'd have to replace
file_name = ARGV[0]
etc, but I'm also wondering about the require 'openssl' etc
Definitely! But there are few points to be taken care of. That's a pure ruby script, when it comes to rails there are certain rules/best practices. One of which is thin controller and fat model..
You need to create a route/action in your app which will ask the app to execute this request with required params.
Write a method in your model which contains the code and call it from controller and pass the permitted params to it.
Instead of hardcoding your credentials in the model, make them environment variables.
Would suggest using Httparty gem wgich will reduce many lines of your code and you just need to pass headers, params, etc. as hash in the arguments.
Last, but not the least...if you notice..there's a puts in the end however, rails uses mvc and so you need to have a view for the controller action you created in step1. Return and save the response.body in the class variable like #response = res.body and you can play with the body depending on the response type.
Hope it helps..
P.S. I wish I could write few lines of code/optimise it for you but i m using my mobile right now. But I think this much information should be enough to convert that script to mvc rails structure..
I'm a Rails noob, but I'd like to use it as a backend for an Ember application with Ember Data. Unfortunately, I have some unknown unknowns.
The RESTAdapter documentation says:
Comments for a post can be loaded by post.get('comments'). The REST
adapter will send a GET request to /comments?ids[]=1&ids[]=2&ids[]=3.
It will generate similar urls if you use something like App.Post.find({title: "Some Title"}), in about the format you'd expect: /posts?title=Some+Title
Is there some option, or gem I can use to handle that sort of simple query, or do I have to go parse parameters in my controllers manually?
To clarify, I'm aware that I can tell my Rails controller to return a set like:
#comments = Comment.find(params[:ids])
respond_with(#comments)
But it seems like querying on ids or accessible attributes like that would be a common enough use case for REST APIs that something would be built in, or have a gem written to handle it.
Can anyone point me in the right direction?
Thanks.
This might be helpful in your case:
https://github.com/ernie/ransack/
Or
https://github.com/ernie/squeel
I'm a beginner with ember.js, and i don't know the best way to do this:
my application has devise gem, and it's a server side mvc application until you log in. Then it's ingle page. So all my ember code is declared inside the "inner" layout. In this layout, obviously, i also have my current_user.name method. I would like to use this information, and not doing an http request to get it.
I think that if there could be a way to put it inside my applicationController... it would be perfect!
Any ideas? Or maybe i'm trying to do the wrong thing?
thanks,
Marco
You can embed your current_user's json in a data-current-user attribute on the body.
<body data-current-user="<%= current_user.to_json %>">
This way you can check if they are authenticated, and get their attributes without an HTTP call.
Ryan Bats goes into detail on this: http://railscasts.com/episodes/324-passing-data-to-javascript
I'm using pagination on our Ruby on Rails app and the pages are defined in the request with a query string parameter like
http://ourdomain.com/?page=2
I use this on our front page where there's no page number explicitly defined.
I'd like to do a check to see if there is a query string, and if not then display the correct page=1, by redirecting to that page I presume
Does anyone know how I can do this?
Thanks ahead for any help
Simply check,
if request.query_string.present?
# Do your stuff here.
end
Thats it ! Cheers!!!!
if params[:page].blank?
redirect_to :page => 1
end
I would like to retrieve the id of the current item in a Controller#show page. How is this done? I can't seem to figure this out or find it anywhere.
You're probably looking for:
<%= params[:id] %>
There are lots of things wrong with doing this, one of which is it can open you up to CSRF attacks. Make sure to escape your output (this is done automatically in rails 3.)