sindhugauri
sindhugauri

Reputation: 189

Redirect button to view in Ruby on Rails

I have a login and a signup page. I want that after clicking on the login or signup button, the user is redirected to another view which will be a micropost.

The code for the signup page is as follows. (as in the form part) -

   <h2 class="text-center">Sign Up</h2>
                <%= form_for(input_output_SignUp_url) do |f| %> 
                <%= f.label :first_name,"First name:" %>
                <%= f.text_field :first_name %>

                <%= f.label :last_name,"Last name:" %>
                <%=f.text_field :last_name %>

                <%= f.label :email,"Email:" %>
                <%= f.email_field :email %>

                <%= f.label :phone,"Phone no:"%>
                <%= f.text_field :phone %>

                <%= f.label :city,"City:" %>
                <%= f.text_field :city %>

                <%= f.label :addr_1,"Address 1:" %>
                <%=f.text_field :addr_1 %>

                <%= f.label :addr_2,"Address 2:" %>
                <%= f.text_field :addr_2 %>

                <%= f.label :state,"State:"%>
                <%= f.text_field :state %>

                <%= f.label :postal_code,"Postal Code:"%>
                <%= f.text_field :postal_code %>

                <%= f.label :password,"Password:"%>
                <%= f.password_field :password %>

                <%= f.label :password_confirmation, "Confirmation:" %>
                <%= f.password_field :password_confirmation, class: 'form-control' %>

                <%= button_to "View profile", input_output_micropost_path%>
            <% end %>

Now this isn't working. When I click on the button, it shows an error- No route matches [POST] "/input_output/SignUp"

The routes file is as follows:-

   root 'static_pages#home'

   get 'static_pages/home'
   get 'static_pages/genre'
   get 'static_pages/accessories'
   get 'static_pages/contactus'
   get 'static_pages/aboutus'

   get 'input_output/Login'
   get 'input_output/SignUp'
   get 'input_output/micropost'

   get '/genre', to: 'static_pages#genre' 
   get '/accessories', to: 'static_pages#accessories'
   get '/aboutus', to: 'static_pages#aboutus'
   get 'contactus', to: 'static_pages#contactus'

   get 'Home', to: 'static_pages#home'
   get '/Login', to: 'input_output#Login' 
   get '/micropost', to: 'input_output#micropost'
   get '/SignUp', to: 'input_output#SignUp'
end

How can I make this error go away?

Edit:- As asked, this is the result of rake routes /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated

/home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated

/home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated

rake aborted!

ArgumentError: Missing :controller key on routes definition, please check your routes.

/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/config/routes.rb:25:in `block in '

/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/config/routes.rb:1:in `'

/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/config/environment.rb:5:in `'

Tasks: TOP => routes => environment

Upvotes: 1

Views: 2946

Answers (2)

MarioV
MarioV

Reputation: 48

The top part of your routes are wrong, the route must point to a controller and action inside the controller, so the routes should be something like:

   root to: 'static_pages#home'
   get '/genre', to: 'static_pages#genre' 
   get '/accessories', to: 'static_pages#accessories'
   get '/aboutus', to: 'static_pages#aboutus'
   get '/contactus', to: 'static_pages#contactus'

   get '/Home', to: 'static_pages#home'
   get '/Login', to: 'input_output#Login' 
   get '/micropost', to: 'input_output#micropost'
   get '/SignUp', to: 'input_output#SignUp'
   get '/Home', to: 'static_pages#home'
   get '/Login', to: 'input_output#Login' 
   get '/micropost', to: 'input_output#micropost'
  ##################################################
  #### Action for the form is set as post, so signup
  #### must be a post route
  ##################################################
   post '/SignUp', to: 'input_output#SignUp'

Upvotes: 1

Steve Carey
Steve Carey

Reputation: 3024

This is pretty simple. Instead of

<%= button_to "View profile", input_output_micropost_path%>

Make that a submit

<%= f.submit "Sign up", class: "btn btn-default" %>

Then in your controller in the create action add a redirect at the end. Something like this

def create
  @user = User.new(user_params)
  if @user.save
    redirect_to input_output_micropost_path, notice: 'User was successfully created.'
  else
    render :new
  end
end

Edit: You need RESTful routes for your user model. If you are putting that under the input_output namespace it would be something like this

resources :input_outputs do
  resources :users
end

I recommend the Rails Tutorial book by Michael Hardt. It shows how to do something like this in very good detail. It's a classic.

Upvotes: 0

Related Questions