Rails - Routing Error for the POST method - ruby-on-rails

Rails newbie here (trying to get these questions answered)
I'm using Ryan Bates' Rails Cast on Wicked Wizard Forms to create a multi-step form. I'm getting a "No route matches [POST] "/user_steps/gender" (where gender is a view under the user_steps controller).
Any ideas?
routes.rb:
Store::Application.routes.draw do
resources :likes
resources :categories
resources :users
resources :user_steps
root to: 'static_pages#home'
user_steps controller:
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :gender, :items, :brands, :final
def show
render_wizard
end
def update
#user.attributes = params[:user]
render_wizard
end
end

When you define a new method, rails will default to a get request on that method. In order to make it a post method, try adding
match "user_steps/gender", to: "user_steps#gender", via: "post"
Check out this routes reference

I'm looking for rails wizards solutions and was going through "#346 Wizard Forms with Wicked" railscas when I ran into the same issue.
Adding a route, as #Klipfel suggests, is a good "rails" answer but not the correct approach for the wicked gem. If you add a route pointing to another controller method you are routing the request outside of the wicked framework.
I resolved this problem by specifying put for the http method. In the #346 Wizard Forms with Wicked railscast case it would look like this:
<%= form_for #user, url: wizard_path, method: :put do |f| %>
I'm not sure why it works in the railscast

Related

Why does Devise redirect sign_up error to a different page?

I created new rails project with just a generated controller for a home page to test this.
Me devise model is User, so the signup page is http://localhost:3000/users/sign_up.
If I trigger an error, for example, submitting the form without giving password, I get redirected to http://localhost:3000/users.
How can I stay on http://localhost:3000/users/sign_up after making errors?
I read this question and the only answer suggests using redirect_to :back in the controller.
The problem is I am not overriding the controller. If overriding is the only way to solve, please explain how it should be.
This is my routes.rb
Rails.application.routes.draw do
root to: "home#index"
get 'home/index'
devise_for :users
end
Here is the source code of Devise::RegistrationsController. How should I override new or create to achieve what I need?
I strongly recommend that you don't do this. The default behavior of Rails applications (following the route naming conventions) is that way you described. But, if you really need to to this, here's how.
First, go to your routes.rband:
devise_scope :user do
post 'users/sign_up', to: 'devise/registrations#create'
end
EDIT
Then, generate the devise views so you can override the default behavior of the forms.
rails g devise:views
Then, open your app/views/devise/registrations/new.html file and edit the form_for line so it looks like this:
<%= form_for(resource, as: resource_name, url: users_sign_up_path) do |f| %>

Rails routing error when routing to slug

I have a the following link that goes to the correct page i.e. /events/tech/schedule but I get a routing error.
<%= link_to "View Schedule", event_sessions_path(#event.slug) %>
Error
ActionController::RoutingError at /events/tech/schedule
Not Found
Routes
resources :events do
resources :sessions, path: "schedule", only: [:index]
end
Sessions Controller
before_filter :find_event
private
def find_event
#event = Event.find_by(slug: params[:id])
##event = Event.find(params[:event_id]) This works if I use <%= link_to event_sessions_path(#event.id) %>
end
I don't get any error if I use the event id to link to but this looks ugly and isnt a very good solution.
Edit - add rake routes
event_sessions GET /events/:event_id/schedule(.:format) sessions#index
Here's your solution:
#event = Event.find_by(slug: params[:event_id])
When using resourceful routes in the structure you have, you'll end up wtih this:
events/:event_id/schedule #-> to schedules#index with params[:event_id]
The name of the variable doesn't matter - it's the content which does. :event_id can hold anything (id or slug), what matters is how you deal with it in the controller
If you change your Event.find_by method to use the event_id param, you'll be able to find what you need
friendly_id
A better way to handle this is with friendly_id
If you use the finders module in friendly_id, you'll be able to search either id or slug like this:
Event.find params[:event_id]

Thumbs up gem routing error

I seek clarification on using the thumbs up gem with rails 4. I current have a user resource and a post resource and have set up thumbs up as follows.
add gem to gemfile and installed it using bundler.
Generated require migration
User model
class User < ActiveRecord::Base
acts_as_voter
end
Post model
class Post < ActiveRecord::Base
acts_as_voteable
end
Post controller
def vote_up
#post = Post.find(params[:id])
current_user.vote_for(#post)
respond_to do |format|
format.js
end
end
View
<%= link_to('vote for this post!', vote_up_post_path(#post) , :method => :post) %>
Route file
resources :posts do
member do
post :vote_up
end
end
However i keep getting this error
No route matches [POST] "/posts/vote_up"
And after running rake routes I can see that the following route is available to me:
vote_up_post POST /posts/:id/vote_up(.:format) posts#vote_up
any ideas what could be the cause of this error ?
Could you please show us your view.
Apparently, you are calling
/posts/vote_up
instead of
/posts/:id/vote_up
"vote_up" acts like a RESTful action, similar to "post/1/delete", "post/1/edit". So you need to add this custom RESTful action in route.
Change your route like this at first.
resources :posts do
member do
post 'vote_up'
end
end
Then, in your view, to use this path, add resource as arg
vote_up_post_path #post
Reference: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
Other people reading this notes should realise that post is not a singluar of his controller posts... but the post http verb
resources :contenders do
member do
post 'vote_up'
end
end
Not sure but in your view, instead of #post try giving #post.id to vote_up_post_path.
<%= link_to('vote for this post!', vote_up_post_path(#post.id) , :method => :post) %>

Rails: restful resource routing with action_controller_path

I am putting a randomize def in my controller and would like to access it with a restful route. The route should be accessed with the following:
<%= link_to "Randomize", random_reader_path %>
But I cannot figure out how to get this route to appear in rake routes or configure it correctly in my routes.rb file.
The random method will do the same thing as index only provide a random page content #variable
Currently I have my reader Controller as
resources :reader
in my routes.rb
Add more RESTful actions!
resources :reader do
get 'random', on: :collection
end
The route will be random_readers_path, though.

Rails form helper and RESTful routes

I have a form partial current setup like this to make new blog posts
<% form_for([#current_user, #post]) do |f| %>
This works great when editing a post, but when creating a new post I get the following error:
undefined method `user_posts_path' for #<ActionView::Base:0x6158104>
My routes are setup as follows:
map.resources :user do |user|
user.resources :post
end
Is there a better way to setup my partial to handle both new posts and editing current posts?
map.resources :user do |user|
user.resources :posts
end
pluralize your model names in routes declaration. as you can see it says resourc-es, so you must use user-s and post-s too.
Controllers should be named UsersController and PostsController
Models should be named User and Post.
if above example still does not work for you, try a this one
map.resources :users do |u|
u.resources :posts
end

Resources