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
Related
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
I receive an error saying that it has an
undefined local variable or method 'your_questions_path'
My routes.rb file:
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'
resources :questions do
collection do
get :your_questions
end
end
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/your_questions', to: 'questions#your_questions' original
get '/questions/:id', to: 'questions#show'
get 'search', to: 'controller#action', as: :search
My view file with the questions path a.k.a app/views/layouts/application.html.erb
<div id="nav">
<ul>
<li>
<%= link_to 'Home', root_path %>
</li>
<% if logged_in? %>
<li>
<%= link_to "Your Q's", your_questions_path %>
</li>
<li>
<%= link_to "Logout (#{current_user.username})", logout_path, method: 'get' %>
</li>
<% else %>
<li>
<%= link_to 'Register', register_path %>
</li>
<li>
<%= link_to 'Login', login_path %>
</li>
I want the questions path to go to http://localhost:3000/questions/1
Thank you in advance!
The routes you've defined create the following routes:
your_questions_questions GET /questions/your_questions(.:format) questions#your_questions
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
The top route is the your_questions route, which means you want use:
your_questions_questions_path
this is happening because you have nested your path inside of a resources block, ie. you have this:
resources :questions do
collection do
get :your_questions
end
end
if you were to get to this path, (run rake routes) you would need to use questions_your_questions_path, however since you want to route to something like http://localhost:3000/quesitons/1 this is a simple 'show' path that is provided to you by the resources command, instead change your routes file to simply say:
resources :questions
and then use question_path(#question) where #question is an instance variable set in the controller.
However... also looking from your code, you seem to be wanting the questions_path instead since that would provide the index action (a listing of questions). You can then in your controller scope the collection of questions to the ones the current user sees...
ie.
QuestionsController < ApplicationController
def index
#questions = current_user.questions
end
(assuming your User has_many :questions)
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.
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.
I'm following the http://ruby.railstutorial.org/
and I'm in chapter10 --build the microposts
but I named the_microposts not microposts
and then I had this problem:
When I visit static_page/home , it shows:
undefined method `the_microposts_path' for #<#:0xb628402c>
and the application trace is:
app/views/shared/_the_micropost_form.html.erb:1:in _app_views_shared__the_micropost_form_html_erb___1052260201__619399208'
app/views/static_pages/home.html.erb:9:in_app_views_static_pages_home_html_erb_733532872_628014758'
It seems that I didn't defined the method?
But I had defined it in my static_pages controller:
def home
#the_micropost = current_user.the_microposts.build if signed_in?
end
My home page:
<section>
<%= render 'shared/the_micropost_form' %>
</section>
and my _the_micropost_form.html.erb is:
<%= form_for(#the_micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
my user models:
has_many :the_microposts, dependent: :destroy
the_micropost models:
belongs_to :user
My routes:
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
is anything wrong??
I always follow the steps, is it because I defined the different name?(the_microposts as microposts)
You need to define route for Micropost resource just like you have defined for users and sessions. Update your config/routes to include resource :microposts.
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts
root 'static_pages#home'
...