form_for method undefined - ruby-on-rails

I read a book called 'Rails 3 in Action' and made two pages: 'index' and 'new', set routes.rb:
root :to => 'projects#index'
match '/new', to:'projects#new'
and projects_controller:
def new
#project = Project.new
end
def create
#project = Project.new(parmas[:project])
#project.save
flash[:notice] = "Project has been created"
redirect_to #project
end
and view files:
index.html.erb
<%= link_to "new", new_path %>
This works correctly, because I end up at localhost:3000/new, but the problem is:
<%= form_for(#project) do |f| %>
This results in:
undefined method `projects_path' for #<#:0x416b830>
Where is projects_path? When I print <%= root_path %>, I get /, but <%= projects_path %> gives error undefined method.
How do I define a method projects_path? Root is not projects_path?

You should define resource for project in routes.rb
resources :projects
This will generate helper projects_path and a banch of others

Remove the line match '/new', to:'projects#new' from routes.rb and add this:
resources :projects

The path used to create new projects is a HTTP post to the index URL of "/projects". You need to specify this route:
post "/projects", :controller => "projects", :action => "create", :as => "projects"
This will generate a projects_path, which is the helper method your form_for is looking for.
As others have said, you should probably use resources :projects instead. If you're interested in only creating a subset of the seven RESTful routes created by default, you can use :only:
resources :projects, :only => %w(index new create)

Related

twice nested form path issue

i have the following form for my user_calendar model:
<%= form_for([company,user,calendar], :remote => true) do |f| %>
....
<% end %>
route is the following:
scope '(:locale)' do
resources :companies do
resources :users do
resources :user_calendar
end
end
end
When user clicks Edit on users/home:
<%= link_to 'E', edit_company_user_path(user.company, user), :remote => true %>
The method is called from users_controller:
def edit
#calendar = #user.user_calendars.build
respond_to do |format|
format.js
end
end
And edit.js should load the models data for the form:
$('#calendar-form').html("<%= escape_javascript(render :partial => 'user_calendars/form', :locals => { :company => #company, :user => #user, :calendar => #calendar }) %>");
But instead of rendering the form i get the following error:
ActionView::Template::Error (undefined method `company_user_user_calendars_path' for #<#<Class:0x007fc351d12808>:0x00000001cf6128>):
The three #company, #user and #calendar have the right data, but still the path shows the error. Here the result of rake routes on user_calendars controller:
Controller#Action
company_user_user_calendar_index GET (/:locale)/companies/:company_id/users/:user_id/user_calendar(.:format) user_calendar#index
POST (/:locale)/companies/:company_id/users/:user_id/user_calendar(.:format) user_calendar#create
new_company_user_user_calendar GET (/:locale)/companies/:company_id/users/:user_id/user_calendar/new(.:format) user_calendar#new
edit_company_user_user_calendar GET (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id/edit(.:format) user_calendar#edit
company_user_user_calendar GET (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#show
PATCH (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#update
PUT (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#update
DELETE (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format) user_calendar#destroy
it could happens because of: resources :user_calendar
if you need resources you should write resources :user_calendars - with (s)
also you could look to guid about resources/resource

Rails route error switch local

I'm setting my new Rails website in more than one language, and I'm having problems with the routes. I'm following the instruction of the book 'Agile web development with Rails 4'.
The browser print me this error but I can see that routes are created correctly, so:
What am I doing wrong? (At the end of this message I'll attach all my routes)
No route matches [POST] "/en/home"
When I try putting the routes directly in the browser ("localhost:3000/en" OR "localhost:3000/es") everything works OK. The error prints only when I change my language's switcher. That's why I think the routes are correctly set, and I think is a problem of my switcher or the controller...?
This is the code in the application.html.rb (basically a switcher between languages):
<%= form_tag home_path, class: 'locale' do %>
<%= select_tag 'set_locale',
options_for_select(LANGUAGES, I18n.locale.to_s),
onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
This is the configuration of my routes.rb file:
Group::Application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
scope '(:locale)' do
resources :posts
resources :contacts
root "posts#home"
get "/home" => "posts#home"
get "/contact" => "contacts#new"
# static pages
get "/investment" => "contents#investment"
get "/partner-with-us" => "contents#partner", as: "partner"
get "/our-companies" => "contents#companies", as: "companies"
get "/site-map" => "contents#sitemap", as: "sitemap"
get "/terms-and-conditions" => "contents#terms", as: "terms"
get "/privacy" => "contents#privacy"
end
end
This is a file created in /config/initializers/i18n.rb:
#encoding: utf-8
I18n.default_locale = :en
LANGUAGES = [
['English', 'en'],
["EspaƱol".html_safe, 'es']
]
And finally, this is the code for my posts_controller.rb, because here is where I create an action "home" in order to put the last post in the home page:
class PostsController < ApplicationController
def index
#posts = Post.all.order("created_at desc")
end
def show
#post = Post.find(params[:id])
end
def home
if params[:set_locale]
redirect_to home_url(locale: params[:set_locale])
else
#posts = Post.all.order("created_at desc")
end
end
end
try to add :method => :get to your form, like this
<%= form_tag home_path, class: 'locale', :method => :get do %>

ActionController::RoutingError (No route matches {:controller=>"users", :action=>"profile"})

I inherited an old Rails app and I'm really struggling with this error:
ActionController::RoutingError
(No route matches {:controller=>"users", :action=>"profile"}):
app/views/layouts/application.html.erb:59:in
`_app_views_layouts_application_html_erb__105<snip>'
app/controllers/users_controller.rb:40:in `index'
I ONLY get this error when I log in as admin, not as any other user.
Here is my routes.rb
Vvault::Application.routes.draw do
resources :orders
devise_for :users
resources :videos
resources :users
root :to => "users#index"
match 'users/:id/profile' => 'users#profile', :as => :user_profile
end
I think this is the relevant snippet from users_controller.rb:
def index
if current_user.admin?
# admin sees all users
#users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
else
redirect_to "/users/" + current_user.id.to_s() and return
end
end
I think this is the relevant snippet from application_html.erb:
<div class="sidebar">
<% if user_signed_in? %>
<%= link_to "My account", user_profile_path, :method => :get %>
<% end %>
</div>
If i comment out the third line of application_html.erb I can login as admin, but obviously the profile link does not render for any user.
Any help appreciated.
Thanks!
Try:
<%= link_to "My account", user_profile_path(current_user.id), :method => :get %>
Your user_profile_path helper needs an id to be passed in, as the corresponding route has an :id parameter.
The error message is telling you that no route exists for that controller/action combination without any other parameters.
Or, you need to define a route without an id parameter where the controller automatically loads the current user's profile
You need to provide the id to user_profile_path. But since that route points to the user's account, there is no point in setting the id, because it should always be the current user (is that your intention?). If this is the case, then you can rewrite the route as:
match 'users/profile' => 'users#profile', :as => :user_profile
If not, then provide an id to the helper method for your route.

Rails 3 : Can't get form_for to work as a 'delete' following the RESTful achitecture => always giving a ROUTING ERROR

I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => #user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.
--edit
Routes for relationship resources:
resources :relationships, :only => [:index, :create, :destroy]
Unfollow button (creates a form for itself):
= button_to "Unfollow", relationship_path(relationship), :method => 'delete'

What is wrong in "mapping" URLs to Rails actions with this form?

In 'app/views/users/reset.html.erb' file I have this code:
<%= form_tag( send_reset_users_path, :method => :post ) do %>
<%= text_field_tag :email %>
<%= submit_tag("Send") %>
<% end %>
In 'app/controllers/*users_controller.rb*' I have this code:
def reset
respond_to do |format|
format.html # reset.html.erb
end
end
def send_reset
...
end
In 'config/routes.rb' I have this code:
resources :users do
collection do
get 'reset'
get 'send_reset'
end
end
When I submit the form I get the error: "No route matches "/users/send_reset"" (browser URL becomes '.../users/send_reset'). What is wrong? How can I "map" URLs to Rails actions?
P.S.: I think the problem is in "config/routes.rb"...
the problem is here :method => :post and get 'send_reset', in my opinion you are trying to POST parameters when your conntroller expect GET method
You routes.rb declares the send_reset route as only available via get. You have to write post 'send_reset':
resources :users do
collection do
get 'reset'
post 'send_reset'
end
end

Resources