Accessing Views Within Ruby on Rails Application - ruby-on-rails

I want to make a view for my website coded in RoR wherein all the available path are listed as links. Is there any way to access the views for various models from the program?
For example, something like:
<%model.views.each do |v| %>
<%= link_to v %>
<% end %>

You could use the sitemap_generator or dynamic_sitemaps gem to generate Sitemaps for your application.

You can use named routes, which allows you to create a link to a different part of your rails app, based on names you set in routes.rb. You can also include route parameters, too, which makes it easy to link to models.
In your routes.rb
get 'test_route/:id' => 'example#controller', as :test
In controllers/views:
link_to #example.name, test_path(id: #example.id)
Further reading on named routes

Im not sure why you want this :), but this will give you the routes
Rails.application.routes.routes
if you want to get all the paths as an array
Rails.application.routes.routes.collect {|r| r.path.spec.to_s }

Related

Shopify Route Error: undefine shopify_api_[resource]_path

Currently I am learning to create a Shopify custom app. I am working on metafields. A metafield can be created by
#metafield = ShopifyAPI::Metafield.new()
In form view, I use
<%= bootstrap_form_for #metafield do |f| %>
<% end %>
The form requires me to define shopify_api_metafields_path etc.
Is there a way to define prefix shopify_api to my routes resources :metafields without adding ShopifyApi:: module to my controller?
You could try:
resources :shopify_api_metafields, controller: :metafields
Which will give you:
shopify_api_metafields GET /shopify_api_metafields(.:format) metafields#index
POST /shopify_api_metafields(.:format) metafields#create
new_shopify_api_metafield GET /shopify_api_metafields/new(.:format) metafields#new
edit_shopify_api_metafield GET /shopify_api_metafields/:id/edit(.:format) metafields#edit
shopify_api_metafield GET /shopify_api_metafields/:id(.:format) metafields#show
PATCH /shopify_api_metafields/:id(.:format) metafields#update
PUT /shopify_api_metafields/:id(.:format) metafields#update
DELETE /shopify_api_metafields/:id(.:format) metafields#destroy
I come up with solution by defining a model Metafield which inherit from ShopifyAPI::Metafield and then, I define #metafield = Metafield.new instead of #metafield = ShopifyAPI::Metafield.new
By this way, it will be easier to manage later and it allow us to synchronize data table as well.

Rails: link_to only if route exists

