I am building a simple web application in RoR (ruby 2.2.2). So I build a user control page where I can invite different users. Therefore I build a input field and a 'add user' button. When the add user button is clicked, the value of the input field is copied in a extra created input field beneath (via javascript). So everything worked fine, since today. Now everytime when I open the user control page, next to my wanted input field, is a extra input field which is hidden & has the same id as my wanted input field. I really no not know what I did and how I get rid of this field.
So here is my rails code:
<div class='col-md-12'>
<%= form_for #project, html: { class: 'form-horizontal project' } do |f| %>
<% unless #project.persisted? %>
<%= fields_for :invitations do |invitation|%>
<%= invitation.text_field :email, { value: '', name:'invitationfield', class: "typeahead form-control", size: 50, placeholder: "max.mustermann#student.hpi.uni-potsdam.de" } %>
<% end %>
<input type="button" id='AddOneMoreUser' class='btn btn-default btn-xs' value="Add new User">
<div id="invitedUser"> </div>
<% end %>
</div>
<%= f.submit nil, class: 'btn btn-primary' %>
<%= link_to t('.cancel', default: t('helpers.links.cancel')),
projects_path, class: 'btn btn-default' %>
<% end %>
</div>
Here is my JS code:
inviteInitUser = ->
count = 0
$('body').on 'click', '#AddOneMoreUser', ->
console.log 'clicked'
email = $('#invitations_email').val()
$('#invitations_email').val('')
div = document.createElement("div")
div.id = 'user' + count
deleteButton = document.createElement("input")
deleteButton.type = 'button'
deleteButton.value = 'Remove Invitation'
deleteButton.id = 'delete' + count
deleteButton.name = 'deleteInvitationButton' + count
newInvitation = document.createElement("input")
newInvitation.value = email
newInvitation.name = 'invitations[' + count + ']'
div.appendChild(newInvitation)
div.appendChild(deleteButton)
list = document.getElementById("invitedUser")
list.appendChild(div)
count += 1
$(deleteButton).on 'click', -> deleteInvitation()
return
deleteInvitation = ->
deleteButtonCount = $("input[type=button][clicked=true]").prevObject[0].activeElement.id
deleteButtonCount = deleteButtonCount.split('delete')[1]
list = document.getElementById("invitedUser")
elem = document.getElementById("user" + deleteButtonCount)
list.removeChild(elem)
ready = ->
if $('#AddOneMoreUser').length
inviteInitUser()
return
$(document).ready ready
$(document).on 'page:load', ready
So maybe someone knows where my error is . Here is a picture of my input field next to this 'hidden' one:
Related
How to show form on load. Now I can see form fields on click of button (add keyskills). I want all function to be work together.On load i want to see the form and onclick 'add key skill' or 'remove key skill' it should add and remove(now on click of add or remove it is working, but not sees the form on load, only on click of 'add key skill' i can see the form)
$(function()
{
function check_to_hide_or_show_add_key_skill_link()
{
if ($('#key_skills .nested-fields:visible').length == 4) {
$('#key_skills .links a').hide();
} else {
$('#key_skills .links a').show();
}
}
$('#key_skills').on('cocoon:after-insert', function()
{
check_to_hide_or_show_add_key_skill_link();
});
$('#key_skills').on('cocoon:after-remove', function()
{
check_to_hide_or_show_add_key_skill_link();
});
check_to_hide_or_show_add_key_skill_link();
});
My form
Key Skills*
<div id="key_skills">
<%= f.simple_fields_for :key_skills do |key_skill| %>
<div class="nested-fields">
<div class="field">
<%= f.input :name , input_html: { class: 'form-control keySkill', required: true }%>
<%= f.input :relevant_experience, input_html: { class: 'form-control', required: true } %>
<%= link_to_remove_association "Remove Skill", f %>
</div>
</div>
<% end %>
<div class="links">
<%= link_to_add_association 'Add Key Skill', f, :key_skills, class: "links" %>
</div>
</div>
This is my current output now. On click of add key skill i will get form,but i want it on load)
Since you do not provide a lot of information, I have no idea what the name of the top-element is, but let us assume it is a Job and a job has many key_skills.
In your JobsController there is a create method, more or less as follows:
def create
#job = Job.new
end
if you want to immediately add a key-skill to fill in you can just write
def create
#job = Job.new
#job.key_skills.build
end
(which will add an empty key-skill to fill in).
Related issue on cocoon-repository: https://github.com/nathanvda/cocoon/issues/420
I want to have a button for retweets in my web-application. When I press the button I want a form to display in order to add my own text to the tweet.
I implemented this . However I have a problem: all my forms and buttons contain the same id and class, so you can imagine that when I press a button it will display the first form with that id/class instead of the one that it has to show. How can I show the form that I have to show ?
Here are only the parts of the code that contain retweet part(the other parts are not important I think for the problem but I can post them if someone wants me to) :
<button type="buton" id="retweet-button">retweet</button>
<%= form_for(current_user.tweets.build,url: retweet_act_path, method: :post, html: { multipart: true, :class => "retweet_form" }) do |f| %>
<%= f.hidden_field :original_tweet_id, value: tweet.id %>
<%= f.text_area :content, placeholder: "Add a message to the tweet..." %>
<%= f.submit "Retweet"%>
<% end %>
And the JS:
<script type="text/javascript">
$(document).ready(function() {
$('.retweet_form').hide(); //Initially form wil be hidden.
$('#retweet-button').click(function() {
$('.retweet_form').show();//Form shows on button click
});
});
</script>
How can I show the form that I have to show when pressing a certain button ? (I guess I have to have a different ID for every button and form , but how ? )
As I understood from the code, you use one button/form pair per tweet. So that, you are able to use tweet ID to generate form ID. To connect button to appropriate form, use data-* fields.
Generation of forms and buttons on Rails side (NOTE: syntax highlight is broken):
<button type="button" class="retweet-button" data-target-form-id="<%= "#retweet-form-#{tweet.id}" %>">retweet</button>
<%= form_for(current_user.tweets.build,url: retweet_act_path, method: :post, html: { multipart: true, :class => "retweet_form", id: "retweet-form-#{tweet.id}" }) do |f| %>
<%= f.hidden_field :original_tweet_id, value: tweet.id %>
<%= f.text_area :content, placeholder: "Add a message to the tweet..." %>
<%= f.submit "Retweet"%>
<% end %>
And JS:
$('.retweet_form').hide();
$('.retweet-button').click(function() {
var formId = $(this).data('target-form-id');
$(formId).show();
});
I have a rails app with a job model, and on the index page, each job has a form. I need to be able to access the specific form everytime the submit button is selected, so I can take the input for the form and perform some jQuery validation with it.
Here is some of my index.html page.
<ul>
<% #unassigned.each do |job| %>
<li>
<div class="row new-message">
<div class="small-12 columns">
<%= form_for #message do |f| %>
<%= f.text_area :body, {placeholder: 'enter a new message...'} %>
<%= f.hidden_field :job_id, value: job.id %>
<%= f.submit 'send message', class: 'button small radius secondary message-button', "data-job-id" => job.id %>
<div id="message<%= i %>-validation-errors"> </div>
<% end %>
</div>
</div>
<li>
<% end %>
<ul>
And here is the javascript/jQuery where I try to select the input from a specific form
var messages;
messages = function() {
$('.message-button').click(function(e) {
var id = $(this).data('job-id');
messageValidation(e, id);
});
function messageValidation(e, id) {
var body = $('#message_body').val();
var div = '#message' + id + '-validation-errors';
if(body == "") {
$(div).html('');
e.preventDefault();
$(div).html('<small style="color:red"> Message body empty </small>');
}
};
};
$(document).ready(messages);
The index page will have a form for each job, and I want there to be front end validation that checks when the button is submitted, that the body field is not empty. It works fine the first time, but after going back to the index page, and trying to validate a second time var body = $('#message_body').val(); always seems to be empty, whether I put anything in the body or not. I believe it has something to do with the fact that since there is the same form for each job, there are multiple #message_body id's on the same page, and it is selecting the incorrect one. I am new to jQuery and javascript can someone please help me
You cannot have multiple elements with same id on a page. What you can do is add a class identifier to those elements.
In your case, simple set class="message_body" to your elements and select them as shown below:
$('.message-button').click(function(e) {
var id = $(this).data('job-id');
var body = $(this).parent().find('.message_body').val();
messageValidation(e, body, id);
});
To solve this problem I needed to select the child of the form and find the message_body through that.
messages = function() {
$('.new_message').submit(function(e) {
//var id = $(this).data('job-id');
var $this = $(this);
var body = $this.children('#message_body').val();
var id = $this.children('#message_job_id').val();
messageValidation(e, body, id);
});
function messageValidation(e, body, id) {
var div = '#message' + id + '-validation-errors';
if(body == "") {
$(div).html('');
e.preventDefault();
$(div).html('<small style="color:red"> Message body empty </small>');
}
};
};
In my JavaScript I have three Links. The second link is open depends upon the first link id selected. Everything works fine. But After selecting the second link, if i want to change the first link, the second link is still opens for the old id.If I want to change the first link, all the fields should change and opens as per the first link.
var generic_lookup_Enr_Rds_Section2009_selected = function(id, to_s) {
var question_link = $('#question_picker').attr('href');
question_link = question_link.replace(/\?+$/, '');
question_link = question_link + '?columns[enr_rds_section_id]=' + id;
$('#question_picker').attr('href', question_link);
$("#modal_popup").dialog("destroy");
};
var generic_lookup_Enr_Rds_Question2009_selected = function(id, to_s) {
var answer_link = $('#answer_picker').attr('href');
answer_link = answer_link.replace(/\?+$/, '');
answer_link = answer_link + '?columns[enr_rds_question_id]=' + id;
$('#answer_picker').attr('href', answer_link);
$("#modal_popup").dialog("destroy");
};
html
<div class="question">
<%= f.label :Section %>
<%= link_to pro_generic_lookup_data_path("Enr::Rds::Section2009", format: :js), data: {remote: true} do %>
<%= image_tag("Search-icon.gif", border: 0, :alt => "Look up Sections", title: 'Lookup Sections') %>
<% end %>
</div>
<div class="question">
<%= f.label :Question %>
<%= link_to pro_generic_lookup_data_path("Enr::Rds::Question2009", format: :js), data: {remote: true}, id: "question_picker" do %>
<%= image_tag("Search-icon.gif", border: 0, :alt => "Look up Questions", title: 'Lookup Questions', :class => "image_section_search") %>
<% end %>
</div>
Solved it by Javascript using cascading dropdown function by example. Thanks guys.
Rails 3.1. I have the following:
// _form.html.erb
<%= form_for ([#country, #state], :remote => true) do |td| %>
<div id= "state_errors" style="display:none"></div>
<%= td.text_field :position, :id => "next_state" %>
<div class="actions">
<%= td.submit %>
</div>
<% end %>
// global.js
function nextState() {
Array.max = function( array ) {
return Math.max.apply( Math, array );
};
var getLastState = $("input.sortable_state_item_position").map(function() {
return $(this).val();
}).get();
getLastState.push("0");
return Array.max(getLastState) + 1;
}
$("input#next_state").val(nextState());
// states_controller.rb
class StatesController < ApplicationController
before_filter :load
def load
#country = Country.find(params[:country_id])
#states = State.all
end
def create
#state = #country.states.build(params[:state])
#state.save
end
...
end
You will notice that I created a form tag for user to create a record when the submit button is clicked. But I am not sure if I could get rid of the entire form_for and just use a normal a or button to trigger the create because I kinda thing that the entire form is redundant as there is no need for the user to input anything. My javascript enters the value automatically.
Please advise. Many thanks.
In my State controller:
def create
#state = #country.states.build(params[:trip_day])
#state.position = State.where(:country_id => #country.id).maximum(:position).to_i + 1
#state.save
end
Replace the form with this:
<%= link_to "Add new state", country_states_path(#country), :remote => true, :method => :post %>
I removed the nextState() function, entire form.