15

I'm developing a rails app with a landingpage. On the landingpage, the user can sign up for the app. For login, there is an extra view with an extra controller.

It looks like this:

views/landinpage/index.html --> sign up form
views/login/index.html --> login form

but I only want to have one controller

controllers/login_controller --> create new user from sign up form & check login data

so I have to get a connection between the landingpage view and the login_controller.

This is my attempt:

<%= form_for @login, :url => { :controller => "login_controller", :action => "create" }, :html => {:method => :post} do |f| %>

but it throws a route error:

No route matches {:controller=>"login_controller", :action=>"create"}

I already defined login resources in routes.rb, but it seems that the problem is elsewhere?

resources :logins

any ideas?

4 Answers 4

18

try this

class LoginsController < ApplicationController
   def new
     ...
   end

   def create
     ...
   end
  ...
 end

in your route.rb file write

  match '/login/create' => 'logins#create', :as => :create_login
  or
  resources :logins

in your console - write - rake routes and check your routes

then

<%= form_for @login, :url => create_login_path(@login) do |f| %>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot! Works perfectly. But i have a question left: I have to create a /login/create view. Can i define a redirect to the landingpage after the user submitted the form by changing the routes?
after submit form you want to display a create action view page?
Nope. The user should submit the form on the landingpage. After submitting, the user should not redirect to the "login/create" view but to the landingpage.
ok. in your create action you can define redirect_to landingpage_path. and define routes for landingpage like -- match '/landingpage' => 'controller-name#action name', :as => :landingpage
if you don't like messy routes file check @Tigraine's answer below
|
13

I think your code should look like this:

<%= form_for @login, :url => { :controller => "login", :action => "create" }, :html => {:method => :post} do |f| %>

can't test this right now, but I believe the _controller part is not required.

Update:

Another thing that I'm using a lot and that works:

<%= form_for @login, :url => create_login_path(@login), :html => {:method => :post} do |f| %>

You may have to fix the create_login_path part to match your application's routes but that's how I usually define these views.

1 Comment

unfortunately, it throws: No route matches {:controller=>"login", :action=>"create"}
0

Try this

class LoginsController < ApplicationController
   def new
     ...
   end

   def create
     ...
   end
  ...
 end

in your routes.rb file

resources :logins do
    collection do
      post :create
    end
  end

and in your views

<%= form_for @login, :url => create_login_path(@login) do |f| %>>

Comments

0

you can see the html form action part, you can see!

your config/routes has

resources :posts

namespace :admin do
  resources :posts
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.