I instructed config/routes.rb to use Refinery Blog as a root directory:
root :to => "refinery/blog/posts#index"
mount Refinery::Core::Engine, :at => '/'
In a app/view/layouts/_header.html.slim I'm trying to use blog_root route. For example:
= link_to (image_tag "/logo.gif"), blog_root, class: "brand"
The route is listed when I issue rake routes:
blog_root /blog(.:format) refinery/blog/posts#index
But nothing shows up, the system gives an error:
undefined local variable or method `blog_root' for
#<#<Class:0x00000005e62f80>:0x007fd7241d94c8>
Also, I tryed blog_root_path, but it didn't work either.
Anything I can do in this situation? Thanks a lot!
This question/answer pair was helpful.
I looked inside config/routes.rb of main app, and in comments it was writted that 'We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"'.
So, the working route is refinery.blog_root_path.
Related
I'm new to Rails, and I'm setting up models/controllers for Course and some other models.
When I visit the /courses/show URL in my browser I get the following error:
Couldn't find Course with 'id'=
Screenshot here.
Here's the relevant line from my rake routes and routes.rb:
rake routes
courses_show GET /courses/show(.:format) courses#show
config/routes.rb
get 'courses/show'
You have specified the four routes without any :id parameter, I don't know why you would expect them to have an :id parameter.
I'd recommend that you read the Rails guide on routing and also read the comments in the generated config/routes.rb, in that file you'll see comments like this:
# Example of regular route:
# get 'products/:id' => 'catalog#view'
So, extrapolating that to your example you might end up with:
get 'courses/:id' => 'courses#show'
The example that follows that one shows how to add a named route helper using the :as option:
get 'courses/:id' => 'courses#show', as: :courses_show
Something you'll also see when you read the guide or the comments is that you can use the resources helper to create standard restful routes.
I'm making a website for a class and I'm trying to implement a friend request function with a model called 'Users' and a join model called 'Relationships'. I have a button on the user#show page that should add a friend by using the create method in the Relationships controller. Here is the code for the button:
<%= link_to "Add as Friend", relationships_path(:friend_id => #user), method: :post %>
When I press the link, however, it tries to access the index method instead. After looking in the console, it looks like the link is sending a GET request, which routes to the index method, instead of a POST request, which routes to the create method. Can someone explain why this error is occurring and how I can fix it?
Edit: As requested, here is what I have in my routes:
Rails.application.routes.draw do
resources :interests
get 'interests/create'
get 'interests/destroy'
get 'home/index'
get 'sessions/create'
get 'sessions/destroy'
resources :users
resources :relationships
resources :subscriptions
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'home#index'
get "/auth/:provider/callback" => "sessions#create"
get "/signout" => "sessions#destroy", :as => :signout
Using a link_to helper indicates to Rails that you'd like to produce an a tag in your HTML. No element of the HTML specification regarding a tags allows for producing POST requests. Because Rails understands the utility of allowing for POST and DELETE requests to be issued using links, however, it provides those options in the link_to helper. It's implementation, though, must use JavaScript under the hood in order to appropriately function.
Check that jquery-ujs is installed, and that your asset pipeline is working correctly in order to use the helper in this way.
You may also evaluate whether using a form_for and a button is better, since that will automatically POST.
I'm pretty sure you are matching the wrong route. Run rake routes and see the route that links to the Relationships#create.
Using 'url' instead of 'path' with the route helper solved the problem for me. So instead of 'relationships_path' use 'relationships_url'.
I'm in trouble with routes in Rails. I've been trying to get a path like: home/blog/title-article but the closes I get is: home/blog/posts/title-article
I've tried with namespace, scope and one by one with get 'blog' => 'posts#blablabla', but I get errors like UrlGenerationError or NameError every time I change the paths. I've read the official guide and some answers in this forum, but I'm getting more confused hehe
In my last try I generated a controller Blog and a scaffold Post. The output of rake routes is: http://i.stack.imgur.com/gdfPc.png
routes.rb
Rails.application.routes.draw do
scope :blog do
resources :posts
end
get 'blog' => 'blog#index'
root 'pages#index'
...
Thank you!
Now my routes are like: http://i.stack.imgur.com/cKsFG.png
Thank you!
Not sure its what you expect but try:
resources :posts, path: 'blog'
Feels weird though.
Btw, I guess you have the errors due to url helpers.
use rake routes to check the existing routes and the related url helpers.
Doc lives here
I'm using Rails 3 and have the feeling that the syntax for changing the route for the index (http://localhost:3000) is different than from the former versions.
I'd like to open the dynamic index page (index.html.erb) of the employees-controller (which can be right now opened with localhost:3000/employees) as the default page (localhost:3000). I thought it's quite easy, because in the routes it's written:
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
So that's what I actually did: I deleted public/index.html and set root :to => "employees#index".
But when I open the server and open localhost:3000, it's still opening the "welcome abroad!"-page. Pretty weird!
So I googled the problem and found answers which said, I should write this into my routes-file:
map.root :controller => 'employees', :action => 'index'
Same here - I also still get the "welcome abroad!"-page and the rails-shell says "undefined local variable or method 'map'". (I think this is the old syntax for Rails 2...?)
match "/" => "employees#index" says routing error: No route matches "/"
So what did I do wrong? How can I solve this problem?
Why you use "map" in Rails 3? Should be:
root :to => "employees#index"
I think problem is of cookies. please clear the cookies and the refresh the page.
i have an RoR application Log, which similar to the book store app, my logs_controller has all default action: index, show, update, create, delete..
now i need to add new action :toCSV, i defined it in logs_controller, and add new route in the config/routes as:
map.resources :logs, :collection => { :toCSV => :get }.
from irb, i checked the routes and see the new routes added already:
>> rs = ActionController::Routing::Routes
>> puts rs.routes
GET /logs/toCSV(.:format)? {:controller=>"logs", :action=>"toCSV"}
then ran ‘rake routes’ command in shell, it returned:
toCSV_logs GET /logs/toCSV(.:format) {:controller=>"logs", :action=>"toCSV"}
everything seems working. finally in my views code, i added the following:
link_to 'Export to CSV', toCSV_logs_path
when access it in the brower 'http://localhost:3000/logs/toCSV', it complained:
Couldn't find Log with ID=toCSV
i checked in script/server, and saw this one:
ActiveRecord::RecordNotFound (Couldn't find Log with ID=toCSV):
app/controllers/logs_controller.rb:290:in `show'
seems when i click that link, it direct it to the action 'show' instead of 'toCSV', thus it took 'toCSV' as an id...anyone know why would this happen? and to fix it? Thanks...
map.resources :logs, :collection => { :toCSV => :get }
I think this is perfect. you must restart your server evry time you change the config/routes.rb
It's no answer though but it's important.
This can be a workaround:
Create a named resource:
map.toCSV 'logs\toCSV', :controller => :logs, :action => :toCSV
I am really sorry i forgot to mention the main point!
In your view it should be:
link_to 'Export to CSV', toCSV_path
Also, these named routes come in handy especially when you have authentication involved. For instance, during signup, rather than directing the user to \user\new you can direct him to \signup. Its more friendly.
Thats it!!
Its simpler and it works. Cheers! :)
Remove the map.resources line from routes.rb, and then run rake routes. If you see a route /logs/:id, that is the route that should probably be removed.