Rails: form for different controller - ruby-on-rails

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?

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| %>

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.

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| %>>

you can see the html form action part, you can see!
your config/routes has
resources :posts
namespace :admin do
resources :posts
end

Related

Rails route error switch local

I'm setting my new Rails website in more than one language, and I'm having problems with the routes. I'm following the instruction of the book 'Agile web development with Rails 4'.
The browser print me this error but I can see that routes are created correctly, so:
What am I doing wrong? (At the end of this message I'll attach all my routes)
No route matches [POST] "/en/home"
When I try putting the routes directly in the browser ("localhost:3000/en" OR "localhost:3000/es") everything works OK. The error prints only when I change my language's switcher. That's why I think the routes are correctly set, and I think is a problem of my switcher or the controller...?
This is the code in the application.html.rb (basically a switcher between languages):
<%= form_tag home_path, class: 'locale' do %>
<%= select_tag 'set_locale',
options_for_select(LANGUAGES, I18n.locale.to_s),
onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
This is the configuration of my routes.rb file:
Group::Application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
scope '(:locale)' do
resources :posts
resources :contacts
root "posts#home"
get "/home" => "posts#home"
get "/contact" => "contacts#new"
# static pages
get "/investment" => "contents#investment"
get "/partner-with-us" => "contents#partner", as: "partner"
get "/our-companies" => "contents#companies", as: "companies"
get "/site-map" => "contents#sitemap", as: "sitemap"
get "/terms-and-conditions" => "contents#terms", as: "terms"
get "/privacy" => "contents#privacy"
end
end
This is a file created in /config/initializers/i18n.rb:
#encoding: utf-8
I18n.default_locale = :en
LANGUAGES = [
['English', 'en'],
["EspaƱol".html_safe, 'es']
]
And finally, this is the code for my posts_controller.rb, because here is where I create an action "home" in order to put the last post in the home page:
class PostsController < ApplicationController
def index
#posts = Post.all.order("created_at desc")
end
def show
#post = Post.find(params[:id])
end
def home
if params[:set_locale]
redirect_to home_url(locale: params[:set_locale])
else
#posts = Post.all.order("created_at desc")
end
end
end
try to add :method => :get to your form, like this
<%= form_tag home_path, class: 'locale', :method => :get do %>

rails3 routes.rb

I have been working with rails3, here the view.html.erb form have one login button, so when i click on that button, gives no routes matches :controller => 'home', :action => 'login'. But i have put that in routes.rb. Why this happening?
view.html.erb
<%= form_tag( { :controller => 'home', :action => 'login' }, { :method
=> 'post'}) do %>
<%= text_field(:name, :name, :class => "span2",:placeholder =>
"Username") %>
<%= password_field_tag(:password, :password, :class =>"span2") %>
<%= submit_tag "Login", :class => "btn btn-primary" %>
<% end %>
**routes.rb**
resources :home
resources :home do
post :login, :on => :member
end
**homecontroller.rb**
class HomeController < ApplicationController
def login
end
end
You have defined "resources :home" twice, first declaration is useless and overrides second.
Since you used resources to define your routes (which is recomended) you should use the helper method generated, in this case its login_home_path instead of the old syntax { :controller => 'home', :action => 'login' }
First,
You declare resources :home two times.
try this way in your routes.rb
resources :home
match '/login', to: 'home#login'
and use login_path in submit tag.
I would prefere for login, logout, create Sessions controller
rails generate controller Sessions --no-test-framework
and for login create new method and for logout(signout) create destroy method

Howto write form_for without :url for multiple object editing?

I am following http://railscasts.com/episodes/52-update-through-checkboxes. There a form can be written like this: <% form_tag update_multiple_products_path, :method => :put do %>. This does not generate the right action (action="/products") so i have to manually setup the action url.
Is there a away to write form_for without :url parameter?
My setup:
# index.html.erb
<%= form_for update_multiple_products_path, :url => {:action => 'update_multiple'}, :method => :put do %>
# routes.rb
resources :products do
collection do
put :update_multiple
end
end
# productscontroller
class ProductsController < ApplicationController
...
def update_multiple

Undefined method with "_path" while using rails form_for

I'm running into a (I think) routing error while using the Rails form_for helper. I have been searching around and looked at this question, but the plural for "static_event" with pluralize is "static_events" so I am at a loss. Any help would be apprecited. Here are the details....
ActionView::Template::Error (undefined method `static_events_path' for #<#<Class:0x007f9fcc48a918>:0x007f9fcc46fa78>):
My Model:
class StaticEvent < ActiveRecord::Base
attr_accessible :content, :title, :discount, :location, :day_of_week, :start_time
My Controller:
class StaticEventsController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
before_filter :authorized_user, :only => [:destroy]
def new
#title = "Share An Event"
#static_event = StaticEvent.new
end
def create
#static_event = current_user.static_events.build(params[:event])
if #static_event.save
flash[:success] = "Event Shared"
redirect_to #static_event #this was the old version
else
render :new
end
end
The route:
match '/static-events/new', :to => 'static_events#new'
match '/static-events/', :to => 'static_events#index'
match '/static-events/:id', :to => 'static_events#show'
The view
<%= form_for (#static_event) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= text_field "static_event", "title", "size" => 48 %>
<%= time_select "static_event", "start_time", {:ampm => true, :minute_step => 15} %>
<%= text_area "static_event", "content", "cols" => 42, "rows" => 5 %>
<%= text_field "static_event", "discount", "size" => 48 %>
<%= text_field "static_event", "location", "size" => 48 %>
<%= text_field "static_event", "day_of_week", "size" => 48 %>
<input name="" type="submit" class="button" value="share on chalkboard" />
<% end %>
Only routes created using the resources method are automatically named.
If you want to name your routes, use the :as option:
match '/static-events/new', :to => 'static_events#new', :as => :new_static_event
match '/static-events/', :to => 'static_events#index', :as => :static_events
match '/static-events/:id', :to => 'static_events#show', :as => :static_event
However, it's better to use the resources method. You must pass the "true" name of your model as the first parameter, then override the path if you want:
resources :static_events, :path => 'static-events'
First of all, you should define your routes this way:
resources 'static-events', :only => [:new, :create]
This will create a route for new and create methods.
Because when you use a new ActiveRecord object as an argument to form for, it will looks for *s_path like static_events_path in your routes file with the POST verb.
I think the way you have defined your routes doesn't create the static_events_path with POST verb (you can check that by using rake routes as megas said). So don't use match anymore, use resources or get/post/... instead of match in your Rails 3 projects.
EDIT
I did not notice yesterday, but there is no route for create method. Add the route below before static_events#index or remove all your routes and do like I said above.
post '/static-events/', :to => 'static_events#create'
Run rake routes and you'll see the list of your routes. Then you can fix the route file to have appropriate route path.
This happened to me when i was using a nested resource, but forgot to actually initialize the parent resource using load_and_authorize_resource in cancan. Therefore, the parent resource was null and it threw this error.
I fixed it by declaring load_and_authorize_resource on the parent in the controller.

Rails 3 : Can't get form_for to work as a 'delete' following the RESTful achitecture => always giving a ROUTING ERROR

I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => #user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.
--edit
Routes for relationship resources:
resources :relationships, :only => [:index, :create, :destroy]
Unfollow button (creates a form for itself):
= button_to "Unfollow", relationship_path(relationship), :method => 'delete'

Resources