I am running into an issue when I delete any resource. Before I delete something, current_user returns the correct User; however, after I delete something, current_user returns nill. I tried this with multiple resources and it does the same thing. Has anyone ever encountered a similar problem?
EDIT: I also created a UsersController to allow me to manage the users. My routes file contains:
resources :users
devise_for :users
Found solution: my layout did not have <%= csrf_meta_tag %>. Found answer in this question
Related
I'm working on a project where users can upload videos through a simple form and additionally by FTP to a certain directory and then simply choose the file from the FTP directory instead of uploading it through the form.
I got the following, pretty standard setup for a videos_controller:
# routes.rb
resources :videos
# new.html.rb
form_for(#video) do |f|
...
end
The restful actions in the controller are all working and just standard behaviour. The upload works, that's not the problem. The problem is if I do the following:
# routes.rb
resources :videos do
member do
post :from_ftp
end
end
# new.html.rb
form_for(#video, :url => from_ftp_video_url) do |f|
...
end
I get the error: No route matches {:action=>"from_ftp", :controller=>"videos"}, because the generated route looks like this:
from_ftp_video POST /videos/:id/from_ftp(.:format) videos#from_ftp
which seems right, since it's a member route. But I don't need the :id part of the URL, since I'm creating a new Video object, not through a form but simply by using the file from the FTP directory... So it basically is another create action, that's why I would like to do it as a POST request...
So how do I tackle this the best way?
Although the selected answer is correct for Vapire's situation, it doesn't necessarily answer the title question. If you came here looking for how to get member actions without an ID because you don't need an ID, the answer is a little different.
Say you implemented authentication that sets current_user. You let users edit their own profile only. In that case users/:id/edit doesn't make sense because :id is dictated by the current_user method. In this case /users/edit makes more sense.
You can change your routes.rb file to create member actions without an id in the path.
...instead of this...
resources :user
...use this (note the plurality of resource)...
resource :user
The way to understand member and collection routes is this:
Member routes do something to an object that you have.
Collection routes do something to the set of all objects.
So when we consider what the create route would be, it's a collection route, because it's adding a new object to the collection!
So your from_ftp method should also be a collection route, because it's adding to the collection.
Also, you might want to consider if you can accommodate the FTP functionality within your existing create method - it might be neater.
I am working on a basic rails app and wondering how do I show /users/:id/posts/:id as /posts/:id. Right now my routes is
resources :users do
resources :post
end
Which i need to have so that every post gets associated by the user. But also at the same time for ease of use, i need to be able to show the post at /posts/:id
Any help of suggestion is appreciated.
Thanks
You can declare a second route to the same controller:
resources :users do
resources :posts
end
resources :posts
You just need to handle not having a user_id parameter in your posts controller gracefully.
you probably looking for shallow option in Rails
resources :users, :shallow => true do
resources :post
end
http://rails-bestpractices.com/posts/11-needless-deep-nesting
I have a User model which has_one :blog. In my routes.rb file I have resource :blog. Why doesn't it create a path for blog#show? Do I need to create one myself? Otherwise how do I link_to blog#show?
It does create a path for blog#show - see the output of your routes by running rake routes. The route missing is blog#index, because there is only one to show.
See http://guides.rubyonrails.org/routing.html#singular-resources for more information on singular routes.
In my application, a User has many Posts, and a Post has many Comments. How should I configure the routing? I have configured it like this:
resources :users do
resources :post do
resources :comments
end
end
Some articles say this isn't recommended because it will be confusing.
Yes, It is not recommened. But Allowed in rails
You should use something like this
resources :users do
resources :posts
end
resources : posts do
resources :comments
end
As Deep Nesting Will create problems like, long path names in rails, and long urls in urls.
How do I determine the link_to arguments for show, edit, and destroy? I know "new" is "new_user_post_path". Nothing else seems to follow this convention though?
My relevant routes.rb snippet:
resources :users do
resources :posts
end
Running
rake routes
from your rails root should tell you what you need to know in terms of the name of the routes and the parameters expected.