I am receiving a no route matches error from the line <%= link_to "Ask User Out", askout_user_message_path(#user), :class => "button" %>.
This used to work before I added a gem but now it stopped working. I tried moving under collection but I get no luck with that as that's where it used to be.
Routes:
resources :users do |user|
resources :messages do
member do
post :new
get 'askout', action: 'askout'
end
end
collection do
get :trashbin
post :empty_trash
end
end
resources :conversations do
member do
post :reply
post :trash
post :untrash
end
end
Old Routes:
resources :users do |user|
resources :messages do
collection do
post 'delete_multiple'
get 'askout', action: 'askout'
get 'reply', action: 'reply'
end
end
end
My routes changed as I added mailboxer gem.
You'd be better doing this:
#config/routes.rb
resources :users do
resources :messages do
member do
post :new
get :askout
end
end
collection do
get :trashbin
post :empty_trash
end
end
This will give you:
users/1/messages/5/askout
What I think you want:
#config/routes.rb
resources :users do
resources :messages do
post :new
collection do
get :askout
end
end
collection do
get :trashbin
post :empty_trash
end
end
This will give you:
users/2/messages/askout
The path helper will be as determined in the rake routes view -- you should look at it to get an idea as to what your route is called (allowing you to write it accordingly)
Related
I get this error when I'm running a test that simply does get :index. Here is the test code:
class SubmissionsControllerTest < ActionController::TestCase
setup do
#submission = submissions(:one)
end
test "should get index" do
get :index
assert_response :redirect
# assert_not_nil assigns(:submissions)
end
I have a route set up for this controller though in my routes.rb file:
resources :courses do
resources :assignments do
get 'export', :controller => 'assignments', :action => 'export'
resources :memberships
resources :submissions do
resources :evaluations do
delete 'destroy', controller: 'reviews', action: 'destroy'
end
end
resources :questions do
resources :responses
resources :scales
end
resources :reviews do
get :assign_reviews, :on => :collection
post :edit_review, :on => :collection
end
end
end
Also, this is what I get for submissions routes when I run rake routes:
course_assignment_submissions GET /courses/:course_id/assignments/:assignment_id/submissions(.:format) submissions#index
POST /courses/:course_id/assignments/:assignment_id/submissions(.:format) submissions#create
new_course_assignment_submission GET /courses/:course_id/assignments/:assignment_id/submissions/new(.:format) submissions#new
edit_course_assignment_submission GET /courses/:course_id/assignments/:assignment_id/submissions/:id/edit(.:format) submissions#edit
course_assignment_submission GET /courses/:course_id/assignments/:assignment_id/submissions/:id(.:format) submissions#show
PUT /courses/:course_id/assignments/:assignment_id/submissions/:id(.:format) submissions#update
DELETE /courses/:course_id/assignments/:assignment_id/submissions/:id(.:format) submissions#destroy
And here is my code for the index action:
def index
if !current_user.instructor?(#course)
raise CanCan::AccessDenied.new("Not authorized!")
end
registrations = #course.registrations
#students = registrations.select{|r| !r.user.pseudo or #assignment.memberships.any?{|m| m.pseudo_user_id == r.user_id } }.map{|r| r.user }
respond_to do |format|
format.html # index.html.erb
format.json { render json: #students }
end
end
Does anyone know where this error could be coming from then?
Because your submissions controller is nested into assignments and assignments into courses you have to pass an :assignment_id and a :course_id into your get request.
So for example:
get :index, course_id: your_course, assignment_id: your_assignment
On the home page:
sessions/_goal.html.erb
<%= simple_form_for(:session, url: new_goal_path) do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
# ERROR MESSAGE:
Routing Error
No route matches [POST] "/goals/new"
Once the user submit's that he should be redirected to:
sessions/_habit.html.erb
<%= simple_form_for(:session, url: new_habit_path) do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
Once the user submit's that he should be redirected to:
<%= link_to "Sign Up via Facebook", "/auth/facebook" %> or
<%= link_to "Sign Up via Email", signup_path %>
The information they put in those two partials should then be stored so that when they are signed in they will see it as part of their profile.
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def goal
end
def facebook
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
if user.activated?
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_to root_url
else
message = "Account not activated. "
message += "Check your email for the activation link."
flash[:warning] = message
redirect_to root_url
end
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
routes
Rails.application.routes.draw do
put '/mark_completed/:id', to: 'habits#mark_completed', as: 'mark_completed'
put '/mark_accomplished/:id', to: 'goals#mark_accomplished', as: 'mark_accomplished'
get 'notes/index'
get 'notes/new'
get 'notifications/index'
get 'auth/:provider/callback', to: 'sessions#facebook'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
get 'password_resets/new'
get 'password_resets/edit'
get "/users/:user_id/goals", to: "goals#user_goals", as: "user_goals"
shallow do
resources :habits do
resources :comments
resources :notes
resources :notifications
end
resources :valuations do
resources :comments
resources :notes
resources :notifications
end
resources :goals do
resources :comments
resources :notes
resources :notifications
end
resources :stats do
resources :comments
resources :notes
resources :notifications
end
end
resources :notes
resources :habits do
collection { post :sort }
resources :notes
resources :notifications
resources :comments do
resources :likes
end
resources :likes
member do
post :like
post :notifications
end
resources :levels do
# we'll use this route to increment and decrement the missed days
resources :days_missed, only: [:create, :destroy]
end
end
resources :goals do
resources :notes
resources :comments
member do
post :like
end
end
resources :valuations do
resources :notes
resources :comments
resources :notifications
member do
post :like
post :notifications
end
end
resources :stats do
resources :notes
resources :comments
resources :notifications
member do
post :like
end
end
resources :results
resources :users
resources :account_activations, only: [:edit]
resources :activities do
resources :valuations
resources :habits
resources :stats
resources :goals
end
resources :notifications do
resources :valuations
resources :habits
resources :stats
resources :goals
resources :comments
end
resources :comments do
resources :comments
resources :notifications
member do
post :like
end
end
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
get 'tags/:tag', to: 'pages#home', as: :tag
resources :users do
member do
get :following, :followers
end
end
get 'about' => 'pages#about'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
root 'pages#home'
I have no idea what I'm doing here. Any help would greatly be appreciated :]
That will take some work.
First, fix your routes (post them here as you was rightfully asked).
Next, change your partials into action templates (remove underscore _ from their names).
Then in your controller actions use params[...] to retrieve data (e.g. params[:session][:name] for the first step) and save them in session (session[:name] = params[:session][:name]).
At the end of action call redirect_to :another_action (change :another_action to the concrete action you want next – for example in goal method that would be redirect_to :habit_url – and you should add that habit method to your controller).
Then, in your facebook-related methods you will have session[:name] and other stuff available, so you just fetch it from there and push into database (user.update(name: session[:name])).
Reading material:
Action Controller Overview – you need to attentively read this first of all
Active Record Basics – how to save user details to database
Rails Routing from the Outside In – just look through that, paying attention to places which are relevant to your current setup
I would like to create New Task button which will redirect to /users/:user_id/tasks/new
Routes:
devise_for :users
resources :users do
resources :tasks, shallow: true
end
Task Controller:
def new
#task = Task.new
end
View:
<%= link_to "New Task", new_user_task_path(#user) %>
but it gives me - No route matches {:action=>"new", :controller=>"tasks", :user_id=>nil} missing required keys: [:user_id] error.
Instead of having new_user_task_path(#user) you would like to have probably new_user_task_path(current_user). #user is nil in this case.
Hello im doing tutorial from Rails Book and get into some troubles.
While im trying to log in with my username and password im getting the following error:
Routing Error
No route matches [POST] "/sessions/new"
Try running rake routes for more information on available routes.
This is my route file config.
ZomfgShop::Application.routes.draw do
get "admin/index"
get "sessions/new"
get "sessions/create"
get "sessions/destroy"
resources :users
resources :orders
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
resources :products
root to: 'store#index', as: 'store'
resources :line_items do
#member do
# post 'decrement'
#end
post 'decrement', on: :member
end
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
You have controller instead of resources for the :sessions routes.
resources :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
Try replacing
get "sessions/new"
get "sessions/create"
get "sessions/destroy"
with
resources :sessions
make sure you have a views/sessions/new.html.erb file (under views/devise/ if using devise)
and make sure you have this in sessions_controller.rb if you aren't using Devise
def new
end
def create
end
def destroy
end
Thank you guys, changed my routes.rb file to this and my routing fine is working fine once again!
ZomfgShop::Application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
scope '(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
resources :line_items do
#member do
# post 'decrement'
#end
post 'decrement', on: :member
end
In Ruby on Rails, is there a way to add another RESTful action to the base URL of a plural resource? I'm looking for something like this:
resources :groups do
resources :users do
put on: :base, to: 'users#update_all'
end
end
Which would generate the route: [PUT] groups/:group_id/users => users#update_all
I've already tried doing this:
resources :groups do
resources :users
put 'users', on: :member, to: 'users#update_all'
end
But that doesn't preserve the value of params[:group_id] in the controller.
resources :users do
collection do
put '' => 'users#update_all' ## PUT /users
end
end
UPDATE
It would be recommended to do this though:
resources :users do
collection do
put 'update_all' ## PUT /users/update_all
end
end
Both route to the update_all action of the users controller.
RESOURCES
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions