twice nested form path issue - ruby-on-rails

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

Related

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 %>

Update method "No route matches [POST]"

Having a bit of trouble with the following ruby on rails code snippet - I have books and would like to update a book's name, here is how I try to do it:
<h1>Edit the book "<%= #book.name %>"</h1>
<%= form_tag action: :update, id: #book do %>
<p>
<label for="book_name">Name</label>
<%= text_field :book, :name %>
<%= submit_tag 'Save changes' %>
</p>
<% end %>
This is what I do in the books controller:
def edit
#book = Book.find(params[:id])
end
def update
#book = Book.find(params[:id])
if #book.update_attributes(params[:book])
redirect_to :action => 'show', id => #book
else
#subjects = Subject.find(:all)
render :action => 'edit'
end
end
These are my routes:
root to: 'authors#index'
resources :books, :authors
When I click the submit button in the form, it gives me No route matches [POST] "/books/5" and directs to localhost:3000/books/5 instead of staying and localhost:3000/books/5/edit
What am I doing wrong here? Shouldn't there be a put method for updating stuff somewhere rather than a post method?
Updates should use put not post.
<%= form_tag( { :controller => :books, :action => :update, :id => #book.id }, { :method => :put } ) do %>
or better yet use form_for
<%= form_for #book do |f| %>
On your console run "rake routes" and it will print out all available routes.
Please try this:
We need to specify match in routes file.
match "/books/:id" => "books#update"
resources :books should do the job. you dont have to explicitly use "match".
def edit
#book = Book.find(params[:id])
end
form.html
form_for #book, :method => :put do |f|
def update
#book = Book.find(params[:id])
#book.update_attributes(params[:book])
end
this should do the job.
I had this issue before. Everything was right but still getting the error then I found out it was
gem 'rails-api'
Removed it and it all worked fine.

form_for method undefined

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)

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