Understanding Usage of Symbols in Routes.Rb Files

Understanding usage of symbols in routes.rb files

get :account (using symbol) and get 'account' (using string) are exactly the same in this context. In your route the symbol will be translated to a string by Rails.

It's just a coding style, I personally use the symbols because I like to see the colors in my IDE, it helps me reading my code faster.

And to answer your other question: no you don't need to define symbols anywhere, those are not a method or a variable. You can see them as a constant with a value equal to their names.

Edit: If it's still confusing you can read this pretty complete guide on symbols in Ruby: http://www.troubleshooters.com/codecorn/ruby/symbols.htm

In routes.rb, what does the # symbol mean?

Refer here, the 'stores#index' would redirect to index action of stores controller

Understanding what's going on with : and # in ruby, rails routes

The route is working for you. Thats why ur getting error from users_controller#show.

In your show action you must be doing something like User.where(:id => params[:id]). But in this case, the attribute in your params is called :user . So to make make both routes work, without any change in the show action, change the route to

match '/:id' => "users#show", :as => :user_profile

Customizing a get URL in routes.rb file gives undefined method {symbol}_path

To use form_for, you need to define a "POST" method, that the form can submit to, at the moment you are only setting up a GET for returning the form that you then will fill in.

In your routes.rb file you need to do something like this:

post "/basic_query", :as => "basic_queries"

or

match "/basic_query", :to => "basic_queries#create", :via => :post, :as => "basic_queries"

That should do it.

Accessing URL helpers in routes.rb

I know I'm a little late here, but this question is one of the top hits when googling "use url_helpers in routes.rb", and I initially found it when I had stumbled upon this problem, so I'd like to share my solution.

As @martinjlowm mentioned in his answer, one cannot use URL helpers when drawing new routes. However, there is one way to define a redirecting route rule using URL helpers. The thing is, ActionDispatch::Routing::Redirection#redirect can take a block (or a #call-able), which is later (when the user hits the route) invoked with two parameters, params and request, to return a new route, a string. And because the routes are properly drawn at that moment, it is completely valid to call URL helpers inside the block!

get 'privacypolicy.php', to: redirect { |_params, _request|
Rails.application.routes.url_helpers.privacy_policy_path
}

Furthermore, we can employ Ruby metaprogramming facilities to add some sugar:

class UrlHelpersRedirector
def self.method_missing(method, *args, **kwargs) # rubocop:disable Style/MethodMissing
new(method, args, kwargs)
end

def initialize(url_helper, args, kwargs)
@url_helper = url_helper
@args = args
@kwargs = kwargs
end

def call(_params, _request)
url_helpers.public_send(@url_helper, *@args, **@kwargs)
end

private

def url_helpers
Rails.application.routes.url_helpers
end
end

# ...

Rails.application.routes.draw do
get 'privacypolicy.php', to: redirect(UrlHelperRedirector.privacy_policy_path)
end

Russian symbols in url and route constraints

I think that you need to handle the unescaping of those characters yourself. Have a look at this example here: Redirect when using I18n with Rails is encoding the forward slash as %2F

Rails routes get 'frog' vs get :frog (probably really easy)

In your example, :frog is a ruby symbol. It's easily converted to a string by call to_s on it (try :frog.to_s in irb or the console). Similarly, you can covert a string to a symbol by calling to_sym (try "frog".to_sym in irb or the console). The rails authors decided to accept either a string or a symbol in this case, since is trivial to change from one to the other.

What's the use of get routes rails generate controllers created in routes.rb in rails?

The 'get' is the HTTP verb supported by the routing engine. It's possible to route GET and POST (and the rest) to different methods, even if they hit the same URL. It's also possible to only support certain verbs for certain URLs (as is happening here).

Regarding their necessity - we'd have to see the rest of your routes.rb file to know. If you have a default match rule, that likely will take effect if these are removed.



Related Topics



Leave a reply



Submit