New to Ruby here.
I keep getting: No route matches {:controller=>"home", :action=>"search"}
I have a simple form in my index view:
<%= form_tag(search_path) do %>
<%= text_field(:search, nil, :placeholder => "yada yada") %>
<%= submit_tag("Search") %>
<% end %>
And I have these routes:
root to: "home#index"
match 'search/:term', to: 'home#search', as: :search, via: [:post]
And the controller: home_controller.rb
class HomeController < ApplicationController
def index
end
def search
render 'index'
end
end
I guess something is wrong with my rout where I try to match 'search/:term', but I can't figure out what.
Your route definition states that you must have a "term" as part of the URL, ie:
http://example.com/search/some+term
Your form is POSTing to the /search patch - but is not providing a "term"
Make the term optional
match 'search(/:term)', to: 'home#search', as: :search, via: [:post]
or remove it all together (you're not referencing it in your form)
match 'search', to: 'home#search', as: :search, via: [:post]
and your problem should go away.
Related
I have an issue with my add_item method and have trouble to understand why.
Here is my carts_controller.rb
class CartsController < ApplicationController
def index
#cart_items = CartItem.all
end
def add_item
#cart_item = CartItem.new
produit_id = params[:produit_id]
#cart_item = CartItem.find_or_create_by(params[:produit][:produit_id])
#cart_item.save
binding.pry
end
end
Here is produits/index.html.erb (where the issue comes from)
<div id="produits-column-container">
<% if #produits %>
<% #produits.in_groups_of(4, false).each do |g| %>
<% g.each do |produit| %>
<div id="produits-row-container">
<div id="fiche-produit-container">
<div id="produit-img">
<%= image_tag produit.photo %>
</div>
<div id="produit-nom">
<%= produit.nom %>
</div>
<div id="produit-prix">
<%= number_to_currency(produit.prix, unit: '€', format: "%n%u") %>
</div>
<div id="produit-au-panier">
<%= image_tag('icon/icon-panier') %>
<%= link_to 'Ajouter au panier', carts_add_item_path, method: :post %>
</div>
</div>
</div>
<% end %>
<% end %>
<% end %>
</div>
The error i'm given is :
ArgumentError in CartsController#add_item
wrong number of arguments (given 0, expected 1)
in def add_item(produit_id)
add_item(produit_id) is related to carts_add_item_path
I also give you the routes :
Rails.application.routes.draw do
match "/mon-panier" => 'carts#index', via: :get
post 'carts/add_item' => 'carts#add_item'
resources :categories do
resources :produits
end
resources :order_abonnements, only: [:create, :update, :delete]
get 'livraisons_type/index'
match "/recapitulatif" => 'recapitulatif#index', via: :get
match "/confirmation-carte-cadeau" => 'recapitulatif#confirmation', via: :get
match "/livraison-carte-cadeau" => 'livraison_carte#index', via: :get
match '/activation-carte' => 'code_carte_cadeau#index', via: :get
match "/offrir-une-box-bretonne" => 'cadeau#index', via: :get
resources :order_items, only: [:create, :update, :destroy]
match "/nos-box" => 'nos_box#index', via: :get
get 'categories/index'
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
match '/informations-penn-ar-box' => 'informations_penn_ar_box#index', via: :get
match '/livraison-box-bretonne' => 'livraison_box_bretonne#index', via: :get
match '/abonnements' => 'abonnements#index', via: :get
devise_for :users, path: '', path_names: { sign_in: 'connexion', sign_out: 'déconnexion'}
resources :users do
delete 'déconnexion' => 'devise/sessions#destroy'
end
match '/mon-marche-breton' => 'marche_breton#index', via: :get
root 'home#home'
end
And the logs :
Started POST "/carts/add_item" for ::1 at 2017-05-30 09:48:52 +0200
Processing by CartsController#add_item as HTML
Parameters: {"authenticity_token"=>"QrToQUHVxjuV5cUvZYHd7tj457htfZohOkmsvNDnKv79P413xjsSfR/8RVXtdIU7/wcmhcxjkU85N13CqJkG2w=="}
Cart Load (0.3ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 1 LIMIT 1
Completed 500 Internal Server Error in 27ms (ActiveRecord: 14.9ms)
ArgumentError (wrong number of arguments (given 0, expected 1)):
app/controllers/carts_controller.rb:6:in `add_item'
You need to pass produit_id as a parameter.
So change this line as so...
<%= link_to 'Ajouter au panier',
carts_add_item_path(produit_id: produit.id), method: :post %>
And change your controller method as so...
def add_item
produit_id = params[:produit_id]
...
And change the find_or_create to
#cart_item = CartItem.find_or_create_by(produit_id: produit_id)
This does mean that you can only have one CartItem in your entire application that points to a product... strange design.
There are a lot of things that are wrong with this code, but the simplest thing you could do in order to suppress the error is to change the method signature to:
def add_item
instead of
def add_item(produit_id)
P.S. The controller is not RESTful. It's called CartsController, but in your index action you list the CartItems, not the Carts. The correct name should be CartItemsController. If you rename the controller to CartItemsController, than the index action can stay the same, but the add_item action should better be renamed to simply create. Thus, in your routes you can have:
resources :cart_items, only: [:index, :create]
Also, I am not exactly sure, what is going on in the add_item method - at first you assign a new CartItem to #cart_item, but then you override this assignment with find_or_create_by.... Also, the call to save at the end of the method is redundant, as create will save the record and otherwise no modifications have been made between the find_or_create_by line and the save line.
I have my routes defined as below
get 'contacts/new', to: 'contacts#new'
And in my ContactsController I have defined as below
def new
#contact = Contact.new
end
In views contacts/new/_form.html.erb I have structured form as below
<%= form_for #contact, html: {multipart:true} do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<% end %>
But when i go to localhost:3000/contacts/new
I get the below error.
undefined method contacts_path which is occuring in first line of form.
But when i try to define as below in routes, it worked
get 'contacts/new', to: 'contacts#new', as: 'contact'
Any idea why rails throws such error when i have defined it in the routes file. I am just trying to understand inner workings of rails routes
To avoid this kind of errors, remove your route and use:
resources :contacts, only: [:new, :create]
Try better use railsy way like resources as #Graham mentioned.
or
get 'contacts', to: 'contacts#index', as: :contacts #genetares: contacts_path
get 'contacts/new', to: 'contacts#new', as: :new_contact #new_contact_path
Make a post type route for contacts (for this it is throwing error)
Or remove this route
get 'contacts/new', to: 'contacts#new'
And add this simply
resources :contacts
My error says that:
Couldn't find Question with 'id'=your_questions"
and
ActiveRecord::RecordNotFound in QuestionsController#show
What should I do to fix it?
def show
#question = Question.find(params[:id])
#answer = Answer.new
end
on the second line it says where the error is.
Edit:
The Index View File
<%= form_for(#question) do |f| %>
<%= render 'common/form_errors', object: #question %>
<p>
<%= f.label :body, "Question" %><br />
<%= f.text_field :body %>
<%= f.submit "Ask a Question" %>
</p>
<% end %>
Rails.application.routes.draw do
get "/" => "main_app#index"
get "/location" => "location#location"
post "/location/index" => "location#index"
get "/location/index" => "location#index"
get "/location/directions" => "location#directions"
root to: 'questions#index'
get '/logout', to: 'sessions#destroy', via: :delete
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create]
resources :questions, except: [:new] do
resources :answers, only: [:create]
end
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
get '/logout', to: 'sessions#destroy', via: :delete
get '/questions/:id', to: 'questions#your_questions'
get '/search', to: 'questions#search'
You mention you have this route:
get '/questions/your_questions', to: 'questions#your_questions
If you also have a route like following the restful style, you should also have something like:
get 'questions/:id', to: 'questions#your_questions'
Or a resource call. Anyway, so Rails is actually trying to access your show action passing "your_questions" as the id for the route. Write this route like this:
get '/questions/:id', to: 'questions#show
This means: "If a request using the GET HTTP method follows the url 'questions/:id', then go to controller: questions and its action(method in the controller) called: your_questions" and pass into the params hash the value of id as in the URL.
I'm making a game in Ruby on Rails as a school project but now i'm stuck with this error:
undefined method `storylines_index_path' for #<#:0x007ff34cca8f68>
I'm making an page in which you I wou like to have a form to add an storyline, so I need a form with some fiels in it. I'd like to use the form_for method. But when adding I get this error
Here is my code:
views/new.html.erb
<% provide(:title, 'Overzicht Storylines') %>
<h1>Voeg nieuwe storyline toe</h1>
<%= form_for(#storyline) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :title %>
<%= f.text_field :title%>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
storylines_controller.rb
class StorylinesController < ApplicationController def index
#storylines = Storylines.find(:all) end
def show
#storyline = Storylines.find(params[:id])
end
def new
#storyline = Storylines.new end end
storylines.rb
class Storylines < ActiveRecord::Base
attr_accessible :title, :text
end
routes.rb
StoryLine::Application.routes.draw do
get "users/new"
get "storylines/new"
resources :users
resources :storylines
resources :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
match '/about', to: 'static_pages#contact'
match '/home', to: 'static_pages#home'
end
Rails conventions require that you name your model in a singular form, i.e., Storyline not Storylines. Renaming your model name in the class definition and the controllers should fix this.
When you do
form_for(#storyline)
It will try to look for the storylines_index_path, in order to create a Storyline object into your database.
So you need to define the route on the file config/routes.rb, if you already defined resources :storylines that should define the route, if you don't want to create a REST resource, you can create your own route
match 'storylines/create', to: 'storylines#create', as: :storylines_create
and then on the view
I advise to read the Rails Routing Guide since it explains much better everything related routing on rails
class UsersController < ApplicationController
def edit
...
end
Mu routes.rb
match '/user/:id/profile/edit', :to => 'users#edit', :as => "user_profile_edit", :via => "get"
My link:
<%= link_to image_tag("icons/run.png"), user_profile_edit_path %>
Exception:
No route matches {:controller=>"users", :action=>"edit"}
What I'm doing wrong?
Thanks.
You need to supply the user record.
Assuming the record is assigned to #user
<%= link_to image_tag("icons/run.png"), user_profile_edit_path(#user) %>