I have a form that for some reason always keep same values after error.
For example, if I enter name that already exists, it will get me an error and at the same time it will keep sending old value even if you change it (so you have to refresh page and lose all changes to actually resolve error).
= provide(:title, t(:create_task))
.center
= form_for :task, url: tasks_path do |f|
= render 'shared/big_flash'
= render 'task_error_messages'
.row.text-center
.col-lg-10.col-lg-offset-1.col-md-12.col-sm-12.col-xs-12
.jumbotron
%h2= t(:create_task)
.row
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_task_name)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_new_name)} info_outline
= f.text_field 'name', maxlength: 255, class: 'form-control'
= f.label t(:par_category)
= f.collection_select 'category', Category.order(:name), :name, :name, {}, {class: 'form-control'}
= link_to t(:btn_modify_categories), categories_modify_path, class: 'btn btn-primary btn-block', data: { confirm: t(:changes_lost_proceed) }
= f.label t(:par_task_type)
= f.select 'task_type', Task::TASK_TYPES_LIST, {include_blank: true}, {class: 'form-control', id: 'type_of_task'}
= f.label t(:par_score)
= f.number_field 'score', min: '0', step: '1', class: 'form-control'
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_asset)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_asset)} info_outline
= f.file_field 'category'
.description
= f.label t(:par_description)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_description)} info_outline
= f.text_area 'name', class: 'form-control'
= f.check_box 'mathjax'
= f.label t(:par_mathjax)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_mathjax)} info_outline
.row
.correct_solutions.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_correct_solutions)
#generated_correct_solutions
= button_tag t(:btn_add_correct_solution), type: 'button', class: 'btn btn-primary btn-toolbar', id: 'add_correct_solution'
.wrong_solutions.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_wrong_solutions)
#generated_wrong_solutions
= button_tag t(:btn_add_wrong_solution), type: 'button', class: 'btn btn-primary btn-toolbar', id: 'add_wrong_solution'
#close-ended_task{class: 'hidden'}
= f.check_box 'random', id: 'randomize_task'
= f.label t(:par_random)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_random)} info_outline
#randomize{class: 'hidden'}
.row
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_number_of_solutions)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_no_random_solutions)} info_outline
= f.number_field 'no_random_solutions', min: '1', step: '1', class: 'form-control'
.col-lg-6.col-md-6.col-sm-6.col-xs-12
= f.label t(:par_min_number_of_correct_solutions)
%i{class: 'material-icons text-muted', rel: 'tooltip', title: t(:tt_task_min_no_random_correct_solutions)} info_outline
= f.number_field 'min_no_random_correct_solutions', min: '0', step: '1', class: 'form-control'
.btn-group.btn-group-justified.hidden-xs
.btn-group
= f.submit t(:btn_create_new_task), class: 'btn btn-lg btn-primary btn-toolbar'
.btn-group
= link_to t(:btn_cancel), '#', class: 'btn btn-lg btn-primary btn-toolbar'
.btn-group-vertical.visible-xs
= f.submit t(:btn_create_new_task), class: 'btn btn-lg btn-primary'
= link_to t(:btn_cancel), '#', class: 'btn btn-lg btn-primary'
And my tasks_controller.rb
def new
#task = Task.new
end
def create
#task = Task.new(task_params)
#task.user_id = helpers.current_user.id
if #task.save
flash[:success] = t(:task_created)
redirect_to new_task_path
else
render 'new'
end
end
Any idea what could be the problem?
I don't want to clean up fields after error, I want to keep them, just on next POST, send new values instead.
Related
I have rails app with conditions according to params
# _search.html.slim
.search.text-center.my-5
.container
-if params[:city_slug]
= form_tag(city_jobs_path(city_slug: params[:city_slug]), method: :get, class: "form-inline") do
.row
.col-md-8.mb-md-0.no-padding
= search_field_tag :search, params[:search], placeholder: 'Find a job (name, company, position)', class: 'form-control rounded-left no-border-radius bg-light h-100'
.col-md-2.mb-md-0.no-padding
= select_tag(:salary,
options_for_select( #salary_range.collect {|s| [vnd_format(s), s]},
params[:salary] ),
class:"form-select bg-light h-100")
.col-md-2.mb-md-0.no-padding
= submit_tag "Search", class: "h-100 w-100 btn btn-block btn-lg btn-info"
-elsif params[:industry_slug]
= form_tag(industry_jobs_path(industry_slug: params[:industry_slug]), method: :get, class: "form-inline") do
.row
.col-md-8.mb-md-0.no-padding
= search_field_tag :search, params[:search], placeholder: 'Find a job (name, company, position)', class: 'form-control rounded-left no-border-radius bg-light h-100'
.col-md-2.mb-md-0.no-padding
= select_tag(:salary,
options_for_select( #salary_range.collect {|s| [vnd_format(s), s]},
params[:salary] ),
class:"form-select bg-light h-100")
.col-md-2.mb-md-0.no-padding
= submit_tag "Search", class: "h-100 w-100 btn btn-block btn-lg btn-info"
- else
= form_tag(jobs_path, method: :get, class: "form-inline") do
.row
.col-md-4.mb-md-0.no-padding
= search_field_tag :search, params[:search], placeholder: 'Find a job (name, company, position)', class: 'form-control rounded-left no-border-radius bg-light h-100'
.col-md-2.mb-md-0.no-padding
= select_tag(:city,
options_for_select( #city_slug_list,
params[:city] ),
include_blank: 'Select city',
class:"form-select bg-light h-100")
.col-md-2.mb-md-0.no-padding
= select_tag(:industry,
options_for_select( #industry_slug_list,
params[:industry] ),
include_blank: 'Select city',
class:"form-select bg-light h-100")
.col-md-2.mb-md-0.no-padding
= select_tag(:salary,
options_for_select( #salary_range.collect {|s| [vnd_format(s), s]},
params[:salary] ),
include_blank: 'Select salary',
class:"form-select bg-light h-100")
.col-md-2.mb-md-0.no-padding
= submit_tag "Search", class: "h-100 w-100 btn btn-block btn-lg btn-info"
Overall, they look similar. Only the path is different, is there any way to refactor this code
The easiest way to fix DRY here is to extract common erb-code to partial and render it from every template with:
<%= render partial: 'search_fields' %>
Also your can make your partial more universal with passing some options ith there with locals:
<%= render partial: 'search_fields', locals: { industries: false } %>
https://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
BTW
It is not a good practice to access to params object inside views because it is a controller level. This way your code becomes too coupled. It is better to pass required values to instance variables.
Also you can use PageObject pattern to make your code more maintainable.
rails 6.0.3
bootstrap 5
I'm sorry if there is a simple answer, or if it's not even possible to change the following:
I currently have a select dropdown on my form,
<div class="field mb-3">
<%= form.label :Please_select_document_type %>
<%= form.select(:priority, [['Critical'],['Moderate'], ['Low']], { :include_blank => '-- Select One --' }, {class: "form-check"}) %>
</div>
Drop down menu
I have written below a radio button with the three same priority levels. How do I get the radio button to store the correct value to priority when it is selected?
<%= form.label :priority %><br>
<div class="btn-group" data-toggle="buttons-radio">
<%= form.radio_button :priority, 'Low', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "success-outlined", autocomplete: "off", checked: true %>
<%= form.label :priority, class: "btn btn-outline-success", for: "success-outlined", value: "Low" %>
<%= form.radio_button :priority, 'Moderate', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "warning-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-warning", for: "warning-outlined", value: "Moderate" %>
<%= form.radio_button :priority, 'Critical', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "danger-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-danger", for: "danger-outlined", value: "Critical" %>
</div>
Radio button
The radio buttons should have the name <obj>['priority']. The code in the question does not work since it uses a different name for each radio button. Remove the name key from the radio_button method call and let Rails add it for you.
<%= form.radio_button :priority, 'Low', type: "radio", class: "form-check btn-check", id: "success-outlined", autocomplete: "off", checked: true %>
<%= form.label :priority, class: "btn btn-outline-success", for: "success-outlined", value: "Low" %>
<%= form.radio_button :priority, 'Moderate', type: "radio", class: "form-check btn-check", id: "warning-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-warning", for: "warning-outlined", value: "Moderate" %>
<%= form.radio_button :priority, 'Critical', type: "radio", class: "form-check btn-check", id: "danger-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-danger", for: "danger-outlined", value: "Critical" %>
I am developing a rails application and i am having a drop down submit form as below,
#container
= form_tag({:controller => "r4d", :action=> "result" }, remote: true, method: :get) do
= label_tag(:q, "Trip Type: ")
= select_tag(:q, options_for_select(r4d_options, "r4d_002"), class: "form-control")
= submit_tag("Get Trip Details", :id => "submit", :class => "btn btn-sm btn-default")
The problem here is, the drop down label appears in an line followed by drop down and submit button in an another line. How can i make this appear in same line. I am using bootstrap scss.
Thanks for your help.
Based on the below answer, i have changed the code to
= form_tag({:controller => "r4d", :action => "result", :class => "form-inline"}, remote: true, method: :get) do
.row
.col-sm-12
.col-sm-4
= label_tag(:q, "Trip Type: ")
.col-sm-4
= select_tag(:q, options_for_select(r4d_options), class: "form-control")
.col-sm-4
= submit_tag("Get Trip Details", :id => "submit", :class => "btn btn-sm btn-default")
but there are too much spacing between the form elements and it doesn't looks good.
If you are using haml please try below format for set content in one row
.row
.col-sm-12
.col-sm-6
= label_tag(:q, "Trip Type: ")
= select_tag(:q, options_for_select(r4d_options, "r4d_002"), class: "form-control")
.col-sm-6
= submit_tag("Get Trip Details", :id => "submit", :class => "btn btn-sm btn-default")
I am using the carrierwave gem to upload images of movie listings. I have created a migration creating a image column to my listings database.
updated the Listing model.rb file
class Listing < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :user
end
added the :html = > { multipart: true } command to my simple form
<div class="wizard-container">
<%= simple_form_for(#listing, url: new_user_registration_path, method: :get, :html => { multipart: true } ) do |f| %>
<div class="card wizard-card ct-wizard-orange" id="wizard">
<!-- You can switch "ct-wizard-orange" with one of the next bright colors: "ct-wizard-blue", "ct-wizard-green", "ct-wizard-orange", "ct-wizard-red" -->
<div class="wizard-header">
<h3>
<b>LIST</b> YOUR MOVIE <br>
<small>This information will let us know more about your movie</small>
</h3>
</div>
And the image field to my listing.html.erb file
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<%= f.file_field :image %><br>
</div>
</div>
but when i create a listing the folloeing information , appears in console
Started GET "/users/sign_up?utf8=%E2%9C%93&listing%5Btitle%5D=Top+Gun&listing%5Btime%5D=14.35&listing%5Bdate%5D=24%2F03%2F2016&listing%5Bseats%5D=5&listing%5Bvenue_name%5D=Preston+Library&listing%5Bimage%5D=IMG_0892.JPG&listing%5Bprice%5D=2.3&listing%5Blocation%5D=Preston+Town+Hall&listing%5Bother_info%5D=This+is+a+great+venue&listing%5Bprojector%5D=0&listing%5Bcables%5D=0&listing%5Blaptops%5D=0&listing%5Bseating%5D=0&listing%5Bblinds%5D=0&listing%5Binternet%5D=0&listing%5Blighting%5D=0&listing%5Bcamcorder%5D=0&listing%5Bcatering%5D=0&listing%5Btoilets%5D=0&listing%5Bfire%5D=0&listing%5Bfire%5D=1&commit=Create+Listing" for 127.0.0.1 at 2015-03-24 14:36:31 +0000
Processing by Devise::RegistrationsController#new as HTML
Parameters: {"utf8"=>"✓", "listing"=>{"title"=>"Top Gun", "time"=>"14.35", "date"=>"24/03/2016", "seats"=>"5", "venue_name"=>"Preston Library", **"image"=>"IMG_0892.JPG",** "price"=>"2.3", "location"=>"Preston Town Hall", "other_info"=>"This is a great venue", "projector"=>"0", "cables"=>"0", "laptops"=>"0", "seating"=>"0", "blinds"=>"0", "internet"=>"0", "lighting"=>"0", "camcorder"=>"0", "catering"=>"0", "toilets"=>"0", "fire"=>"1"}, "commit"=>"Create Listing"}
So it captures the image but does mot display the image?
this app is slightly different to what i normally do , I make the user sign in first and then create details ,this app you create listings and then create an account
new.html.erb
<%= render 'layouts/header' %>
<h2 class="text-center">Sign up</h2>
<%
if !user_signed_in?
resource.listings.build
resource.listings[0].title = params[:listing][:title]
resource.listings[0].time = params[:listing][:time]
resource.listings[0].date = params[:listing][:date]
resource.listings[0].seats = params[:listing][:seats]
resource.listings[0].venue_name = params[:listing][:venue_name]
resource.listings[0].location = params[:listing][:location]
resource.listings[0].other_info = params[:listing][:other_info]
resource.listings[0].price = params[:listing][:price]
resource.listings[0].projector = params[:listing][:projector]
resource.listings[0].cables = params[:listing][:cables]
resource.listings[0].laptops = params[:listing][:laptops]
resource.listings[0].seating = params[:listing][:seating]
resource.listings[0].blinds = params[:listing][:blinds]
resource.listings[0].lighting = params[:listing][:lighting]
resource.listings[0].camcorder = params[:listing][:camcorder]
resource.listings[0].catering = params[:listing][:catering]
resource.listings[0].toilets = params[:listing][:toilets]
resource.listings[0].fire = params[:listing][:fire]
resource.listings[0].internet = params[:listing][:internet]
end
%>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: {class: "form-signin"}) do |f| %>
<%= f.error_notification %>
<%= f.input :name, required: true, autofocus: true, label: false, placeholder: "Name", input_html: {class: "form-control"} %>
<%= f.input :email, required: true, label: false, placeholder: "Email", input_html: {class: "form-control"} %>
<%= f.input :password, required: true, label: false, placeholder: "Password", input_html: {class: "form-control"} %>
<%= f.input :password_confirmation, required: true, label: false, placeholder: "Password Confirmation", input_html: {class: "form-control"} %>
<div style="display:none">
<%= f.fields_for :listings do |listing_form| %>
<%= listing_form.input :title, label: false, placeholder: "Movie Title", input_html: {class: "form-control"} %>
<%= listing_form.input :time, label: false, placeholder: "What time does your movie start?", input_html: {class: "form-control"} %>
<%= listing_form.input :date, label: false, placeholder: "What date", input_html: {class: "form-control"} %>
<%= listing_form.input :seats, label: false, placeholder: "Number of seats", input_html: {class: "form-control"} %>
<%= listing_form.input :venue_name, label: false, placeholder: "Name of venue", input_html: {class: "form-control"} %>
<%= listing_form.input :location, label: false, placeholder: "Address", input_html: {class: "form-control", rows: 5 } %>
<%= listing_form.input :other_info, label: false, placeholder: "Other information", input_html: {class: "form-control", rows: 5} %>
<%= listing_form.input :price, label: false, placeholder: "Ticket Price (£)", input_html: {class: "form-control"} %>
<p> Projector </p>
<%= listing_form.input :projector, required: false, label: false %>
<p> Cable </p>
<%= listing_form.input :cables, required: false, label: false %>
<p> Laptop </p>
<%= listing_form.input :laptops, required: false, label: false %>
<p> Seating </p>
<%= listing_form.input :seating, required: false, label: false %>
<p> Blinds </p>
<%= listing_form.input :blinds, required: false, label: false %>
<p> Lighting </p>
<%= listing_form.input :lighting, required: false, label: false %>
<p> Camcorder </p>
<%= listing_form.input :camcorder, required: false, label: false %>
<p> Catering </p>
<%= listing_form.input :catering, required: false, label: false %>
<p> Toilets </p>
<%= listing_form.input :toilets, required: false, label: false %>
<p> Fire alarm </p>
<%= listing_form.input :fire, required: false, label: false %>
<p> Wifi/internet </p>
<%= listing_form.input :internet, required: false, label: false %>
<% end %>
</div>
<%= f.button :submit, "Sign up", class: "btn btn-primary btn-block" %>
<%= render "devise/shared/links" %>
<% end %>
application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(
:name,
:email, :password, :confirmation_password,
listings_attributes: [
:title, :time, :date, :seats,
:venue_name, :location, :other_info,
:price, :projector, :cables,
:laptops, :seating, :blinds,
:lighting, :camcorder, :catering,
:toilets, :fire, :internet, :image
]
)
end
devise_parameter_sanitizer.for(:account_update) << :name
end
end
any thoughts
Neil
rake routes
Rails.application.routes.draw do
resources :listings
devise_for :users
root 'pages#home'
get 'about' => 'pages#about'
get 'pages/contact'
get 'dashboard' => 'pages#dashboard'
get 'start' => 'listings#listing'
I try to add multipart upload for Carrierwave in my form, but I have two controllers(admin_posts and posts) in one model(post).So I do not understand how to specify this
_form.html.haml
= form_for [:admin, #post] do |f|
= f.fields_for :photos do |photo_fields|
= photo_fields.file_field :image
= f.text_field :title, class: "form-control", placeholder: "Title"
= f.text_area :body, rows: 12, class: "form-control", placeholder: "Body"
.pull-right
= f.submit "Send", class: "btn btn-success"
how fix?
sorry for my English
Try this
= form_for [:admin, #post], url: your_action_path(#post), html: { multipart: true } do |f|