So I have a Message model, and a ChatRoom model.
When I display a chat room, I use the show action on the ChatRoom controller. In the view for this action, there is a little form for the user to create a post, and submit that post to the chat room being shown.
When I run my tests, however, I get an error "no route matches [POST] /messages/an_id_of_some_sort". Specifically, in this little test:
post message_path, params: {message: {body: "yo ho ho and a bottle of rum!"}}
assert_redirected_to chat_room_path(#channel)
the error pops up in the post message_path.
The show method on the chat room controller looks like
def show
if(#user = current_user)
#chats = #user.chat_rooms
#chosen = ChatRoom.find_by(id: params[:id])
if(#chosen.messages.any?)
#messages = #chosen.messages
else
#messages = nil
end
#message = Message.new
end
end
Then the little form bit of the view is:
<div class="message-input">
<%= form_for(#message) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :body, placeholder: "Write Message..." %>
<%= f.hidden_field :room, :value => params[:room] %>
<%= button_tag(type: "submit", class: "message-submit-btn", name: "commit", value: "") do %>
<span class="glyphicon glyphicon-menu-right"></span>
<% end %>
<% end %>
</div>
I have a create action on the Messages Controller which does the saving to the database:
#message = current_user.messages.build(message_params);
#message.chat_room = params[:room]
if #message.save
redirect_to chat_room_path(#message.chat_room)
end
and routing-wise I have
Rails.application.routes.draw do
root 'welcome#welcome'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get 'users/signup_success'
delete '/chat_rooms/leave/:id', to: 'chat_rooms#leave', as: 'current'
get 'welcome/welcome'
resources :users
resources :account_activations, only: [:edit] #Only providing an Edit route for this resource.
resources :password_resets, only: [:new, :edit, :create, :update]
resources :chat_rooms, only: [:new, :create, :show, :index]
resources :messages, only: [:create, :edit, :destroy]
end
I have tried playing around with explicitly setting the :url on the form_for, but no dice. There is another question on this problem, but the solution there hasn't really helped.
I would greatly appreciate any help.
With this line you're running POST /messages/:id
post message_path, params: {message: {body: "yo ho ho and a bottle of rum!"}}
In your routes file you have this:
resources :messages, only: [:create, :edit, :destroy]
This will create the routes POST /messages, PUT/PATCH /messages/:id, and DELETE /messages/:id. You can verify this with rake routes.
None of these generated routes handle POST /messages/:id.
If you're trying to have the test create a new message, then you can use messages_path instead. message_path (with singular message) takes a message argument as a message, e.g. message_path(Message.first) and uses that to build the url.
EDIT: Thanks for the replies, I tried everything listed below but still now luck. If I type localhosts/posts/new it takes me to the form, however the link does not work when I click from the navigation bar. I've updated the code and included my rake routes results.
I am new to ruby and working through a tutorial, however one of my links is not working and not sure what is happening.
My navigation link to create a new post is not taking me to the correct page, when I click the link to the "posts_path" the page does not change.
I can create a new posts by typing in the /posts/new in the address bar, but when I click the "New Post" link on the nav bar the page does not update (although the url displays /posts). Any idea how to fix this?
config/routes.rb
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/search', to: 'static_pages#search'
get '/login', to: 'sessions#new'
get '/posts', to: 'posts#new', as: 'new_post'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
get 'users/new'
get 'static_pages/home'
get 'posts/new'
get 'sessions/new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:new, :create, :destroy]
end
app/views/layouts/_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<% link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Search", search_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Posts", posts_new_path %></li>
<li class="dropdown">
Account <b class="caret"></b>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li><%= link_to "Logout", logout_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
posts_controller.rb
class PostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#post = current_user.posts.build(post_params)
if #post.save
flash[:success] = "Post created!"
redirect_to root_url
else
#feed_items = []
render 'static_pages/home'
end
end
def destroy
#post.destroy
flash[:success] = "Post Deleted"
redirect_to request.referrer || root_url
end
def new
#post = current_user.posts.build if logged_in?
end
private
def post_params
params.require(:post).permit(:description, :picture)
end
def correct_user
#post = current_user.posts.find_by(id:params[:id])
redirect_to root_url if #post.nil?
end
end
rake routes:
password_resets_new GET /password_resets/new(.:format) password_resets#new
password_resets_edit GET /password_resets/edit(.:format) password_resets#edit
sessions_new GET /sessions/new(.:format) sessions#new
root GET / static_pages#home
search GET /search(.:format) static_pages#search
login GET /login(.:format) sessions#new
new_post GET /posts(.:format) posts#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
signup GET /signup(.:format) users#new
users_new GET /users/new(.:format) users#new
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_search GET /static_pages/search(.:format) static_pages#search
posts_new GET /posts/new(.:format) posts#new
GET /password_resets/new(.:format) password_resets#new
GET /password_resets/edit(.:format) password_resets#edit
GET /sessions/new(.:format) sessions#new
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
posts POST /posts(.:format) posts#create
GET /posts/new(.:format) posts#new
post DELETE /posts/:id(.:format) posts#destroy
Remove get 'sessions/new' & get '/posts', to: 'posts#new', as: 'new_post'
Restart server
and try link_to "Posts", new_post_path, method: :post
Should work
Rails scans the routes from top to bottom i.e. routes.rb. Whenever it finds the matching routes corresponding controller#action gets executed and remaining all will be skipped(later routes after match found).
In this case routes are conflicting, So do bellow changeseverything will work fine.
config/routes.rb
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/search', to: 'static_pages#search'
get '/login', to: 'sessions#new'
#get '/posts', to: 'posts#new', as: 'new_post' #not required as resources :posts will do the job in last line
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
get 'users/new'
get 'static_pages/home'
get 'posts/new', to: 'posts#new', as: 'posts_new_path' #Made changes here according to helper used in view 'posts_new_path'
get 'sessions/new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:new, :create, :destroy]
end
I would suggest that you remove get 'posts/new' & get '/posts', to: 'posts#new', as: 'new_post' as these are duplicate routes.
You already have the correct route created in the route resources :posts, only: [:new, :create, :destroy]. By including new, Rails is doing this portion for you. See here: http://guides.rubyonrails.org/v4.2/routing.html#crud-verbs-and-actions
Then you should be able to use the link that everyone else has posted.
<%= link_to "New Post", new_post_path %> based on the guide here: http://guides.rubyonrails.org/v4.2/routing.html#path-and-url-helpers
The problem is there is conflict occurring in your routes.
You have defined routes twice for new
get /posts', to: 'posts#new', as: 'new_post'
and
resources :posts, only: [:new, :create, :destroy]
now there are 2 paths for posts#new. I suggest u remove first one. Change your html to
<li><%= link_to 'New Post', posts_path %></li>
and it should work fine.
i went through your code...and this is what i recommend to add/edit changes..and sure it should work :).
for routes.rb,its looks good BUT
there is a duplicate url entry.So either remove get 'posts/new' or resources :posts, only: [:new, :create, :destroy] as they both will create a duplicate entry for posts#new.You can verify this by outputting your routes in seperate txt file by rake routes >> path.txt
in app/views/layouts/_header.html.erb,its good too and dont need any change.
in posts_controller.rb,there is a condition added for only logged in user,Kindly check if you are logged in else remove that condition and check again.
Finally,debug using F12 in chrome/firefox and see what log do you get in web browser console before and after click to check is there some javascript error or not.
code for link to new post
<%= link_to "New Post", new_post_path %>
Try new_post_path
Also, rake routes in terminal will list all the routes and associated helpers for you to double check against.
Finally, adding 'as' to: get '/posts', to: 'posts#new', as: 'new_post' in your routes file will allow you to define your own path name (in this case new_post_path, but you can set it as anything you'd like).
<%= link_to "Posts", posts_path %> corresponds to index action not to the new action.
Remove the only condition for your posts controller in routes.rb which will create all the basic routes needed rather than specific routes.
resources :posts
So, new_post_path returns /posts/new
Finally add a link link <%= link_to 'New Post', new_post_path %>
This may be helps to redirect,
in routes.rb,
resources :posts
It gives the paths to all default controller methods like index, new, create, edit, update, show, destroy. Then check with rake routes. Get the corresponding path like,
new_post GET /posts/new(.:format) posts#new
if it correct means give the url like,
<%= link_to 'New Post', new_post_path %>
It redirects to method new in controller and render corresponding html.erb page. This url formed by the routes.rb.
OR
push the redirection url manually.In html page,
<%= button_tag "New", :onclick => "getNew()" %>
function getNew(){
var form = document.forms[0];
// get the id and value with any variable //
form.method = "post";
// pass the value to parameters //
form.var name = column name; (description r picture)
form.action = "posts/new";
form.submit();
}
in routes.rb,
match '/posts/new' => 'posts#new', via: [:get, :post]
It makes the manual redirection url. It calls the new in controller method.
Let me know your feedback! Thanks
As per your rake routes output the following should work
<%= link_to "New Post", posts_new_path %>
Please let us know whether there any error comming in the console log on clicking the new post link.
The bottom line is that you're trying to do things in a non-Rails manner. Rails favors convention over configuration, meaning if you don't do things the Rails way, you're going to have a bad time.
This means you should clean up your routes until you have no unnecessary duplication. Right now you have 3 different routes that go to posts#new each with a different path prefix.
I'd remove:
get '/posts', to: 'posts#new', as: 'new_post'
get 'posts/new'
since resources :posts, only: [:new, :create, :destroy] already creates that route. Then your new_post_path helper should create the correct URL and your routes will have no ambiguity.
I am trying to come up with upvote/downvote method in my app and I running into a no method error.
Here is my voter form:
<div>
<div class= 'pull-left'>
<div><%= link_to " ", post_up_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
<div><strong><%= post.points %></strong></div>
<div><%= link_to " ", post_down_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
</div>
Here are my routes:
App::Application.routes.draw do
devise_for :users
resources :users
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post '/up-vote' => 'votes#post_up_vote', as: :up_vote
post '/down-vote' => 'votes#post_down_vote', as: :down_vote
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
And here is my partial call:
<%= render partial: 'votes/voter', locals: { post: post } %>
Now I don't think there is anything wrong with my partial call or my voter partial because everything works until I try to route it.
First, you will need to change up your routes a bit.
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post 'upvote', on: :member
post 'downvote', on: :member
end
The reason these are member routes instead of collection routes is being they apply to a specific member of the collection of posts...a single Post.
Then, you will want to run the command, rake routes in your Terminal to see what the appropriate path methods are. It should be something like
upvote_post_path(:id)
which requires an object be passed in..so in your view you would use it like you currently are.
upvote_post_path(post)
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