I'm writing a Rails Engine and in one of my views I'd like to create a link to main_app if a named route exists.
I will give an example to clarify the question:
The Main application may or may not define in routes.rb:
resources :my_resource, only: :index
In my rails engine I need to add a link to my_resource_path only if main_app defined that route. I think it should be something like the following:
<%= link_to "Link", main_app.my_path if my_resource_path_exists? %>
I tried:
<%= link_to "Link", main_app.my_resource_path if
main_app.respond_to?(:my_resource_path) %>
but, when the path does not exist, that raises NoMethodError:
undefined method `my_resource_path' for #<Module>
How my_resource_path_exists? method could be implemented? I'm using ruby 2.2 and rails 4.2
The question is related to: Determine if path exists as route in Rails controller
But in this case, I have a named route (my_resource_path), so I need something slightly different, but can't figure out by myself.
I found a solution wich does not require to recover from fail. It's possible to check if the route is defined using url_helpers:
Rails.application.routes.url_helpers.method_defined?(:my_path)
So, the code could be written like that:
<%= link_to "Link", main_app.my_resource_path if
Rails.application.routes.url_helpers.method_defined?(:my_resource_path) %>
You could either use main_app.try(:my_path) or rescue NoMethodError to return false.

default route in rails

I had a doubt when i was writing rails code.
In my link_to i used my route order to show my order. So:
<% #orders.each do |order| %>
<tr>
<th><%= order.name %></th>
<th><%= link_to 'Mostra', order %></th>
</tr>
<% end %>
I saw my rake routes and there was a :
order GET /orders/:id(.:format) orders#show
If i remember right i generated Order resource with scaffolding. However , when i created by hand new resources (not using scaffolding)
i had a different route for my resource. For example , i have something like name_resource_show(:id) for the show. This kind of style is good cause i understand that i have to pass the id , if i want to see a specific resource. But in the case before , the case of order , i really don't know how rails is able to understand to use the id of the object order. And also:
why i have different routes name? why i have sometimes _path and sometimes (maybe when i generate resource with scaffolding) other things?
i would expect something like order_show(:id) and not simply order.
how it works?
Rails helpers are smart enough to use model object to form url.
<%= link_to 'Mostra', order %> equivalent to <%= link_to 'Mostra', order_path(order) %> and both points to order show page.
This will generate 7 routes for your controller orders.
resources :orders
order GET /orders/:id orders#show
Here order is the helper method it provides to call routes instead of using /orders/:id.
Simply you can use order_path(order) to get route /orders/:id
Similary we get helper for all 7 routes. You can also override the helpers.
Go to below link for more information.
Reference: http://guides.rubyonrails.org/routing.html
First, I recommend following the Rails conventions on routes (see the main reference article here).
Here are answers to your questions in order.
The route you got from rake routes makes sense in the following way. Look at the URL (orders/:id). Within all of your orders, the :id passed specifies which one to look at. The GET nature of the request indicates you are getting the data on that record, i.e. that it is a SHOW action.
Rails understands where the ID is because of how the routes are structured. If you had order GET /orders/:year/:id in routes, then Rails will know to look for the third parameter for the ID it needs.
The two for accessing routes options are _path and _url (see here for details), but there are some alternatives explained in the main reference article I linked at top.
You can still use the explicit route, but the order option is simply a bit of sugar Rails offers to make things easier to read.

What does xxxx_path mean in ruby on rails

I try to add new controller and model use name foo and foos_controller, hope foos_path can redirect. Doesn't work.
A origin code here (working):
href="<%= contacts_path %>"
After I add new controller and model follow name convention I try use the same (Not working):
href="<%= foos_path %>"
And this contacts_path is not defined anywhere else in rb project.
what does xxxx_path mean and how to use it?
Rails follows convention to handle roots of application
when we execute this command
rails g scaffold foo
it generates routes along with your model, controller and views.
it generates a line in routes.rb as
resources :foo
this line makes you access all the actions of your controller
for example:
foos_path: # redirects you to the index page of your foos controller
new_foo_path: # redirects you to the create page of your foos controller etc.,
please go through this link for reference: http://guides.rubyonrails.org/routing.html
If you go to your terminal and type rake routes, it will list your currently defined routes. On the left side you'll have their prefix. For example, contacts might route to the index action in ContactsController.
The path suffix is a way of referring to those routes inside your code.
If foos_path is giving you an error, that means you either have not yet defined that route for the resource, or you have but it is called something else (if you defined it manually with the as: option).
More info can be found in the Rails Guide for Routing.
You'll be best reading up on this documentation
Path Helpers
Basically, the path_helpers of Rails are designed to help you define routes for your links etc in the most efficient way possible.
The bottom line is the path helper will take routes defined in your config/routes.rb and then allow you to call them dynamically - IE:
#config/routes.rb
resources :photos #-> photos_path
The path names are typically from your controllers, allowing you to route to the various actions inside them. As Rails is built around being a resourceful structure, it will by default create routes for the "standard" controller actions:
link_to
In order to use the path helpers effectively, you'll be best using the rake routes command in cmd, or by simply typing an invalid url into your app's address bar
I notice you're using the standard HTML <a href=""> tag in your question. You'll be much better suited to using the link_to helper of Rails:
<%= link_to "text", your_path %>

1.8.x Ruby on Rails RESTful nested admin page and form_for problems

So I am creating a website that I want to have an admin directory in rails 1.8.x and I'm struggling a bit to get the form_for to link to the right controller. I am trying to be RESTful. What I basically want is an admin page that has a summary of actions which can then administer sub models such as:
/admin (a summary of events)
/admin/sales (edit sales on the site)
/admin/sales/0 (the normal RESTful stuff)
I can't use namespaces since they were introduced in Rails 2.0 (production site that I don't want to mess with updating rails and all that).
Anyway, what I have in the routes.rb is:
map.resource :admin do |admin|
admin.resources :sales
end
I am using the map.resource as a singleton as recommended by another site.
The problem comes in when I try to use the form_for to link to the subresource RESTfully. If i do :
form_for(:sales, #sale)
it never links to the right controller no matter what I try. I have also tried:
form_for [#admin, #sale] do |f|
and that does not work either (I am guessing since admin is a singleton which does not have a model, it's just a placeholder for the admin controller). Am I supposed to add a prefix or something? Or something into the sales controller to specify that it is a subcontroller to admin? Is there an easier way to do this without manually creating a bunch of routes? Thanks for any help.
You can use controller, action and html options something like following.
<%= form_for :sales, :controller=>controllerName, :action=>actionName, :id=>idParameter}, :html=>{:onsubmit=>"someFunction()"} do |f| %>
|
|
|
<% end %>

Resources