I have a form with multiple buttons, I want one of the buttons to NOT validate the form, specifically required true text box. Here is the button. I do have simple_form gem.
<div class="col-md-2"> <%= f.submit 'Process Custom Amt', class: "btn btn- primary btn-md", name: 'new_order_with_irregular_pmt' %></div>
Thanks in advance
In your controller, instead of:
if #order.save
redirect_to #order
else
render "orders/new"
end
You could use:
if params[:new_order_with_irregular_pmt]
#order.save(validate: false)
redirect_to #order
else
if #order.save
redirect_to #order
else
render "orders/new"
end
end
I would have two different names for each button in the form.
<input type="submit" name="validate" value="Submit" />
<input type="submit" name="dont_validate" value="Submit" />
then I would check in the controller which one was submitted
if params[:dont_validate]
#order.save(validate: false)
else
#order.save
end
Related
We have an application that has many models, two of them are behaving curiously (Candidate and MarketSegment).
Each entity of class Candidate holds a market_segment_id attribute. We also have a form that allows us to edit instance of Candidate and submit/update via a JSON request.
When I try to render a partial that builds the collection of radio buttons with market segment options to be selected for that candidate within the edit form, I'm always getting the last value/market_segment_id of the radio button list in params hash, that is market_segment_id => '35' every time.
We already tried many different approaches, including:
1) Inspected html to confirm if the 'collection_radio_buttons' helper is generating the right values for each button;
2) Built the loop to create each radio button with its respective attributes manually.
<%= render partial: 'shared/forms/market_segments_select', locals: {f: f} %>
ruby/erb partial content that generates radio button list
<div class="w-100 h4 overflow-scroll overflow-x-hidden bn pt3">
<%= f.collection_radio_buttons :market_segment_id, MarketSegment.all, :id, :description_pt do |m| %>
<div class="w-100 mb2 market_segment__radio-group">
<%= m.radio_button %>
<%= m.label %>
</div>
<% end %>
</div>
html generated by the partial
<div class="w-100 h4 overflow-scroll overflow-x-hidden bn pt3">
<input type="hidden" name="candidate[market_segment_id]" value="" />
<div class="w-100 mb2 market_segment__radio-group">
<input type="radio" value="1" name="candidate[market_segment_id]" id="candidate_market_segment_id_1" />
<label for="candidate_market_segment_id_1">Agronegócio e Bioenergia</label>
</div>
<div class="w-100 mb2 market_segment__radio-group">
<input type="radio" value="2" name="candidate[market_segment_id]" id="candidate_market_segment_id_2" />
<label for="candidate_market_segment_id_2">Alimentos</label>
</div>
<div class="w-100 mb2 market_segment__radio-group">
<input type="radio" value="3" name="candidate[market_segment_id]" id="candidate_market_segment_id_3" />
<label for="candidate_market_segment_id_3">Auditoria</label>
</div>
...and so on until market_segment_id = 35
params
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Sgpo39Lo...==", "candidate"=>{"avatar"=>"", ..., "market_segment_id"=>"35", ...}, "commit"=>"Atualizar", "id"=>"8"}
controller action
def update
respond_to do |format|
if #candidate.update(candidate_params)
create_candidate_skill_tags
format.html do
redirect_to admin_candidate_path(#candidate),
notice: 'Candidato atualizado com sucesso'
end
format.json { render json: #candidate, status: :ok }
else
format.html { render :edit }
format.json do
render json: #candidate.errors, status: :unprocessable_entity
end
end
end
end
Regardless of the radio button I select the params always get market_segment_id => '35' (that is the last record of the MarketSegment table.
I have done similar thing. You could do something like this
def create_candidate_skill_tags
new_skill_tags_from_params.map do |skill_text|
#candidate.skill_tags << SkillTag.where(id:
skill_text).first_or_create
end
end
<label for="users"><strong>Your Form</strong></label>
<div class="custom-select form-control pull-right">
<%= form.collection_check_boxes(:market_segment_id, MarketSegment.all, :id,
:description,
checked: #candidate.market_segments.map(&:id)) do |b| %>
<%= b.check_box %> <%= b.label %>
<% end %>
</div>
also you need in your candidate.rb
has_and_belongs_to_many :market_segments, foreign_key: :candidate_id
and in market_segment.rb
has_and_belongs_to_many :candidates
ensure you have :candidate_id column in your db. or any how you did it.
I have a resource Invoice and every invoice has an edit path:
http://localhost:3000/invoices/1/edit
On my index page where I will all my invoices I want to have an input field and a submit button to jump right to the edit page for a certain invoice:
<form class="form-inline" action="???" method="get">
<div class="form-group">
<input type="text" class="form-control date" placeholder="Invoice ID" name="id" >
</div>
<button type="submit" class="btn btn-info">Show</button>
</form>
How do I have to define the action so that if I enter for example 1 and click on Show it goes right to my edit method?
I think you're looking for something like this in your html
<%= form_tag find_invoice_url do |f| %>
<%= f.text_field :invoice_number =>
<%= f.submit "Search" =>
<= end =>
then you would need to add a route
post '/invoices/find_invoice', to: "invoices#find_invoice",
as: "find_invoice"
then in your controller something like
def find_invoice
invoice = Invoice.find_by_id(params[:invoice_number])
if invoice
redirect_to invoice_url(invoice.id) #whatever your edit route is
else
redirect_to root_url #anywhere really
end
end
if you are iterating through all the invoices, you wouldn't need an input field.
assuming that you have an invoice obhect, you would just do something like
<%= form_tag edit_invoice_path(invoice), method: "get" do -%>
<%= submit_tag "Show", class="btn btn-info" %>
<% end %>
You may want the button to say edit instead - that's more of a ux "principle of least surprise" issue
I want to print a simple message as an alert like after a user write his email click on the subscribe button.
Your email is saved, you'll be contacted for a beta very soon!
This is my view:
<form action="/welcome/subscribe" date-remote="true">
<div>
<div id="bottomm">
<input id="bottom" type="text" name="email" placeholder="email" />
</div>
<div>
<input type="submit" value="Ok" class="fit" />
</div>
<%= alert %>
</div>
</form>
This is the subscribe method in my controller :
def subscribe
#test = Test.new
#test.email = params['email']
#test.save
redirect_to root_url, notice: "Your email is saved, you'll be contacted for a beta very soon!"
end
But nothing is showing. I saw some people with the same issue as mine on stack but I tried the solutions but it didn't worked.
I tried to do these but none worked for me.
flash.keep
flash[:notice] = "..."
flash.now[:notice] = "..."
flash[:alert] = "..."
flash.now[:alert] = "..."
and redirecting after
Do you have a block either in application.html.erb or whatever your root view is to actually display the flash, like so:
<% if notice.present? %>
<p><%= notice %></p>
<% end %>
If not, that could be the problem.
I have a list of group and I want to edit a single group name via modal. I use edit.js.erb file and bootstrap modal.
There are two cases, both wrong.
First:
Using "remote: true" in Partial and link_to(in single row file)
Then modal is displaying, but after SAVE button clicked always is rendering again and never close. (I don't know why)
Second:
Delete remote:true from partial.
Then modal is displaying, I can even save the changes. But when I type wrong name (blank or too long), line "render 'edit'" from Controller crash. (error: "Missing template groups/edit, application/edit...". I think rails doesn't know about edit.js.erb then, but how to repair it?
Have you got any ideas about this situation ?
Controller
def edit
#group = current_user.groups.find_by(id: params[:id])
respond_to do |format|
format.js # actually means: if the client ask for js -> return file.js
end
end
def update
#group = current_user.groups.find_by(id: params[:id])
if #group.update_attributes(group_params)
flash[:success] = "Nazwa grupy została zmieniona"
redirect_to groups_path
else
render 'edit'
end
end
Partial for displaying form_for in modal
<%= form_for(#group, remote: true) do |f| %>
<div>
<%= render 'shared/error2_messages', object: f.object%>
<p>
<%= f.label :name, "Nazwa" %>
<%= f.text_field :name, class: 'form-control'%>
</p>
</div>
<%= f.submit yield(:button_name), class: "btn btn-primary" %>
<% end %>
edit.js.erb file
<% provide(:button_name, 'Zapisz zmiany') %>
$('.modal-title').html("Edytuj nazwę grupy");
$('.modal-body').html("<%= escape_javascript( render partial: 'layouts/group_data_form', locals: {group: #group} ) %>");
$('#myModal').modal();
Single row in a list
<li id="group-<%= group.id %>" class="list-group-item">
<span class="group-name"><%= group.name %></span>
<%= link_to edit_group_path(group.id), remote: true, :class => "edit-option btn" do %>
<i class="fa fa-pencil-square-o fa-2x" ></i>
<% end %>
<%= link_to group, method: :delete, data: { confirm: "Na pewno chcesz usunąć tę grupę?" }, :class => "delete-option btn btn-danger" do %>
<i class="fa fa-trash-o" > usuń</i>
<% end %>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Zmień nazwę grupy</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Anuluj</button>
</div>
</div>
</div>
</div>
This looks like a follow-up to your question of yesterday.
I won't say both your approaches are wrong, I'll say you are conbining two right approaches in such a way...
(1) since you used js to send save (with the use of remote: true on your form), all you need to do is to close the modal on successful save as will be defined from update.js.erb (just like you defined an edit.js.erb for the format.js in edit) as follow:
#update.js.erb
<% if #group.errors.empty? %>
$('.modal-body').html("");
$('#myModal').modal('hide');
$("#notice").html("Group was successfully updated.");
<% else %>
$("#notice").html("Error! Group not updated.");
<% end %>
#controller update action
def update
#group = current_user.groups.find_by(id: params[:id])
#group.update_attributes(group_params)
respond_to do |format|
format.html
format.js
end
end
What you essentially did here is to check if there is no error in the #group object(this will be true if #group got saved), then emptied your Modal and then hid it from view.
(2) When you removed remote: true you are no longer using js, so when you have a wrong name, the update does not work, and the render :edit portion of your update method kicks in. However, your edit action only specifies format.js, So what you may have to do in this case is to add format.html if you want to call with html.
def edit
#group = current_user.groups.find_by(id: params[:id])
respond_to do |format|
format.js # actually means: if the client ask for js -> return file.js
format.html
end
end
Note however that this may not open your modal, it will go to the edit_group_path of your app, which is the /groups/:id/edit
I have been trying for quite a while to get Paperclip up and running on my website and have followed the step-by-step process outlined on github multiple times and it still won't work. I really need to get this up and running as soon as possible. When I run the code through localhost, I get the message:
undefined local variable or method `survey' for #<SurveysController:0x007ff09e82aa50>
app/controllers/surveys_controller.rb:18:in `survey_params'
app/controllers/surveys_controller.rb:8:in `create'
Here is my code
class SurveysController < ApplicationController
before_filter :new, :create, :survey_params, :owners, :seeker, :survey_params, :idcheck
def new
#survey = Survey.new
end
def create
#survey = Survey.new(survey_params)
#if #survey.save
#redirect_to root_path, notice: "Survey is created."
#else
#render 'new'
#end
end
private
def survey_params
survey_params = params.require(survey).permit(:avatar)
end
private
def owners
#survey = Survey.new(survey_params)
#survey.user_id=current_user.id
#survey.save
end
def seeker
#survey = Survey.new
#survey.save
end
private
def idcheck
end
end
<p>
<%= f.file_field :avatar %>
</p>
<p><%= f.submit "Submit" %></p>
<!---</div>-->
<div class="circle_wrapper roommates_both">
<div class="dark_circle" ></div>
<div class="light_circle" ></div>
<div class="light_circle" ></div>
</div>
<div class="housing_only">
<div class="submit_button">
<input id="submit_room_only" class="nextbutton one pages submitbutton" type="submit" name="submit" value="Submit!" disabled/>
</div>
</div>
</form>
<% end %>
<form action="FileUpload.php" method="post" enctype="multipart/form-data">
<label for="upload">Upload your Profile Photo:</label><br />
<input type="file" name="upload" id="upload"><br/>
<input type="submit" name="submit" value="Upload">
<div class="roommates_both">
<input id="next1" class="nextbutton one pages" type="submit" name="submit" value="Save and continue >"/>
</div>
</form>
What can I do to finally get this working?
Change survey_params to (missed colon):
def survey_params
survey_params = params.require(:survey).permit(:avatar)
end
And you have 3 private, remove last 2 (not an error, but bad smell).