I am trying to get each candidate belonging to a position have its own page. How do I implement this?
= form_for #vote do |f|
- Position.includes(:candidates).order(:name).each do |position|
= position.name
- position.candidates.each do |candidate|
= image_tag(candidate.image, :size => '50x50') if candidate.image.attached?
= candidate.name
= candidate.info
= f.check_box :c_votes
= f.submit "Submit"
When I use this code it creates a submit button for each candidate on the same page.
Related
I have a form inside a loop that looks like this:
- #templates.each do |template|
tr
td.col-md-2
= template.kind.capitalize
td.col-md-9
= form_tag(add_workout_from_template_path, method: :post) do
= hidden_field_tag 'date', params[:date]
= hidden_field_tag 'athlete', params[:athlete]
= hidden_field_tag 'title', template.title
= hidden_field_tag 'kind', template.kind
= hidden_field_tag 'distance', template.distance
= hidden_field_tag 'duration', template.duration
= hidden_field_tag 'notes', template.notes
= hidden_field_tag 'video', template.video
= submit_tag(template.title, class: 'fake-button')
td.col-md-1
= link_to image_tag('cancel.png'),
template_path(template),
method: :delete
For iteration 2, 3 and 4 it works like a charm. Whenever I click template.title the form executes, hits the controller and does what I want. It prints this list and I can click the titles to add them to a calendar:
However, when I click on the first one (Other 20x ...) it never creates what it should in the controller, it just creates a blank object and redirects. When I try to raise in the controller, it never raises on the first one so for some reason it seems like it is not hitting the controller at all?
What am I doing wrong here?
controller code:
def add_workout_from_template
#athlete = User.find_by(slug: params[:athlete])
#workout = Workout.create!(
user: #athlete,
date: params[:date],
title: params[:title],
kind: params[:kind],
distance: params[:distance] || 0,
duration: params[:duration] || 0,
notes: params[:notes],
video: params[:video]
)
redirect_to team_user_workouts_path(current_user.team,
current_user, athlete: #athlete)
end
I have a submit button on my form:
= semantic_form_for record do |form|
= form.actions do
= form.submit 'Save'
I want to add an icon from FontAwesome, with link_to I can use a block:
= link_to record do
= fa_icon 'save'
Save
But this doesn't work with Formtastic's form.submit.
I also tried:
= form.submit fa_icon('save')
= form.submit fa_icon('save').html_safe
But both renders escaped HTML.
How do I add a font-awesome icon to the <button> tag?
Try using capture to set a label, as in:
- label = capture do
= fa_icon 'save'
Save
= form.button label
This only seems to work for form.button, but not for form.submit. You'll have to use form.button label, type: :submit.
This is a complicated one so I will try to explain as best as I can. I have an attachment model and I am using carrierwave_direct as the gem. Carrierwave_direct requires uploading on one page and then the attributes on another page. The other page it needs to redirect you to is the edit page, however the attributes on the edit page I cannot validate through the model, unless I want the upload to fail(because it won't create an id with validations meant for another page.). I think what I am saying is how would I validate that model attributes have a presence only on the edit page and if the user exits the page without filling in the form, it deletes the model instance. I want it to do this because for my model to work they need it to create the model instance on upload. See my code in the controller below
def edit
#attachment = Attachment.find(params[:id])
end
def report_uploader
#uploader = Attachment.new.delivery
#uploader.success_action_redirect = uploaded_attachments_url
#uploader
end
helper_method :report_uploader
def after_upload
#attachment = Attachment.new(recruiter_id: current_user.id, uploader_name: current_user.first_name + " " + current_user.last_name)
#recruiter = current_user
#attachment.url = #attachment.delivery.direct_fog_url + params[:key]
#attachment.key = params[:key]
#attachment.filename = File.basename(#attachment.key)
#attachment.save
redirect_to edit_attachment_path(id: #attachment.id, :anchor => 'reports')
end
attachment modal
.head
%h2 Add Attachment
.close
%a{:href=>"#",:rel=>"modal:close"} ❎
.content
= direct_upload_form_for report_uploader , :html => { :class => "standard" } do |f|
.fieldgroup.upload
.field
.upload
= link_to "#", class: "btn primary" do
= image_tag 'loader-white.gif', alt: 'Loading', class: 'loading'
Upload Attachment
= f.file_field :delivery, class: 'file_upload', id: 'upload_attachment'
edit page(User is redirected here)
.content
%h2 Add Some Additional Stuff to your Attachment!
= form_for #attachment, html: {class: "standard manage-job"} do |f|
.fieldgroup.upload
.field
Your Attachment Title
%br
%a{href: attachment_path(id: #attachment.id), target: '_blank' }
= #attachment.filename
.field
%select{name: "attachment[attachment_type]"}
%option{value: ""} Select Attachment Type
%option{value: "Type A"} Type A
%option{value: "Type B"} Type B
%option{value: "Type C"} Type C
.field
Description of your Attachment
= f.text_area :description
.actions
= f.submit "Submit Report", class: "btn primary"
= link_to 'Cancel', attachments_path
I have form that create two objects and save them to database.
I want to do next things:
save data in database (booth objects)
validate fields (I have validation in model)
and if validation fail, I want to populate fields with entered data
edit action for this form
Problems:
If I use #report I get:
Called id for nil, which would
mistakenly be 4 error
(can't find object). I have in controller, in encreate action #report = ReportMain.new and in action that render that view.
When I use :report_main (model name) it works, it save data to database, but I can't get fields populated when validation fails.
Questions:
What to do with this two models to make this to work (validation, populating fields, edit)?
Can you give me some advice if approach is wrong?
My view looks like this:
<%= form_for(#report, :url => {:action => 'encreate'}) do |f| %>
<%= render "shared/error_messages", :target => #report %>
<%= f.text_field(:amount) %>
<% fields_for #reporte do |r| %>
<%= r.check_box(:q_pripadnost) %>Pripadnost Q listi
<%= select_tag('nacinpakovanja',options_for_select([['Drveno bure', 'Drveno bure'], ['Kanister', 'Kanister'], ['Sanduk', 'Sanduk'], ['Kese', 'Kese'], ['Posude pod pritiskom', 'Posude pod pritiskom'], ['Kompozitno pakovanje', 'Kompozitno pakovanje'], ['Rasuto', 'Rasuto'], ['Ostalo', 'Ostalo']])) %>
<%= r.text_field(:ispitivanjebroj) %>
<%= r.text_field(:datumispitivanja) %>
<% end %>
<input id="datenow" name="datenow" size="30" type="text" value="<%= #date %>">
<div class="form-buttons">
<%= submit_tag("Unesi izvestaj") %>
</div>
<% end %>
encreate actin in ReportController:
def encreate
#report = ReportMain.new
#reporte = ReportE.new
#reportparam = params[:report_main]
#report.waste_id = params[:waste][:code]
#report.warehouse_id = Warehouse.find_by_user_id(current_user.id).id
#report.user_id = current_user.id
#report.company_id = current_user.company_id
#report.amount = #reportparam[:amount]
#report.isimport = false
#report.isfinished = false
#report.reportnumber = ReportMain.where(:company_id => current_user.company_id, :isimport => false).count.to_i+1
if #report.save
#reporte.report_main_id = #report.id
else
redirect_to(:action => 'exportnew')
return
end
#reporte.vrstaotpada = params[:vrstaotpada]
#reporte.nacinpakovanja = params[:nacinpakovanja]
#reporte.ispitivanjebroj = #reportparam[:ispitivanjebroj]
#reporte.datumispitivanja = #reportparam[:datumispitivanja]
#reporte.q_pripadnost = #reportparam[:q_pripadnost]
#reporte.datumpredaje = #date
if #reporte.save
redirect_to(:action => 'show', :id => #reporte.id)
else
redirect_to(:action => 'exportnew')
end
end
I think your problem in this case is that you use redirect_to instead of render. When you use redirect_to then you lose all the variables from your current action. I would probably do something like this in your encreate action:
if #reporte.save
render :show
else
render :exportnew
end
When you use render then it will use the variables from the current action but the view from the action you send to the render method. So when form_for is called with the #report variable, it is already populated with the values that was sent to encreate. Just make sure that you use the same variable names in the different actions but it looks like you do that already.
I am trying to create a generic set of Submit, Cancel, and Destroy actions for forms. At this point, it appears that everything is working, except that I lose :back functionality then a form reloads due to validation errors. Is there a way to catch the fact that validation has failed, and in that case, keep the request.env['HTTP_REFERER'] or :back value the same without having to edit every controller?
= simple_form_for #announcement do |f|
= f.error_notification
= f.input :message
= f.input :starts_at
= f.input :ends_at
#submit
= f.button :submit
= "or "
= link_to("cancel", url_for(:back))
.right
- if !f.object.new_record?
- resource = (f.object.class.name).downcase
= link_to "destroy", url_for(:action => 'destroy'), :confirm => "Are you sure that you want to delete this #{resource}?", :method => :delete
.clear
.non_input
#post_back_msg
#indicator.inline
= image_tag "indicator.gif"
.inline
= "Please wait..."
.non_input
How about something like a helper method with something like this?
#previous = #previous.blank? ? request.env['HTTP_REFERRER'] : #previous
Then the Cancel button is just:
link_to('Cancel', #previous)
Does this work?
link_to "Cancel", #model.errors.any? ? request.env['HTTP_REFERRER'] : :back