I am trying to build a simple message relay system from scratch. My form looks like this:
<%= simple_form_for #message do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Your message" %>
<%= f.input :target_user_id, label: "Who are you sending your message to?" %>
<%= hidden_field_tag 'user_id', current_user.id %>
<%= hidden_field_tag 'sender_email', current_user.email %>
<%= f.button :submit %>
<% end %>
I am trying to pass "current_user.email" to the message model, where "Message.sender_email" is a string, but I am getting "nil" when I look at the created Message in the console. All the other fields do get passed though.
Just add this line to your controller:
#message.sender_email = #user.email
in the message create method.
Related
I have a simple_form form setup and it will show inline errors fine. I have had problems with some users not seeing these errors and have had requests for a clear enumeration at the top of the very long form. I've used the code setup from the Rails Tutorial:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Which is called as a partial (loaded from app/views/application) onto the form (object = f.object). It does not render, but I'll see the failure in the rails console in development. Any ideas why this won't show errors in this fashion? Is this some failure related to Turbo/Hotwire (there is a controller watching the text area to resize if content overflows)?
A much shorter form that also uses the same error partial (and exhibits the same behavior):
<%= simple_form_for [:admin, #annotation] do |f| %>
<%= render 'error_messages', object: f.object %>
<%= f.input :abbreviation,
input_html: { minlength: 1 } %>
<%= f.input :name,
input_html: { minlength: 1 } %>
<%= f.input :description,
as: :text,
input_html: { data: { controller: "textarea-autogrow"} },
input_html: { minlength: 1 } %>
<fieldset>
<legend>Used For</legend>
<%= f.input :oa,
as: :boolean,
label: "OA (Lodge, Chapter, Section) Issues" %>
<%= f.input :council,
as: :boolean,
label: "Council (CSP, JSP, etc) Issues" %>
<%= f.input :camp,
as: :boolean,
label: "Camp Issues" %>
</fieldset>
<%= f.button :submit,
data: { disable_with: "Please wait..." } %>
<% end %>
With some help from Michael Koper, we were able to sort this out. The controller methods were missing status: :unprocessable_entity on the format.html statements. So changing:
format.html { render action: "new"}
to
format.html { render action: "new", status: :unprocessable_entity }
Solved this issue.
I have a simple_form with an image uploader, which is connected to CarrierWave:
<%= simple_form_for #house do |f| %>
<%= f.input :price %>
<%= f.input :town %>
<%= f.input :description %>
<%= f.input :bedrooms %>
<%= f.input :bathrooms %>
<%= f.input :url, label: "URL" %>
<%= f.input :rating %>
<%= f.input :remote_image_url %>
<%= f.file_field :image %>
<%= f.button :submit, "Add House" %>
<% end %>
For some reason the two fields
<%= f.input :remote_image_url %>
<%= f.file_field :image %>
are storing to the exact same column in my database, which is named "image". I don't understand why.
By carrierwave, you can either upload file/picture from your machine directly by <%= f.file_field :image %>, or from the link on remote server using helper method 'remote_image_url' . This naming convention remote_yourimagefield_url is important, as carrierwave will come to know by this naming structure to grab files from the remote server. As :remote_image_url points to the same column as :image, therefore they are stored in the same column
I am lost, I do not know what I'm doing wrong! I have 4 radio buttons and a hidden field (value = "1"). When you click on the second radiobutton, the value of the hidden field changes to 2 and so on. This works fine with a js function.
Different divs will be showed when a different radiobutton is selected. Now, when I'm trying to get the value of the hidden field in my controller it always returns nil.
Here's the code:
view:
(radiobuttons, hiddenfield and one div)
<div>
<%= form_tag patients_path do %>
<%= radio_button_tag 'searchRBN', 'patient', true, :onchange => "checkRadioButton()" %>
<%= label_tag :byPatient_patient, "Patient" %>
<%= radio_button_tag 'searchRBN', 'staff', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byStaff_staff, "Staff" %>
<%= radio_button_tag 'searchRBN', 'ocmw', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byOcmw_ocmw, "OCMW" %>
<%= radio_button_tag 'searchRBN', 'mutuality', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byMutuality_mutuality, "Mutuality" %>
<%= hidden_field_tag :hidden_one, "1" %>
<% end %>
</div>
<div id="searchByPatient">
<%= form_tag patients_path, :method => 'get' do %>
<p>
<%= text_field_tag :search1, params[:search1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
controller:
def index
#staff_all = Staff.all
#ocmw_all = Ocmw.all
#mutuality_all = Mutuality.all
debugger
if params[:hidden_one] == '1'
#patients = Patient.searchByName(params[:search1])
elsif params[:hidden_one] == '2'
#patients = Patient.searchByStaff(params[:search2])
else
#patients = Patient.all
end
end
It's because you have two forms. When you submit the second form it won't send the fields of the first form. If you put everything in one form it will work as expected.
Use only single form:
Also as a workaround use two submit tag in a single form:
differentiate both the action with params[:action]
For Example:
<%= form_for :attachment_metadata, :url=>{:action=>'delete_files'}, :html=>{:onsubmit=> "return confirm('Are you sure, you want to delete selected files?');",:multipart => true} do |f| %>
<table>
..........Some stuff here..........
</table>
<%= submit_tag 'Reprocess', :class =>'button' %>
<%= submit_tag 'Remove', :class =>'button' %>
<% end %>
params[:commit] can differentiate the actions of two submit tags.
#action = params[:commit]
it gives #action value as "Reprocess" if your click the Reprocess button and gives "Remove" value if you click the Remove button,
Then you will get your values.
I want to pass selected :ammount value to my controller as :quantity. What i am doing wrong?
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= button_to 'Add to cart', line_items_path(:product_id => product.id, :quantity => :ammount) %>
You should use form here to pass the data to controller`s action:
<%= form_tag line_items_path(:product_id => product.id) do %>
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= submit_tag 'Add to cart' %>
<% end %>
All that you did in your sources is just passing symbol :ammount to a controller`s action as a quantity param.
I am using semantic_form_for and I wonder if there is such attribute as :complete then upon successful creation, the form would alert something so that the user would have to click on "Okay" to proceed?
Do you mean confirmation before submit?
you could use something like the following:
<%= semantic_form_for #user do |f| %>
...
<%= f.buttons do %>
<%= f.commit_button :button_html => {:confirm => 'Are you sure?'} %>
<% end %>
<% end %>
My solution is like the following and it works well:
<%= semantic_form_for #post do |f| %>
...
<%= f.action :submit, :button_html => { 'data-confirm'.to_sym => 'Are you sure?' } %>
<% end %>