Validation for the create action are not working, I have made validation for the field to be present, but if I keep the fields empty and press submit, I get routing error, if I fill in the complete fields, It works perfectly fine. Also, the validation works perfectly fine for the update action.
Here is the view:
<%= stylesheet_link_tag 'gmaps4rails' %>
<%= form_for #estate, :html => { :class => 'form-horizontal',:multipart => true } do |f| %>
<% if #estate.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#estate.errors.count, "error") %> prohibited this estate from being saved:</h2>
<ul>
<% #estate.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<script>
function enableDisable(bEnable, textBoxID)
{
document.getElementById(textBoxID).disabled = !bEnable
}
</script>
<div class="control-group">
<%= f.label :Name, "Property/Tenant Name", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :Name, :class => 'text_field', :placeholder => "e.g., Saxbys Coffee" %>
</div>
</div>
<div class="control-group">
<%= f.label :Address, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :Address, :class => 'text_area', :cols => '10', :rows => '10', :placeholder => "e.g., 1236 36th Street NW Washington, DC 20007" %>
</div>
</div>
<div class="control-group">
<%= f.label :asset, "Upload Picture", :class => 'control-label' %>
<div class="controls">
<%= f.file_field :asset %>
</div>
</div>
<% if #estate.asset.present? %>
<div class="control-group">
<%= f.label "Delete Existing Picture", :class => 'control-label' %>
<div class="controls">
<%= f.check_box(:delete_asset) %>
</div>
</div>
<% end %>
<!-- <div class="control-group">
<%= f.label :Mgmt, "Would you like to share this property with your Real Estate Management Company?", :class => 'control-label' %>
<div class="controls">
<input type="checkbox" id="chkbox" onchange="document.getElementById('txtBox').disabled=!this.checked;" checked="checked" />
</div>
</div> -->
<% if current_user.Company.nil? %>
<div id="flip"><a>Would you like to share this property with your Management Company?</a></div>
<br />
<div id="panel">
<div class="control-group">
<%= f.label :Mgmt, "Company Name", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :Mgmt, :class => 'text_field', :id => 'txtBox'%>
</div>
</div>
<div class="control-group">
<%= f.label :companyemail, "Company Email", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :companyemail, :class => 'text_field', :id => 'txtBox'%>
</div>
</div>
</div>
<% end %>
<div class="form-actions">
<% if current_page?(controller:"estates", action:"edit", :id => params[:id] || 0)%>
<%= f.submit "Update Property Details", :class => 'btn btn-info' %>
<% else %>
<%= f.submit "Upload Property Details", :class => 'btn btn-info' %>
<% end %>
<% end %>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
</script>
Here is the controller:
def create
# #estate = Estate.new(params[:estate])
if current_user.Company.nil?
#estate = current_user.estates.build(params[:estate])
else
serve = User.find(##key)
#estate = Estate.new(params[:estate])
#estate.user_id = serve.id
#estate.Mgmt = current_user.Company
end
respond_to do |format|
if #estate.save
if current_user.Company.nil?
if #estate.companyemail = ''
#
else
EstateMailer.company_confirmation(#estate).deliver
end
end
format.html { redirect_to #estate, notice: 'Property details were successfully updated.' }
format.json { render json: #estate, status: :created, location: #estate }
else
format.html { render action: "new" }
format.json { render json: #estate.errors, status: :unprocessable_entity }
end
end
end
Error message:
No route matches {:action=>"show", :controller=>"estates", :id=>#<Estate id: nil, Name: "", Address: "", created_at: nil, updated_at: nil, user_id: 5, asset_file_name: nil, asset_content_type: nil, asset_file_size: nil, asset_updated_at: nil, Mgmt: "", companyemail: "", latitude: nil, longitude: nil, gmaps: nil>}
routes.rb
resources :feedbacks
root to: 'home#index'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :profiles
resources :estates do
resources :records do
resources :documents
end
end
get 'faq/faqs'
match '/records',to: 'estates#record'
get 'management/index'
match 'management/show', to: 'management#show'
match 'management/showrecord', to: 'management#showrecord'
after a little chat with Hrishikesh Sardar the bug was fixed, the problem was in if block of the create action:
if current_user.Company.nil?
#estate = current_user.estates.build(params[:estate])
else
serve = User.find(##key)
#estate = Estate.new(params[:estate])
#estate.user_id = serve.id
#estate.Mgmt = current_user.Company
end
this line:
#estate = current_user.estates.build(params[:estate])
had to be replaced by:
#estate = Estate.new(params[:estate])
#estate.user_id = current_user.id
somehow current_user.estates.new(params[:estate]) didn't worked, even if the relation between estate and user was built as required.
Related
I'm using Mailboxer as my messaging gem in my rails app.
So the site's about classifieds. Every classified has a contact seller link , which i have implemented to route to the new_message form and write the message to the recipient fine!
Now i want to add some info about the classified that the contact form is made for!
class MessagesController < ApplicationController
before_action :authenticate_user!
def new
#user = User.find_by(id: params[:recipient_id])
end
def name
first_name
end
def mailboxer_email(object)
email
end
def create
recipients = User.find_by(id: params[:recipient_id])
conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
flash[:success] = "Message has been sent!"
redirect_to conversation_path(conversation)
end
end
This is the view where i call new_message_path and then retrieve the user_id to define the recipient.
<div class="center-div">
<div class="col-lg-12" id="image">
<% #classified.photos.each do |p| %>
<h3 class="dark-grey"><%= #classified.title %></h3>
<h5 class="dark-grey">Loved by: <%= #classified.favorited_by.count %> </h5>
<%= image_tag p.image %>
<%end%>
</div>
<%= link_to "", new_message_path(:recipient_id => #classified.user_id), :class => "glyphicon glyphicon-envelope" , :style => "color:#EFCE7B" %>
<%if current_user.favorite_classifieds.collect(&:classified_id).include?(#classified.id) %>
<%= link_to "", favorite_classified_path(#classified, type: "unfavorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#FF0000", method: :put %>
<%else%>
<%= link_to "", favorite_classified_path(#classified, type: "favorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#000000", method: :put %>
<%end%>
<%= link_to "", editlisting_path(#classified) , :class => "glyphicon glyphicon-flag" , :style => "color:#EB573B" %>
<ul>
<h3 class="dark-grey">Model</h3>
<li><%= #classified.model %></li>
<h3 class="dark-grey">Description</h3>
<li><%= #classified.description %></li>
<li><%= link_to "Back", '/' , :class => "link" %></li>
<li><%= link_to #classified.user.first_name, profile_path(#classified.user_id) %></li>
</ul>
</div>
and this is my message_new view where i want to have the data of the classified
<div class="center-div">
<div class="messages-box">
<% page_header "Αποστολή μηνύματος" %>
<%= form_tag messages_path, method: :post do %>
<div class="form-group">
<%= label_tag 'message[subject]', 'Subject' %>
<%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= label_tag 'message[body]', 'Message' %>
<%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= hidden_field_tag(:recipient_id, "#{#user.id}") %>
</div>
<%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>
</div>
</div>
also for any solution a good explanation would help me a lot.
Thank you.
I ended up using nested routes.
resources :classifieds do
put :favorite, on: :member
resources :messages do
end
end
this is my view
<div class="center-div">
<div class="col-lg-12" id="image">
<% #classified.photos.each do |p| %>
<h3 class="dark-grey"><%= #classified.title %></h3>
<h5 class="dark-grey">Loved by: <%= #classified.favorited_by.count %> </h5>
<%= image_tag p.image %>
<%end%>
</div>
<%= link_to "", new_classified_message_path(:recipient_id => #classified.user_id , :classified_id => #classified.id), :class => "glyphicon glyphicon-envelope" , :style => "color:#EFCE7B" %>
<%if current_user.favorite_classifieds.collect(&:classified_id).include?(#classified.id) %>
<%= link_to "", favorite_classified_path(#classified, type: "unfavorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#FF0000", method: :put %>
<%else%>
<%= link_to "", favorite_classified_path(#classified, type: "favorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#000000", method: :put %>
<%end%>
<%= link_to "", editlisting_path(#classified) , :class => "glyphicon glyphicon-flag" , :style => "color:#EB573B" %>
<ul>
<h3 class="dark-grey">Model</h3>
<li><%= #classified.model %></li>
<h3 class="dark-grey">Description</h3>
<li><%= #classified.description %></li>
<li><%= link_to "Back", '/' , :class => "link" %></li>
<li><%= link_to #classified.user.first_name, profile_path(#classified.user_id) %></li>
</ul>
</div>
Got the value from the controller
class MessagesController < ApplicationController
before_action :authenticate_user!
def new
#user = User.find_by(id: params[:recipient_id])
#classified = Classified.find_by(id: params[:classified_id])
end
def name
first_name
end
def mailboxer_email(object)
email
end
def create
classifieds = Classified.find_by(id: params[:classified_id])
recipients = User.find_by(id: params[:recipient_id])
conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
flash[:success] = "Message has been sent!"
redirect_to conversation_path(conversation)
end
end
and last rendered it in my messages_new view
<div class="center-div">
<div class="messages-box">
<% page_header "Αποστολή μηνύματος" %>
<%= form_tag messages_path, method: :post do %>
<div class="form-group">
<%= label_tag 'message[subject]', 'Αποστολή μηνύματος για την αγγελία : ' %>
<%= label_tag(:classified_id, "#{#classified.title}") %> <!-- integrate this -->
<%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %> <!-- integrate here -->
</div>
<div class="form-group">
<%= label_tag 'message[body]', 'Μύνημα στον χρήστη : ' %>
<%= label_tag(:recipient_id, "#{#user.first_name}") %>
<%= text_area_tag 'message[body]', nil,rows: 15, cols: 3, class: 'form-control', required: true %> <!-- integrate here -->
</div>
<div class="form-group">
<%= hidden_field_tag(:recipient_id, "#{#user.id}") %>
</div>
<%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>
</div>
</div>
I have 2 models.
Scoreboard Model : can have many teams
Team Model : belongs to Scoreboard
Teams has the follow columns:
name: string
win,loss,tie: integer
On the Team#Index view, I have a collection of all the teams associated to the Scoreboard. Also on that page, I can render an edit form on top of each team object and update it through ajax. Here is the relevant code:
Team#Index View:
<div class="team-list">
<%= render #teams.reject(&:new_record?) %>
</div>
_team.html.erb
<div class="row team-div" id="team_<%=team.id%>">
<%= link_to (scoreboard_team_path(#scoreboard, team)) do %>
<div class="col-xs-4 team-div-1"> <%= team.name %> </div>
<% end %>
<%= link_to (edit_scoreboard_team_path(#scoreboard, team)), remote: true, class: "team-edit-link" do %>
<div class="col-xs-6 team-data">
<div class="row">
<div class="col-xs-4 team-div-2"><%= team.win %> </div>
<div class="col-xs-4 team-div-2"><%= team.loss %> </div>
<div class="col-xs-4 team-div-2"><%= team.tie %></div>
</div>
</div>
<% end %>
</div>
As you can see, I have an edit link available which renders an edit form in place of the team object in question through ajax. The edit form only updates the win,loss,tie columns.
Edit form rendered:
<%= form_for [#scoreboard, #team], remote: true do |f| %>
<div class="row team-edit-form">
<div class="col-xs-4 edit-team-1">Placeholder</div>
<div class="col-xs-2 edit-team-2"><%= f.number_field :win, min: 0, max: 9999, class: "form-control", placeholder: "0" %></div>
<div class="col-xs-2 edit-team-2"><%= f.number_field :loss, min: 0, max: 9999, class: "form-control", placeholder: "0" %></div>
<div class="col-xs-2 edit-team-2"><%= f.number_field :tie, min: 0, max: 9999, class: "form-control", placeholder: "0" %></div>
<div class="col-xs-2 edit-team-3"> <%= f.submit "Done", :data => {:disable_with => "Saving..."}, class: "btn btn-primary" %></div>
</div>
<% end %>
Team#update Controller Method(which by ajax reloads the newly edited team object div):
def update
#scoreboard = Scoreboard.find(params[:scoreboard_id])
#team = #scoreboard.teams.find(params[:id])
if #team.update_attributes(team_params)
respond_to do |format|
format.html {redirect_to scoreboard_teams_path(#scoreboard)}
format.js
end
else
respond_to do |format|
format.html {redirect_to scoreboard_teams_path(#scoreboard)}
format.js { render action: "update_error" }
end
end
end
Now for my problem. All the above happens on the team#index view by ajax.
On the team#show view, I would like to edit only the team name without ajax.
So far, this is what the team#show view looks like:
Team#show View
<h3> <%= #team.name %> <h3>
<%= form_for [#scoreboard, #team] do |f| %>
<div class="col-xs-4"><%= f.text_field :name, required: true, maxlength: 30, class: "team-name-field form-control", placeholder: "Enter name" %></div>
<div class="col-xs-1"> <%= f.submit "Update", :data => {:disable_with => "Saving..."}, class: "btn btn-primary" %></div>
<% end %>
Upon submission, I would like to update the name and then redirect the page back to the team#show(no ajax required). Currently, this form also routes to the same update method. Is it possible to use the same update method but execute different response code for the team#show view? If not, how can I have a customized update code executed when I submit the form on the team#show page?
I'm not sure you can use a different respond_to as you can only redirect or render once in an action.
One approach would be to create a new route / controller method for updating the team name.
routes
resources :teams do
member { post 'update_name' }
end
show.html.erb
In the view, you can post to the above route and in the controller, create a method for the new route.
<%= form_for #team, :url => update_name_team_path(#team)
teams_controller
def update_name
#team = Team.find(params[:id])
redirect_to team_path(#team)
end
Yes, you can do this by checking the request type or sending a flag/keyword to check for the response to render, I would do it like :
Add hidden_field for request param in edit form like :
<%= form_for [#scoreboard, #team], remote: true do |f| %>
<div class="row team-edit-form">
<div class="col-xs-4 edit-team-1">Placeholder</div>
<div class="col-xs-2 edit-team-2"><%= f.number_field :win, min: 0, max: 9999, class: "form-control", placeholder: "0" %>
<%= f.hidden_field :request, 'js' %> </div>
<div class="col-xs-2 edit-team-2"><%= f.number_field :loss, min: 0, max: 9999, class: "form-control", placeholder: "0" %></div>
<div class="col-xs-2 edit-team-2"><%= f.number_field :tie, min: 0, max: 9999, class: "form-control", placeholder: "0" %></div>
<div class="col-xs-2 edit-team-3"> <%= f.submit "Done", :data => {:disable_with => "Saving..."}, class: "btn btn-primary" %></div>
</div>
<% end %>
Add hidden_field for request param in name edit form in show page like :
<%= form_for [#scoreboard, #team] do |f| %>
<div class="col-xs-4"><%= f.text_field :name, required: true, maxlength: 30, class: "team-name-field form-control", placeholder: "Enter name" %>
<%= f.hidden_field :request, 'http' %> </div>
<div class="col-xs-1"> <%= f.submit "Update", :data => {:disable_with => "Saving..."}, class: "btn btn-primary" %></div>
<% end %>
Team#Update Method will be like :
def update
#scoreboard = Scoreboard.find(params[:scoreboard_id])
#team = #scoreboard.teams.find(params[:id])
if #team.update_attributes(team_params)
redirect_to scoreboard_teams_path(#scoreboard) if params[:request] == 'http'
render "" if params[:request] == 'js'
else
redirect_to scoreboard_teams_path(#scoreboard) if params[:request] == 'http'
render action: "update_error" if params[:request] == 'js'
end
end
I have a problem with this code .
This is the controller :
def create
#mean = TrasmissionMean.new(trasmission_mean_params)
if #mean.save
flash[:success] = "Mezzo di Trasmissione #{#mean.description}"
else
flash[:error] = "Error #{#mean.errors.full_messages}"
render :action => 'new'
end
end
def trasmission_mean_params
params.require(:mean).permit(:description)
end
I have a problem with this code .
This is the view , with render the form:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<%= form_for #mean, :html => {:class => "form-horizontal" },:url => url_for(:controller => "trasmission_means", :action => "create" ) do |f| %>
<hr>
<%= render partial: "form", :locals => { :#mean => #mean, :f => f } %>
<hr>
<%= f.submit "Aggiungi", class: 'btn btn-success btn-lg pull-right' %>
<% end %>
</div>
</div>
the render form :
<div class="form-group">
<div class="col-lg-12">
<%= f.text_area :description, :rows => 4, class: 'form-control ', placeholder: 'Descrizione' %>
</div>
</div>
and this is the error . I do not understand where the problem is
ActionController::ParameterMissing in Protocol::TrasmissionMeansController#create
param is missing or the value is empty: mean
The parameters are based on the class
def trasmission_mean_params
params.require(:mean).permit(:description)
end
Is looking for mean in the params hash but your class is called TransmissionMean which means it should be
def trasmission_mean_params
params.require(:transmission_mean).permit(:description)
end
I'm following this tutorial to set up Devise integration with Stripe: http://www.jaredrader.com/blog/2013/12/18/a-stripe-integration
I have successfully setup Stripe as detailed and created the various controllers, models and views.
However, the forms are creating an ArgumentError in Users::Registrations#new
Here's the error code:
ArgumentError in Users::Registrations#new
Showing /home/action/workspace/mediadb/app/views/devise/registrations/new.html.erb where line #62 raised:
First argument in form cannot contain nil or be empty
The form:
<div class="panel panel-default">
<div class="panel-heading">
<% if params[:plan] == "2" %>
<h1>Sign up with premium!</h1>
</div>
<div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= hidden_field_tag 'plan', params[:plan] %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: "form-control" %>
</div>
<h2>Payment</h2>
<%= f.hidden_field :stripe_card_token %>
<div class="form-group">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil, class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil, class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<div class="form-group">
<%= f.submit "Sign up", class: "btn btn-lg btn-success" %>
</div>
<% end %>
</div>
<% else %>
<h1>Sign up for free</h1>
</div>
<div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { id: "free_plan"}) do |f| %>
<%= devise_error_messages! %>
<%= hidden_field_tag 'plan', params[:plan] %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Sign up", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
<% end %>
</div>
</div>
The routes.rb file:
Rails.application.routes.draw do
resources :lists
resources :publications
resources :contacts
devise_for :users, controllers: { registrations: 'users/registrations' }
devise_scope :user do
get '/sign_up', to: 'users/registrations#new', as: :sign_up
get '/sign_in', to: 'devise/sessions#new', as: :sign_in
get '/:id/edit', to: 'users/registrations#edit', as: :edit
put 'users/update_plan', :to => 'users/registrations#update_plan'
put 'users/cancel_plan', :to => 'users/registrations#cancel_plan'
end
resources :users, only: [:index, :show]
# root should always be last
root to: 'pages#home'
end
The Registration controller:
class Users::RegistrationsController < Devise::RegistrationsController
def new
unless (params[:plan] == '1' || params[:plan] == '2')
flash[:notice] = "Please select a plan to sign up."
redirect_to root_url
end
end
def update_plan
#user = current_user
#user.update_attributes(plan_id: params[:plan], email: params[:email], stripe_card_token: params[:user][:stripe_card_token])
if #user.plan_id == 2
#user.save_with_payment
redirect_to edit_user_registration_path, notice: "Updated to premium!"
else
flash[:error] = "Unable to update plan."
render :edit
end
end
def cancel_plan
#user = current_user
if #user.cancel_user_plan(params[:customer])
#user.update_attributes(stripe_customer_token: nil, plan_id: 1)
flash[:notice] = "Canceled subscription."
redirect_to edit_user_registration_path
else
flash[:error] = "There was an error canceling your subscription. Please notify us."
render :edit
end
end
private
def build_resource(*args)
super
if params[:plan]
resource.plan_id = params[:plan]
if resource.plan_id == 2
resource.save_with_payment
else
resource.save
end
end
end
def setup
plans = Plan.all
plans.each do |plan|
unless plan.id == 1
#startup_plan = plan
end
end
end
end
Any idea what's wrong?
Update Users::RegistrationsController#new as follows
def new
if (params[:plan] == '1' || params[:plan] == '2')
super
else
flash[:notice] = "Please select a plan to sign up."
redirect_to root_url
end
end
My Rails application just wont add any class to fields with errors. Cant find wihat is the problem.
Got this in model:
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :phone
Any ideas where to start looking for solutions ?
This is the view erb file which does not generate the required styled class:
<%= form_for :company, :url => {:action => 'create_lead'}, :html => {:class => "form-horizontal"} do |f| %>
<div class="">
<div class="span2">
<%= f.label :csdd_nr, "CSDD numurs" %>
<%= f.text_field :csdd_nr, {:class => "input-small"} %>
</div>
<div class="span4">
<%= f.label :name, "Nosaukums" %>
<%= f.text_field :name %>
</div>
<div class="span6">
<%= f.label :ap_veh_count, "Auto skaits" %>
<%= f.text_field :ap_veh_count, {:class => "input-small"} %><br /><br />
</div>
<div class="span6">
<%= f.label :office_adress_street, "Faktiskā adrese" %>
<%= f.text_field(:office_adress_street, {:placeholder => 'Iela', :class => "input-medium"}) %> <%= f.text_field(:office_adress_city, {:placeholder => 'Pilsēta', :class => "input-small"}) %> <%= f.text_field(:office_adress_postcode, {:placeholder => 'Pasta indekss', :class => "input-small"}) %>
</div>
<div class="span4">
<%= f.label :web, "Mājaslapa" %>
<%= f.text_field :web %><br /><br />
</div>
<div class="span4">
<%= f.label :phone, "Telefona numurs" %>
<%= f.text_field :phone %>
</div>
<div class="span4">
<%= f.label :email, "E-pasts" %>
<%= f.text_field :email %>
</div>
<div class="span4">
<%= f.label :company_field, "Uzņēmuma nodarbošanās" %>
<%= f.text_field :company_field %><br /><br />
</div>
<%= f.hidden_field(:company_status, :value => "3") %>
<div class="span12">
<br /><br />
<%= submit_tag("Saglabāt", :class => 'btn btn-primary') %>
<%= link_to "Atcelt", {:action => 'list_leads'}, :class => 'btn' %>
</div> def new_lead
#company = Company.new
end
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
redirect_to(:action => 'new_lead')
end
end
</div>
<% end %>
OK, and here is the controller which saves the data to database:
def new_lead
#company = Company.new
end
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
redirect_to(:action => 'new_lead')
end
end
This happens because you're redirecting instead of rendering, when there is a validation error. Your controller should look like:
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
render(:action => 'new_lead')
end
end