I've used a form successfully in embedded Ruby that looks like this:
<%= form_for :comment, url: user_comments_path do |c| %>
<%= c.text_field :location %>
<%= c.text_field :title %>
<%= c.text_field :body %>
<%= c.submit ("Submit") %>
<% end %>
However when I tried to put this into Bootstrap I came up against problems. I then tried to make a simpler HTML form like this:
<form accept-charset="UTF-8" action="user/comments" method="post">
<input id="comment_location" name="comment[location]" type="text"/>
<input id="comment_title" name="comment[title]" type="text"/>
<input id="comment_body" name="comment[body]" type="text"/>
<input name="commit" type="submit" value="Create"/>
</form>
...but I came up against the same error message: No route matches [POST] "/users/1/comments/new/user/comments". I am using nested resources, and Rails routes in the terminal tells me that the user_comments path 'POST' equates to user/user_id/comments and the Create controller action.
In my HTML form if I enter action="user/comments" then the resulting path is "/users/1/comments/new/user/comments", if I enter action="comments" the path is "/comments", and if I enter "/" the path is "/". The path that I'm trying to reach is "user/comments" but there seems to be no way there through my HTML form.
I've racked my brains but I can't understand why this might be. Any help would be much appreciated! Thanks
ps. Routes look like this:
Rails.application.routes.draw do
get 'sessions/new'
get 'welcome/index'
root 'welcome#index'
get 'user' => 'users#show'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users do
resources :comments
end
resources :sessions
end
update routes.rb use collection instead of resources to get /users/comments like path
resources :users do
collection do
get 'comments'
end
end
Related
I am facing a problem with link_to and yield. I was successfull in showing the root page (start page) in the yield-area of application.erb.html all other links like "help" (see below) are shown on a seperate page.
that is what my application.erb.html looks like:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%= render 'layouts/navbar' %>
<div class="main">
<div class="main-inner">
<div id="content">
<%= yield %>
</div>
</div>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
In navbar.html.erb I definied two links:
<%= link_to "Start", root_path %>
which works perfect. the second one
<%= link_to "Help", help_path %>
works but is not shown in the yield-area.
routes.rb looks like this:
Rails.application.routes.draw do
get 'set_language/english'
get 'set_language/german'
#scope "(:locale)", :locale => /en|de/ do
# root :to => 'pages#home'
# get "pages/home"
#end
get 'pages/home'
get 'pages/help'
get 'sessions/new'
get 'users/new'
resources :sessions, :only => [:new, :create, :destroy]
root :to => "pages#home"
#root 'pages#home'
get 'help' => 'pages#help'
get 'about' => 'pages#about'
get 'contact' => 'pages#contact'
get 'signup' => 'users#new'
post 'signup' => 'users#create'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
#delete 'logout' => 'sessions#destroy'
get 'logout' => 'sessions#destroy'
I tried using content-for and giving the yield a name identifier but unfortunately without any success.
im using gem parse-ruby-client and im trying to create a login. and when the login is successful then i want to go to a welcome#index
here is my login_controller.rb
class LoginController < ApplicationController
def index
end
def log_in
#user = Parse::User.authenticate(params[:user][:username], params[:user][:password])
end
end
index.html.erb
<div class="Log_in_Form">
<h4><center>Log in with your existing "app_name" account</center></h4>
<%= form_for(:user, :url => {:controller => 'login', :action => 'log_in'}) do |f| %>
<center><p> Username:</br> <%= f.text_field :username%> </p></center>
<center><p> Password:</br> <%= f.password_field :password%></p></center>
<center><h4><%= f.submit :Login %></h4></center>
<% end %>
</div>
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
root 'login#index'
get 'login/log_in' => 'login#log_in'
end
you need to have an post route or your login. change your routes to this one (if you need the get route also)
Rails.application.routes.draw do
get 'welcome/index'
root 'login#index'
get 'login/log_in' => 'login#log_in'
post 'login/log_in' => 'login#log_in'
end
or change
get 'login/log_in' => 'login#log_in'
to
match 'login/log_in' => 'login#log_in', via: [:get, :post]
I'm trying to implement a Twitter Boostrap login form, that's gonna be used on every page (because the navigation bar is a part of the layout).
However, when trying the code below I get the following error:
No route matches {:action=>"show", :controller=>"users"}
User controller:
class UsersController < ApplicationController
def index
#users = User.all
end
def show
...
end
def login
...
end
end
_navigation.html.erb:
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
<%= form_for("user", :url => user_path) do |f| %>
<%= f.label :email%>
<%= f.text_field(:email, :size => 30, :class => 'login_field', :placeholder => 'Användarnamn')%>
<%= f.label :password%>
<%= f.text_field(:password, :size => 30, :class => 'login_field', :placeholder => 'Lösenord')%>
<%= f.submit "Logga in", :class => 'login_submit btn btn-primary' %>
<% end %>
</div>
config/routes.rb:
get "home/index"
resources :users
resources :projects
resources :tickets
root :to => 'home#index'
rake routes (that has to do with users):
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
I'm new to Rails but find it strange that it complains that the route doesn't exist because the action "show" is to be found inside the user controller.
The other thing I'm wondering about is why it looks for the action "show", while it should be "login" in this case?
Why is this happening and what shall I do?
your error is in this line
<%= form_for("user", :url => user_path) do |f| %>
user_path is expecting an id. if you change that to users_path, that should fix it but I don't think that's your intention.
UPDATE: to use the login action on the users controller, you need to update your routes
resources :users do
post :login, on: :collection, as: :login
end
passing the :as option creates a named_route for you called login_users_path which you can use on your form_for. and since we wanted to do a post, we also need to specify that in the form_for
<%= form_for("user", :url => login_users_path, :html => { :method => :post }) do |f| %>
Update your routes.rb to look like:
get "home/index"
resources :users do
post :login, :on => :collection
end
resources :projects
resources :tickets
root :to => 'home#index'
and in your view file change the form_for line to be:
<%= form_for("user", :url => login_users_path) do |f| %>
resources :users only adds default routes. If you want to add new action (other then defaults) you need to use 'collection. And you can specify the method get or post. After adding to routes.rb. You can get the path by running rake routes then you add the correct route in the action of form.
resources :users, :collection => {:login => :post}
I've followed a railscast on creating and displaying static pages using a pages model and my code looks like this:
Pages model
has fields of name, permalink and description.
Routes:
get "log_in" => "sessions#new", :as => "log_in"
get "log_out" => "sessions#destroy", :as => "log_out"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :pages
match ':permalink', :controller => 'pages', :action => 'show'
_footer.html.erb
<div class="footer">
<div class="footer_container">
<%= link_to 'About Us', root_path('about') %>
</div>
</div>
going to /localhost:3000/about displays the about page correctly but the link in the footer wants to direct to /localhost:3000/.about and actually links to the sign up a new user page.
How can I get my link to direct to /about and display the page?
Thanks for any help its much appreciated!
root_path will always take you to users#new because that is what you specified in your routes.rb file. What you can do is name your last route with the :as key, like this:
match ':permalink', :controller => 'pages', :action => 'show', :as => 'my_page'
Then in your views, you should be able to do something like this:
<%= link_to 'About Us', my_page_path('about') %>
I'm trying to make some nice simple routes in Rails 3 with 2 custom matchers which assume the root :id will be a city and :city_id/:id will be a place... it seems to work fine except when trying to edit.
Ie.
root_url/countries/france
root_url/paris/some_place
root_url/paris
Here's my code to be more precise.
resources :countries do
resources :cities
end
resources :cities do
resources :places, :reviews
end
match ':id' => 'cities#show', :as => :city, :method => :get
match ':city_id/:id' => 'places#show', :as => :city_place, :method => :get
That seems to work perfectly accept when I try to edit records. The html is as below:
<% form_for #city do |f| %>
<% end %>
Produces:
<form accept-charset="UTF-8" action="/kiev" class="edit_city" id="edit_city_3581" method="post">
Which would only work if it were:
<form accept-charset="UTF-8" action="/cities/kiev" class="edit_city" id="edit_city_3581" method="post">
I know I could simply provide a more advanced form_for route explicitly to get around this, but I'm wondering if there's something better to do in my routes.rb to make my life easier rather than patching?
Thanks
How about if you renamed your custom routes like this and let normal routes handle edit etc.
get ':id' => 'cities#show', :as => :city_shortcut
get ':city_id/:id' => 'places#show', :as => :city_place_shortcut