Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: form for different controller

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?

like image 923
Slevin Avatar asked Oct 23 '12 10:10

Slevin


2 Answers

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| %>
like image 166
Dipak Panchal Avatar answered Nov 15 '22 01:11

Dipak Panchal


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.

like image 40
Tigraine Avatar answered Nov 15 '22 03:11

Tigraine