I am trying out for basic login and registration in ruby on rails,
ISSUE: When I click register button from new.html I am getting GET request but I can see method="post" in page source of that html
I have posted my code below
new.html.erb
<form>
<%= form_for(#user) do |f| %>
<%= f.label :user_name %>
<%= f.text_field :user_name %></br>
<%= f.label :email_id %>
<%= f.text_field :email_id %></br>
<%= f.label :password %>
<%= f.password_field :password %></br>
<%= f.label :college %>
<%= f.text_field :college %></br>
<%= f.label :major %>
<%= f.text_field :major %></br>
<%= f.label :current_job %>
<%= f.text_field :current_job %></br>
<%= f.submit("Create Account",class: "btn btn-primary") %>
<% end %>
</form>
My index.html.erb code which is loaded while application starts
<div class="container">
<div class="row">
<div class="login">
<%= form_tag("/user/login",:method => :post) do %>
<%= label_tag(:EmailId) %>
<%= text_field_tag(:email, params[:email]) %></br>
<%= label_tag(:password) %>
<%= password_field_tag(:password, params[:password]) %></br>
<%= submit_tag("Log In",class: "btn btn-primary") %>
<%= submit_tag("Register",class: "btn btn-primary") %>
<% end %>
</div>
</div>
</div>
My controller code
class UsersController < ApplicationController
def index
#user = User.all
end
def login
print "In Sign in controller"
#user = User.new
if params[:commit] == 'Register'
print "inside Register class"
redirect_to '/users/new'
else
#user = User.find(params[:email_id])
if #user and user.authenticate(params[:password])
sessions[:userId] = #user.user_id
end
end
end
def new
puts "****Inside New Method******"
#user = User.new
end
def create
puts "****Inside create Method******"
end
private
def user_params
end
end
My Route code
Rails.application.routes.draw do
root 'users#index'
resources :users
post '/users/login' => 'users#login'
As per my understanding post request should hit create method, but get /users method is hitting. Please help me out regarding this
Why do you have nested forms in new.html.erb? Remove the first form tag
<form>
<%= form_for(#user) do |f| %>
You have a form tag inside another form tag. Remove the tag at the top of your form. <%= form_for(#user) %> takes care everything that's needed to build the correct form.
Related
I've been creating a system where I create these entries but the problem is when I tried to create the edit feature, it does not update the post but creates a new one.
My controller file looks like this:
class BetsController < ApplicationController
def new
#bet = Bet.new
end
def create
#bets = Bet.new(bet_params)
if #bets.save
flash[:success] = 'Bet Successfull Logged.'
redirect_to new_bet_path
else
flash[:danger] = 'Error, Bet has not been logged. Try again mate.'
redirect_to new_bet_path
end
end
def show
#bet = Bet.find(params[:id])
end
def edit
#bet = Bet.find(params[:id])
end
def update
#bet = Bet.find(params[:id])
if #bet.update_attributes(bet_params)
flash[:success] = "Bet Updated!"
redirect_to bet_path(params[:id])
else
render action: :edit
end
end
private
def bet_params
params.require(:bet).permit(:bet_placed, :game, :units_placed, :odds, :profit_or_loss)
end
end
And also the form that is submitting looks like this:
<%= form_for :bet, url: bets_path, :html => { :multipart => true } do |f| %>
<p class="form-group">
<%= f.label :bet_placed %><br>
<%= f.text_field :bet_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :game %><br>
<%= f.text_field :game, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :units_placed %><br>
<%= f.text_field :units_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :odds %><br>
<%= f.text_field :odds, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :profit_or_loss %><br>
<%= f.text_field :profit_or_loss, class: 'form-control' %>
</p>
<%= f.submit 'Update Profile', class: 'btn btn-default' %>
<% end %>
The way your form is currently setup, it will always hit the create action. If you change your form_for to the following it should work
<%= form_for #bet do |f| %>
...
<% end %>
You shouldn't need multipart => true either since your form doesn't have any file inputs.
According to the rails natural routing the path bets_path always goes to the create method. As update needs an id, and you are not giving it, it goes to create.
Change the form to,
<%= form_for #bet, :html => { :multipart => true } do |f| %>
<p class="form-group">
<%= f.label :bet_placed %><br>
<%= f.text_field :bet_placed, class: 'form-control' %>
</p>
....................
<p class="form-group">
<%= f.label :profit_or_loss %><br>
<%= f.text_field :profit_or_loss, class: 'form-control' %>
</p>
<%= f.submit 'Update Profile', class: 'btn btn-default' %>
<% end %>
For reference,
HTTPVerb Path controller#Action Named Helper
GET /bets bets#index bets_path
GET /bets/new bets#new new_bet_path
POST /bets bets#create bets_path
GET /bets/:id bets#show bet_path(:id)
GET /bets/:id/edit bets#edit edit_bet_path(:id)
PATCH/PUT /bets/:id bets#update bet_path(:id)
DELETE /bets/:id bets#destroy bet_path(:id)
I hope they are good, need to do nested forms with the profile of a user and their respective car.
each user has their own profile:
class PerfilController < ApplicationController
before_filter :authenticate_user!
def index
end
def show
#usuario = current_user
#usuario.perfil ||= Perfil.new
#perfil = #usuario.perfil
end
def update
#usuario = current_user
#perfil = #usuario.perfil
respond_to do |format|
if #usuario.perfil.update_attributes(perfil_params)
format.html {redirect_to #usuario, notice: "Datos Actualizados" }
else
format.html {render action: "show"}
end
end
end
private
def perfil_params
params.require(:perfil).permit(:nombre, :apellido, :rut)
end
end
I want the fund to ensure that each time a user posts a car, you can update your profile. Deputy nested form
<%= simple_form_for(#auto) do |f| %>
<%= f.error_notification %>
<%=f.fields_for #perfil do |perfil_builder|%>
<p>
<%= perfil_builder.label :nombre %><br/>
<%= perfil_builder.text_field :nombre %>
<%= perfil_builder.label :apellido %><br/>
<%= perfil_builder.text_field :apellido %>
<%= perfil_builder.label :rut %><br/>
<%= perfil_builder.text_field :rut %>
</p>
<%end%>
<div class="form-inputs">
<%= f.association :region %>
<%= f.association :ciudad %>
<%= f.association :marca %>
<%= f.input :modelo %>
<%= f.input :version %>
<%= f.input :año %>
<%= f.input :patente %>
<%= f.association :tipo_transmision %>
<%= f.association :combustible %>
<%= f.association :tipo_vehiculo %>
<%= f.association :carroceria %>
</div>
<div class="form-actions">
<%= f.button :submit, :id => 'submit' %>
</div>
<% end %>
The problem is when you want to update does not change the user data but if you publish the article. the error in the console is: Unpermitted parameter: perfil
Best regards friends.
You are sending the request to the auto controller, hence the Unpermitted parameter: perfil error and failure to update.
You can add this to your Perfil controller:
has_many: :autos
accept_nested_attributes_for: :autos
Then in the view switch it around like this:
<%= simple_form_for(#perfil) do |f| %>
<%= f.error_notification %>
<%= f.label :nombre %><br/>
<%= f.text_field :nombre %>
...
<%=f.fields_for :auto do |auto_builder|%>
<p>
<%= auto_builder.association :region %><br/>
...
See this page for more info: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.
form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect
I am trying to have a form show up for a related model, but it is not displaying when I view the page in a browser. How do I url field in my fields_for to display?
Here is my code:
User Model:
class UsersController < ApplicationController
def new
#user = User.new
#user.websites.build
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
end
Website Model:
class Website < ActiveRecord::Base
belongs_to :user
end
Users View:
<h1>Sign Up</h1>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
...
<% end %>
<p>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br/>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br/>
<%= f.password_field :password_confirmation %>
</p>
<% f.fields_for :websites do |builder| %>
<%= builder.label :url %><br/>
<%= builder.text_field :url %>
<% end %>
<p class="button"><%= f.submit %></p>
<% end %>
Final output:
You missed the equals sign in your erb tag.
<% f.fields_for :websites do |builder| %>
.. should be ..
<%= f.fields_for :websites do |builder| %>
Does that fix it?
It looks like you're maybe confusing singular and plural in your fields_for as well. Calling it with the plural websites then treating the block as a singular website doesn't make sense.
Im trying to set up a log-in and password page for my site. This will be on the index page. I have a Model Class: "User" and a controller class "users_controller".
The behavior that Im trying to implement is this:
Users type in the url of the site and it opens the index page as shown below. Then the users signs in and is brought to a different page where they have access to the content of the site.
Now the question is what do I need to put in the "def index" function so that it accepts the form's paramters and passes these to another function called "self.authenticate(parameters)" to check and return a status
Here is the code for the users_controller
class UsersController < ApplicationController
def index
#user = User.new
respond_to do |format|
format.html
#index.html.erb
end
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def edit
#user = User.find(params[:id])
#title = #user.name
end
def destroy
#user = User.find(params[:id])
end
def new
#user = User.new
#title = "Sign Up"
end
def create
end
end
Here is the form:
<html>
<h1>Returning Users- Sign In</h1>
<%= form_for(#user) do |f| %>
<div class="field">
<%=f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class = "field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class = "actions">
<%= f.submit "Sign In" %>
</div>
<% end %>
<h1>New Users- Sign Up</h1>
<%= form_for(#user) do |f| %>
<div class="field">
<%=f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%=f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class = "field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class = "field">
<%= f.label :password_confirmation, "Confirm Password" %><br/>
<%= f.password_field :password_confirmation %>
</div>
<div class = "actions">
<%= f.submit "Sign Up" %>
</div>
<%end%>
<br/>
<%= debug(params) if Rails.env.development? %>
<br/>
</body>
Any help would be great. Thanks
Have a look at the recent RailsCast Authentication from Scratch from Ryan Bates. You can also use a common authentication plugin like AuthLogic or Devise.
I'm going to explain to you what you want to do.
You can get the params by using the hash params:
params[]
Then, you can get the value of a field by passing it's identification, like:
params[:id]
or
params[:name]
Now pass it to the self.authenticate(parameters) in the way you can(I don't know how this method is implemented).