No route matches [POST] "/contacts/1" - ruby-on-rails-3

app/view/contact/show.html/erb
<%= form_for(#contact) do |f| %>
<p id="notice"><%= notice %></p>
<p>
<b>Firstname:</b>
<%= #contact.firstname %>
</p>
<p>
<b>Lastname:</b>
<%= #contact.lastname %>
</p>
<p>
<b>Email:</b>
<%= #contact.email %>
</p>
<p>
<b>Mobilephone:</b>
<%= #contact.mobilephone %>
</p>
<% end %>
<%= link_to 'Edit', edit_contact_path(#contact) %> |
<%= link_to 'List', contacts_path %>
in my view/contact/index.html.erb i have a button
<%= button_to 'show', contact %>
in my contacts_controller.rb i just use automatic setting like:
def show
#contact = Contact.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #contact }
end
end
in my routes file
match '/contacts/:id/edit', :controller => 'contacts', :action => 'edit'
match '/contacts/contact_:id/show', :controller => 'contacts', :action => 'show'
resources :contacts
resources :connections
resources :addresses
root :to => 'contacts#index'
and after running rake routes i got
/contacts/:id/edit(.:format) contacts#edit
/contacts/:id/show(.:format) contacts#show
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
connections GET /connections(.:format) connections#index
POST /connections(.:format) connections#create
new_connection GET /connections/new(.:format) connections#new
edit_connection GET /connections/:id/edit(.:format) connections#edit
connection GET /connections/:id(.:format) connections#show
PUT /connections/:id(.:format) connections#update
DELETE /connections/:id(.:format) connections#destroy
addresses GET /addresses(.:format) addresses#index
POST /addresses(.:format) addresses#create
new_address GET /addresses/new(.:format) addresses#new
edit_address GET /addresses/:id/edit(.:format) addresses#edit
address GET /addresses/:id(.:format) addresses#show
PUT /addresses/:id(.:format) addresses#update
DELETE /addresses/:id(.:format) addresses#destroy
root / contacts#index
And when i click the button 'show' i got Routes Error No route matches [POST] "/contacts/1" Could somebody help me checking what mistake i have maken,please? Thank you very much for helping.

Maybe it's easier if you use the standard routing offered by resources contacts - then you could simply use <%= link_to 'show', contact_path(contact) %>
Also, the button should perform a GET request, not a POST since that route is not defined.
If I were you I would remove the first two custom routes you declared and just rely on the resources :contacts method instead. This in combination with a GET request should fix your problem.

Related

Rails: No route matches [PUT] "/blog/2"

I am creating blog application in rails. I have a common form for creating and updating blog.
This is view of edit and new.html.erb
<%= render :partial => "form"%>
This is view of _form.html.erb blog:
<%= form_for #blog do |f| %>
<%= f.text_field :title, :placeholder => "Title" %><br>
<%= f.cktext_area :article, :placeholder => "Content", :ckeditor => {:toolbar => "MyToolbar"} %>
<%= f.submit %>
<% end %>
My blog is creating successfully but I am getting error on update action. This is my edit and update action in blog controller:
def edit
#blog = Blog.find_by_slug(params[:id])
end
def update
#blog = Blog.find(params[:id]) || not_found
#blog.update_attributes(params[:blog])
redirect_to "/blogs/#{#blog.slug}"
end
When I open form from edit view, and click on update button, it throws error:
No route matches [PUT] "/blog/2"
My routes.rb is:
resources :blogs
get 'blog', to: 'blogs#index'
get '/blog/:id', to: 'blogs#show', as: 'blog'
I am not getting where it is going wrong. I tried to add "url: blogs_path" in form_for, it removes the error but doesn't save the edit changes.
Can anybody help me where I am going wrong here?
Thank you.
Okay. I dont understand why you want to go against conventions. Anyway, using form_for resource would automatically generate action URL as a PUT to /resources/:id if its an update operation.
So to override this you need to do two things.
update your routes to support this:
Add this line to your routes file:
put 'blog/:id' => 'blogs#update', :as => 'update_blog'
It is important that you put this line above your 'resources :blogs` call.
2 . specify the URL to which the form should submit:
You will need to create the form tag like this:
<%= form_for #blog, :url => update_blog_path(#blog) do |f| %>
Try this and let us know.

Rails - how do I show an Add Favourite button to users who are not logged in?

I'd like to display an "Add to Favourites" button to users that are not logged in so that they can see that logged in users can add posts (in this case) to their favourites.
Here's the current code:
routes.rb
resources :users do
resources :favourites
end
resources :favourites, only: [:create, :destroy]
posts_helper.rb (I'll move this application_helper once I get it working)
PostsHelper
def new_favourite
if signed_in?
return current_user.favourites.build
else
return Favourite.new
end
end
end
show.html.erb
<%= render :partial => 'shared/favourites/favourite_form', :locals => { :object => #post } %>
_favourite_form.html.erb
<% if signed_in? && current_user.favourited?(object) %>
<%= render partial: 'shared/favourites/unfavourite', locals: { object: object } %>
<% else %>
<%= render partial: 'shared/favourites/favourite', locals: { object: object } %>
<% end %>
_favourite.html.erb
<%= form_for(new_favourite, remote: signed_in?) do |f| %>
<div>
<%= f.hidden_field :object_id, :value => object.id %>
<%= f.hidden_field :object_type, :value => object.class.name.demodulize %>
</div>
<%= button_tag(:type => 'submit', :id => 'add_favourite') do %>
Add to Favourites
<% end %>
<% end %>
I want it to not do an ajax call (ie submit the add favourite form using a page reload) if the user is not signed in so that it will see the user is trying to access a protected page, will redirect them to login and upon success [when it redirects them back to where they were originally trying to go] it will add the favourite.
Can someone advise what I would need to change to make this possible.
EDIT:
Code updated to reflect current state.
After signing in it redirects back to No route matches [GET] "/favourites" because favourites are a nested resource under users.
EDIT 2:
The redirect works pretty much the same as in Rails Tutorial:
Redirect user to sign in page (signed_in_user):
http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#code-correct_user_before_filter
Store location to redirect user back to:
http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#code-friendly_forwarding_code
http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#code-friendly_session_create
The only difference is that on my signin page I use a facebook omniauth link for the user to login but the redirection after logging in still works as expected.
Use a helper method instead of current_user.favourites.build?
<%= form_for(new_favourite, remote: true) do |f| %>
Helper:
PostsHelper
def new_favourite
if signed_in?
return current_user.favourites.build
else
return Favourite.new
end
end
end

Rails 3 - form_for pluralization causing path/method error on "new" action but not on "edit"

When I go to the path: /genre/new in my application I get this error:
myapp/app/views/genre/_form.html.erb where line #1 raised:
undefined method `genres_path' for #<#<Class:0x007fdcb39edcb0>:0x007fdcb39e8080>
However when I go to /genre/:id/edit the _form.html.erb file renders without error and the record is updated with no problems.
My new.html.erb and edit.html.erb files call <%= render 'form' %> and my _form.html.erb file has:
<%= form_for(#genre) do |f| %>
<%= f.label :title %> <br /> <%= f.text_field :title %>
<%= f.label :desc %> <br /> <%= f.text_field :desc %>
<%= f.submit %>
<% end %>
In genre_controller.rb my 'new' and 'edit' actions are as follows:
def new
#genre = Genre.new
current_user.authorize! :create, #genre # cancan authorization
respond_to do |format|
format.html # new.html.erb
format.json { render json: #genre }
end
end
def edit
#genre = Genre.find(params[:id])
current_user.authorize! :update, #genre # cancan authorization
end
I've run a search in my codebase for the string "genres" and the only place it occurs is in the logs, so I'm sure this is not a typo in my code.
My guess is that Rails routing system correctly pluralizes "genre" to "genre", but form_for (or a dependency) is creating the pluralization "genres", but only when the parameter passed to it is empty or "new".
Given the error is around 'genres_path', I tried various combinations of the following in my routes.rb file, but they didn't solve the problem:
match "/genres" => "genre#index", :as => :genre
match "/genres/:id(.:format)" => "genre#show", :as => :genre
match "/genre" => "genre#index", :as => :genres
match "/genre/:id(.:format)" => "genre#show", :as => :genres
Any thoughts on how I can work around this?
EDIT: Here are the routes generated by the resources :genre statement in my routes.rb file:
genre_index GET /genre(.:format) {:action=>"index", :controller=>"genre"}
POST /genre(.:format) {:action=>"create", :controller=>"genre"}
new_genre GET /genre/new(.:format) {:action=>"new", :controller=>"genre"}
edit_genre GET /genre/:id/edit(.:format) {:action=>"edit", :controller=>"genre"}
genre GET /genre/:id(.:format) {:action=>"show", :controller=>"genre"}
PUT /genre/:id(.:format) {:action=>"update", :controller=>"genre"}
DELETE /genre/:id(.:format) {:action=>"destroy", :controller=>"genre"}
on new.html.erb try
<%= form_for(#genre, :url => genre_path, :method => :post) do |f| %>
assuming you have your route setup as a resource - resources :genre
also this will not work on edit.html.erb
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Update:
this is the one we are interested in
POST /genre(.:format) {:action=>"create", :controller=>"genre"}
try this
<%= form_for(#genre, :url => {:action=>"create", :controller=>"genre"}, :method => :post) do |f| %>

Rails appends id to singular route when render edit after errors

I have the following singular route:
scope '/seller' do
resource :seller_profile, :path => "/profile", :only => [:show, :edit, :update]
end
and the following controller:
class SellerProfilesController < ApplicationController
before_filter :validate_user_as_seller
def show
#seller_profile = current_user.seller_profile
end
def edit
#seller_profile = current_user.seller_profile
end
def update
#seller_profile = current_user.seller_profile
if #seller_profile.update_attributes(params[:seller_profile])
redirect_to(seller_profile_path, :notice => 'Profile was successfully updated.')
else
render :action => "edit"
end
end
end
I use a singular route given that the user must be authenticated before gaining access to the controller and therefore I can get the seller_profile from the user logged in.
This works like a charm, with only one problem. When I edit the seller_profile and validation error happen, the form is edited again and the errors are displayed correctly. The problem is that rails appends to the url the id of the edited record. For instance,
when I first edit the record, the url is:
http://0.0.0.0:3000/seller/profile/edit
but if the form is submitted with validation errors, the form itself is redisplayed under
http://0.0.0.0:3000/seller/profile.2
where 2 is the ID of the record being edited.
The form is the following:
<%= simple_form_for #seller_profile do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>
Everything, as said, works great but I would totally mask the ID in the url. What should I do?
I have not really worked too much with simple_form_for. But it looks like it is guessing your url always as if they were not single resources. You can provide a custom one:
<%= simple_form_for #seller_profile, :url => seller_profile_path do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>

Routing error while using partial form in rails 3

does anyone know how to use a form partial to create and update data about an object? My update method seems to work, but I can't create a new object. Every time I clink on the 'Add new ad' I get this error:[ActionController:Routing Error in Classified#new No route matches {:controller=>classified}]. Here is the code for the partial form: The error points to the first line:
<%= form_for(#classified) do |f| %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :price %><br/>
<%= f.text_field :price %>
</p>
<p>
<%= f.label :location %><br/>
<%= f.text_field :location %>
</p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
<%= f.label :email %><br/>
<%= f.text_field :email %>
<%= f.submit %>
<% end %>
<%= link_to 'Back', {:action => 'list'} %>
Here are my methods for new, edit and update in the Classified controller class:
def new
#classified=Classified.new
end
Here is the 'def create' method:
def create
#classified = Classified.new(params[:classified])
if #classified.save
redirect_to :action => 'list'
else
render :action => 'new'
end
end
I suspect the problem is in my config/routes.rb. I already have this line:
resources :classified
I have also put:
root :to => "Classified#list"
root :to => "Classified#new"
root :to => "Classified#show"
root :to => "Classified#edit"
root :to => "Classified#form
Could the problem be with the routes.rb file. And how comes it works with the update method and not the new method? Please help. I have tried all possible tricks to no avail. I will be so glad. Thanks in advance
I would like to know the result of your rake routes, because you use the singular for your controller classified.
Could it be classifieds, with the S in all your routes? I'm not that sure because resources: classified I guess came from the scaffold and so it should be good, then you miss a `"' on your last line but this could be a typo.
I would recommend having a plural name for the resource, as that is the norm for most resources:
resources :classifieds
You will also need to change your controller name and class name to classifieds_controller and ClassifiedsController
Also, your named routes should be lowercase (although you should get rid of these routes altogether):
root :to => "classifieds#list"
Get rid of the root routes. The resources line will create all the routes you need. And you are only supposed to have one root route in routes.rb and that should point to your home page controller#action.
If you have an action for form, then you don't need that either.. just mentioning this because you created a route for "Classified#form". Controller actions are not necessary for partials.
Your new and create methods and the form look ok at first glance. Try reworking your routes first and if you are still having problems, run rake routes from the command line and post the output in your question and leave me a comment on this answer and I will see if I can help you figure it out.
Read this first:
http://guides.rubyonrails.org/routing.html