I'm attempting to install Devise on a rather simple event creating/displaying Rails 4 app. I have 2 static pages and the index page displaying without authentication, and all is well there. Anything else kicks you to the "sign up" page. From there, when I attempt to create an account for myself (to simply see the other pages- simple vanilla devise installation attempt) I get a "No route matches [POST] "/registrations/user" error when clicking "submit" (f.submit)
I am using Rails 4, have the 3.0.3 version of Devise, bundled it, ran "rails generate devise:install" to install it, ran "rails generate devise user", ran db:migrate, raked the routes, and restarted the Rails server.
I even recreated the button action with the "correct" route and "get post" - no dice.
Anybody have any idea? I'm at a loss here.
Well it's always tricking debugging sever-side code without seeing it in action, but here's what should work.
yourappname/config/routes.rb :
devise_scope :user do
# matches the url '/register' to the registration controller's 'new' action and creates a :register symbol inorder to reference the link dynamically in the rest of the code
get 'register', to: 'devise/registrations#new', as: :register
end
(Assuming your register button is in your app's partial)
yourappname/app/views/layouts/application.html.erb:
<%= link_to "Register", register_path, class: "btn" %>
Now you could actually put that link_to anywhere, in any page, and it'll work. The reason for this is the 'register_path'.
When you create your route, the as: :register parameter creates a global variable that can be accessed throughout your app.
the register_path variable in your link_to method will always call the right action, even if you change your routes file.
It's just syntax. You could create any symbol you want. This would also work:
routes.rb:
# Inside get method, third parameter
as: :bowtiesarecool
someview.html.erb:
# Inside link_to method, second parameter
bowtiesarecool_path
Just match the variable name with the symbol name and a put a _path after it. The link_to method will handle everything else!
Related
I have a controller Posts in which I have a method:
def report_user
...
end
I have a view where I would like a link that will perform some logic (it should NOT actually take the user to a different page, only perform the logic and possibly show a dialog box after completion). The logic is contained in the report_user action in the Posts controller:
<%= link_to "Report User", :controller => :Posts, :action => :report_user %>
I would ultimately like to pass some variables also to the report_user action, however I haven't gotten that far as I've come across this error:
No route matches {:action=>"report_user", :controller=>"Posts"}
message << " missing required keys: #{missing_keys.sort.inspect}" unless missing_keys.empty?
raise ActionController::UrlGenerationError, message
end
I'm not sure what the issue is. There is definitely an action in the Posts controller called report_user and it is not private. I'm not sure what the missing required keys means either. I've seen on SO other people with that error, but they all have routes defined that require parameters. I do not have any routes defined for this. Possibly I"m going about the entire thing in the wrong way?
As Nils suggested, you need an entry in routes.rb. Assuming that this is a member route using a GET request, that entry would look like this.
resources :posts do
get :report_user, on: :member
end
Next, you need to update your link to use the routing helpers that Rails provides.
<%= link_to "Report User", report_user_post_path(#post), remote: true %>
I included the remote: true option b/c you mentioned that clicking the link shouldn't reload the page. The default response for this request will be app/views/posts/report_user.js.erb.
I would encourage you to read up on Rails routing at http://guides.rubyonrails.org/routing.html.
I have a controller file,
abc_controller.rb.
I have defined the show method inside it.
I have a view file,
show.html.haml
inside app/views/abc/
In my routes.rb file, I am giving the following command
resources :abc
I have a button
= link_to 'abc', abc_path, class: 'btn btn-default'
But when I click on the button, its not going to the new page.
I am getting non-existent route error.
Since I am a newbie to rails, I am not able to figure what the problem is.
You are getting a error because there is no such path as abc_path. Run rake routes and you'll see the routes that Rails understands. In you example, resources :abc produces the following routes.
abc_index GET /abc(.:format) abc#index
POST /abc(.:format) abc#create
new_abc GET /abc/new(.:format) abc#new
edit_abc GET /abc/:id/edit(.:format) abc#edit
abc GET /abc/:id(.:format) abc#show
PATCH /abc/:id(.:format) abc#update
PUT /abc/:id(.:format) abc#update
DELETE /abc/:id(.:format) abc#destroy
The first column are the named routes. So in order to get to the index action of abc_controller, the route is named abc_index_path. There is an abc_path but it needs an id params which means that you need to pass something to it. In your case, there's no definitive value to pass to this method so as a trial just use abc_path(1) which will redirect you to /abc/1. This goes to the show action with params[:id] set to 1.
If you do resources (plural) the resulting route for show requires an id: /abc/:id(.:format), so abc_path requires that you pass that :id or a object. If you are dealing with a singular abc (resource :abc), the resulting path doesn't require that :id, so you code should work (this is less common, but hard to tell with your "abc" example.
Lets say I have a controler test.
In test I define 3 actions:
def zah
end
def zeh
end
def zih
end
I have the views:
zah.html.erb
zeh.html.erb
zih.html.erb
and under routes.rb I have:
get 'test/zah'
get 'test/zeh'
get 'test/zih'
If I write under zah.html.erb, using code automaticaly created from rubymine IDE, this:
<%= link_to test_zeh_path%>
I will get my page source code with this:
http://localhost:3000/test/zeh
which makes the redirection from zah be itself.
running rake routes returns this:
Prefix Verb URI Pattern Controller#Action
test_zah GET /test/zah(.:format) test#zah
test_zeh GET /test/zeh(.:format) test#zeh
test_zih GET /test/zih(.:format) test#zih
Can anyone explain to me why is the link going to itself (from zah to zah) instead of another page(from zah to zeh)?
Edit:
I have found out that adding a name to a link makes the generated code works right:
<%= link_to 'zeh', test_zeh_path%>
I have seen the first usage (link_to test_zeh_path) here at 22:45.
Ruby on rails api does says that if nil name is passed then "the value of the link itself will become the name.".
As for a mistake of myself I was wondering why Dave Jones was able to create a link without a name, but he wasnt and that can be seen on his source code.
Because you have written the url in the display part.
You can simply do
<%= link_to 'Goto Zeh', test_zeh_path %>
and you will be good to go.
I'm pretty new to ruby and ruby on rails so please pardon me if this might sound very trivial to you.
I want to run a shell command on button click from my rails application. I've created required view and action, and also updated my routes.rb file to post to the required view. I also have defined a function in my model, which will finally be executing the shell command. Everything runs smoothly, with the workflow going as planned, but the command is not executed. Also, the app shows no error. Following are the snippets of my respective model, action and controller. I'am using Rails 4.1.1, with Ruby-2.1.2, on a Ubuntu 12.04 LTS.
Here is my routes file. I have overwritten the default /post route from directory#create to 'directory#create_directory'.
Rails.application.routes.draw do
match '/directories', to: 'directories#create_directory', via: 'post'
resources :directories
Model : The command i want to execute is specified in this function.
class Directory < ActiveRecord::Base
def self.create_directory
`mkdir dummy_dir`
end
end
Custom controller-action
def create_directory
#directory = Directory.create_directory
end
New directory view(generated using scaffolding)- This is the page where we land from the index page, after we click on create a new directory option. I've added a button_to, clicking on which redirects me to my custom action create_directory.
<h1>New Directory</h1>
<%= button_to "Create Directory",
url_for( :action => "create_directory", :controller=> :directories ),:remote=>true %>
<%= link_to 'Back', directories_path %>
I also have created a dummy view for create_directory action. I land on this view in the end.
Well, everything runs smoothly without giving any error, but the directory is not created. Any suggestions as to where i might be going wrong.
Thanks in advance
I recently turned to devise for my authentication routine. However, i'm still having some problems with it and mainly with routing. After a user logs in, i have the routes :
devise_for :users
namespace :user do
root :to => "town#index"
end
After i log in, i'm redirected to town controller. There i have this line :
<%= link_to raw("<p class='menu_head'>#{t('menu.inventory')}</p>"), :controller => "character" %>
This actually worked well with restful authentication, but for devise to work i have to specify the controller as "/character", else i get a :
No route matches {:controller=>"user/character"}
Though this route is incorrect. How can this be fixed ?
Moreover, i think this one is caused by devise once more. When a new user signs up, devise redirects them to town#index as it would happen if they have logged in. When this happens, for some reason current_user is not initialized. This only happens after sign up not when a user logs in. I'm pretty sure that i'm doing something wrong with devise.
You should be using the routing helpers rather than the options to get to this controller. Define a path helper in your routes file and use that instead. This way, you'll always be given the right URL.
At the moment, your application is sending you to users/town because that's what you're telling it to. The URL for sign in is /users/sign_in and by passing :controller you're saying "go to /users/town. If you were using named routes this would not occur.