I have following situation:
I've got two models
a Risk model with the attributes name, risk_class_id, description, level
a Risk_Class model with the attributs name and description
A Risk has one Risk Class and a Risk Class has many Risks. This association works fine. I can assign a Risk Class to a Risk. To create a Risk I use the following form:
<%= form_for #risk, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :name, :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<%= f.label :risk_class_id, :class => 'control-label' %>
<div class="controls">
<%= collection_select( :risk, :risk_class_id, RiskClass.all, :id, :name, :prompt => true ) %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description, :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<%= f.label :level, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :level, :class => 'text_area' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
</div>
<% end %>
Now i would like to have that the textarea field for the description gets prepopulated based on the selected risk class, as a risk class has a description.
Thankful for any help!
EDIT
I've added following javascript code to the form and edited the collection select the following way:
<%= javascript_tag do %>
function risk_class_selection(riskclassId){
window.alert(riskclassId);
}
<% end %>
<div class="control-group">
<%= f.label :risk_class_id, :class => 'control-label' %>
<div class="controls">
<%= collection_select( :risk, :risk_class_id, RiskClass.all, :id, :name, {:prompt => true} , {:onchange => "risk_class_selection(this.value)"}) %>
</div>
</div>
SOLUTION:
in case anyone is interested i followed the way grotori described and my solution looks now like this:
I've got this risk.js file:
function risk_class_selection(riskclassId){
if (riskclassId != ""){
$.getJSON("/risk_classes/"+riskclassId+".json", function(data){
if ( $('#risk_description').val() == "" ){
$("#risk_description").val(data.description);
}
else{
if ( confirm('Are you sure you want to change the risk description?') ){
$("#risk_description").val(data.description);
}
else{
}
}
}
)
}
else{}
}
And in the Risk Form i've got the first div with the onchange event and the second where the textarea should be prepopulated:
<div class="control-group">
<%= f.label :risk_class_id, :class => 'control-label' %>
<div class="controls">
<%= collection_select( :risk, :risk_class_id, RiskClass.all, :id, :name, {:prompt => true} , {:onchange => "risk_class_selection(this.value)"}) %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description, :class => 'text_area' %>
</div>
</div>
You must bind an onchange event to your select field that will post back to your server the user's selection and respond with javascript that will re-render the textarea with the desired value you want.
You can do it in controller when preparing the object #risk. Something like this:
#risk = Risk.new(description: "blah blah")
Then the form will have this value with form_for tag.
Related
I have a rails app where user can assign task to another user by creating task action. At the moment I have the f.collection_select, but as the number of users grows it won't be a good solution. I would like to change it into kinda search form. My problem is that I don't see how I can do this. I have other search forms, but those are for show/index action. How can I do this for the create action?
here is my current code:
<%= form_for ([#user, #task]) do |f| %>
<%= render 'layouts/error_messages', object: f.object %>
<div class="field form-group">
<%= f.label :executor_id %>
<%= f.collection_select(:executor_id, User.all, :id, :email, class: "form-control") %>
</div>
<div class="field form-group">
<%= f.label :content %>
<%= f.text_area :content, class: "form-control" %>
</div>
<div class="field form-group">
<%= f.label :deadline %>
<%= f.date_select :deadline, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Create Task", class: 'btn btn-primary', "data-sid" => current_user.id, "data-rip" => :executor_id %>
</div>
<% end %>
I want make a helper how in this example
def textForm()
'<div class="form-group">
<%= f.label :nombre, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.text_field :nombre, :class=> "form-control"%>
</div>
</div>'.html_safe
end
but... when I call this helper with <%= textForm %> just print html syntax.
apparently I need make something as "pre-render". Do you have any ideas?
Partial
As mentioned in the comments, you'll be better using a partial to sort this out:
#app/views/controller/_your_partial.html.erb
<div class="form-group">
<%= f.label :nombre, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.text_field :nombre, :class=> "form-control"%>
</div>
</div>
This will allow you to call the partial as follows:
<%= render partial: "controller/your_partial" %>
--
Helper
If you want to call HTML directly from a helper, you should probably look at using the raw method:
<%= raw textForm %>
and then
#app/helpers/application_helper.rb
def textForm()
'<div class="form-group">
<%= f.label :nombre, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.text_field :nombre, :class=> "form-control"%>
</div>
</div>'
end
Here is what I have
I have a method like this in my bookmarks_controller.rb
def fetch
#result = LinkThumbnailer.generate(params[:url])
end
I have the following form fields to create new bookmarks
url
title
description
thumbnail
tags
So my new.html.erb looks like this
<%= form_for(#bookmark, :html => {class: "panel-body"}) do |f| %>
<% if #bookmark.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#bookmark.errors.count, "error") %> prohibited this bookmark from being saved:</h2>
<ul>
<% #bookmark.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="block">
<%= f.label :url, :class => 'control-label' %>
<%= f.text_field :url, :class => 'form-control', :placeholder => 'URL' %>
</div>
<div class="block">
<%= f.label :title, :class => 'control-label' %>
<%= f.text_field :title, :class => 'form-control', :placeholder => 'Title' %>
</div>
<div class="block">
<%= f.label :description, :class => 'control-label' %>
<%= f.text_area :description, rows: 6, :class => 'form-control', :placeholder => 'Description' %>
</div>
<div class="block">
<%= f.label :thumbnail, :class => 'control-label' %>
<%= f.text_field :thumbnail, :class => 'form-control', :placeholder => 'Thumbnail url' %>
</div>
<div class="block">
<%= f.label :tag_list, :class => 'control-label' %>
<%= f.text_field :tag_list, :class => 'form-control', :placeholder => 'Tags (separated by commas)' %>
</div>
<div class="actions">
<%= f.submit :class => "btn btn-info" %>
</div>
<% end %>
I would like to have a button called Fetch right next to url field.
When user click Fetch button, I would like to populate the form fields using ajax.
I viewed rails documentation. It says to add remote: true attribute to the form.
But I wanna use ajax function only when the user click fetch button.
Can someone help me with this?
Thankyou
You can generate an object using the LinkThumbnailer gem and than convert it to json
with this action:
def fetch
#result = LinkThumbnailer.generate(params['given_url'])
respond_to do |format|
format.json do
result.to_json
end
end
end
And in your view you could use the Jquery getJSON method to access the #fetch action and pass the given url as a parameter:
<script type="text/javascript">
require(['jquery'], function($) {
$('.fetch-button-class').change(function() {
var given_url = $('.url-class').val();
$.getJSON('<%= fetch_action_url %>', {'given_url': given_url }, function(obj) {
$('.title-class').val(object.title);
$('.description-class').val(object.description);
});
});
});
</script>
I currently have this sign in form in a rails 4 app in which I use the form_tag helper. I'm need to add an id. I tried using <%= form_tag( :html => {:id => 'login_form'} ) do %> but I get an error.
<%= form_tag do %>
<div class="form-group">
<%= label_tag :name, 'Username:', class: "control-label" %>
<div class="controls">
<%= text_field_tag :name, params[:name], class: "form-control", size: "30", tabindex: "1" %>
</div>
</div>
<div class="form-group">
<%= label_tag :password, 'Password:' %>
<div class="controls">
<%= password_field_tag :password, params[:password], class: "form-control", size: "30", tabindex: "2" %>
</div>
</div>
<div class="form-group">
<div class="controls">
<%= submit_tag "Sign in", class:"btn btn-large", tabindex: "2" %>
</div>
</div>
<% end %>
<%= form_tag('/login', id: "login-form") do %>
I've got this rails form partial:
<%= form_for #ptab, :remote => true, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field', :placeholder => 'Enter a name...' %>
</div>
</div>
<div class="control-group">
<%= f.label :address, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :address, :class => 'text_field', :placeholder => 'Paste a link...' %>
</div>
</div>
<div class="control-group">
<%= f.label :sequence, :class => 'control-label' %>
<div class="controls">
<%= f.number_field :sequence, :class => 'number_field', :placeholder => 'Enter an order number...' %>
</div>
</div>
<div class="control-group">
<%= f.label :gtab_id, :class => 'control-label' %>
<div class="controls">
<%= f.number_field :gtab_id, :class => 'number_field' %>
</div>
</div>
<div class="modal-footer">
<%= f.submit nil, :class => 'btn btn-primary' %>
<!-- <%= link_to t('.cancel', :default => t("helpers.links.cancel")), ptabs_path, :class => 'btn' %> --> <!-- this was the original cancel button -->
<a data-dismiss="modal" class="btn" href="#">Close</a>
</div>
<% end %>
I would like to turn the field in the last .control-group into a hidden field, but I still want it to be submitted to the controller.
What should I change this to :
<div class="control-group">
<%= f.label :gtab_id, :class => 'control-label' %>
<div class="controls">
<%= f.number_field :gtab_id, :class => 'number_field' %>
</div>
</div>
In order for it to not show in the rendered form but still be submitted with the form?
Use a hidden_field ? Or did I miss something ?
<%= f.hidden_field :gtab_id %>
And of course remove the divs around it as well as the label. hidden_field are submitted as regular fields, no worries