Rails 3 - tinymce doesn't send data - ruby-on-rails-3

In my app use tinymce editor and the problem is that the form send empty textarea. When I remove Tinymce from this form, so everything's working well.
<%= form_tag({ :controller => 'home', :action => 'process_note' }) do %>
<%= hidden_field_tag 'user_id', #user_note.id%>
<div style="width: 800px;">
<div>Your note:</div>
<div>
<%= text_area_tag 'new_note', #user_note.note, :class => 'mceEditor text', :style => 'width: 700px;height: 250px;' %></td>
</div>
<div>
<%= submit_tag "Save" %>
</div>
</div>
<% end %>
Have anyone a similar experience with this behavior? I have already no idea, where could be a problem

Related

Separate Submit Buttons For Forms Displayed With Jquery UI Tabs

Building a Rails 3.1 app Ruby 1.9 with Devise for user authentication.
Since i'm using Devise to authenticate users I used jquery UI tabs display in the users/edit view. I have one tab to display form for user profile settings and and another for user security settings.
Problem is when i click submit to update either profile or security information it attempts to submit the forms for both. I have the submit buttons set up for each tab separately. How can i set this up so that i can submit the forms on each tab individually?
My users/edit.html.erb file
div id="tabs">
<ul id="user-config-tabs">
<li>Profile Settings</li>
<li>Security Settings</li>
</ul>
<div id="profile-tab"> <!-- Start security settings tab info-->
<%= render 'profilesettings'%>
</div>
<div id="security-tab"> <!-- Start Profile settings tab info-->
<%= render 'securitysettings'%>
</div>
</div>
Profile settings PArtial
<h3 style = "text-align: left">Profile Settings</h3>
<div class="row">
<p id="privacy"><%=link_to "privacy policy", privacy_path%></p>
<div class="span6 offset3">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<fieldset>
<legend>General Information</legend>
<div><%= f.label "First Name" %>
<%= f.text_field :name, :autofocus => true %>
<%= f.label "Last Name" %>
<%= f.text_field :last_name, :autofocus => true %>
<%= f.label "Age" %>
<%= f.text_field :age, :autofocus => true%>
<%= f.label "Gender" %>
<%= f.select :gender, User::GENDER_TYPES, prompt: 'Select'%>
</div>
</fieldset>
<div><%= f.submit "Update", class: "btn btn-large btn-primary" %></div>
<% end %>
</div>
</div>
Can't you just let the whole form submit but then only use the information that you want to ?
If you dont want the page to submit then I would use ajax to submit the form and serialize only what you need. For example:
$.ajax({
url: "/post_path",
type: 'POST',
cache: false,
async: false,
data: $("#security_form").serialize(),
success: function(data){
if(data.valid == "true"){
$('#notice').html("Updated succesfully").slideDown();
}else{
$('#errors').html(data.errors).slideDown();
}
}
});
Then you can handle this in the controller.

Rails view text_field existing value vs defaults

I am looking for a more elegant way to accomplish this.
<div class="control-group">
<%= f.label :shoot_date, class: "control-label" %>
<div class="controls">
<% if #shoot.new_record? %>
<%= f.text_field :shoot_date, :class => 'datepicker', :value => Date.today.strftime('%m/%d/%Y'), 'data-behavior' => 'datepicker', :readonly => true %>
<% else %>
<%= f.text_field :shoot_date, :class => 'datepicker', :value => #shoot.shoot_date.strftime('%m/%d/%Y'), 'data-behavior' => 'datepicker', :readonly => true %>
<% end %>
<span class="help-block">Sitting date of this shoot.
</div>
Yeah, just throw it in the controller:
def new
shoot.shoot_date = Date.today
end
You shouldn't have to put it in the edit method or anything; that should happen automatically. You could even put it in the model if you like, but the controller works well.

My form doesn submit with Jquery Validate + Rails Form

I have a form that submits correctly without jquery validate, but does not with jquery validate. Using jquery validate, it correctly shows when the field has less than 6 characters, and the error goes away when it is 6+. However, clicking on the button does nothing. The validate script is:
<% content_for :head do %>
<script type="text/javascript">
$(document).ready(function () {
alert("document ready");
$("#new_hotel").validate({
debug: true,
rules: {
"hotel[name]": {required: true, minlength: 6},
}
});
});
My form is this:
<div class="modal-body">
<%= form_for #hotel do |f| %>
<%= f.label :name %>
<%= f.text_field :name%>
<div class="modal-footer">
<a class="btn" data-dismiss="modal" aria-hidden="true">Cancel</a>
<%= f.submit 'Create Hotel' , :class => 'btn btn-primary', :type => 'submit' %>
</div>
<% end %>

Error after attempting to link_to partials

Using this previous question as a guide, I've attempted to create a ul navigation header above a container that renders partials within the container when clicked. (Hopefully that makes some sense, but it may not be important.) Until the links for the partials are clicked, I have it rendering a partial by default.
However, when I went to click my link_to in hopes of rendering the partial I get the following error:
uninitialized constant ProfileController
I'm using Rails 3. Here's my relevant code:
ProfilesController:
def show_about
#is_on_show_about = true
end
def show_info
#is_on_show_info = true
end
views/profiles/show.html.erb:
<div id="info">
<div id="infoContainer">
<% if #is_on_show_about %>
<%= render :partial => 'show_about' %>
<% elsif #is_on_show_info %>
<%= render :partial => 'show_info' %>
<% end %>
<ul id="info">
<li>
<%= link_to 'About', show_about_path, :remote => true %>
</li>
</ul>
<ul id="settingsLinks">
<li>Advice</li>
<li>
<%= link_to 'Info', show_info_path, :remote => true %>
</li>
</ul>
</div>
<%= render :partial => 'show_about' %>
Routes.rb:
map.show_info 'profiles/:id/info', :controller => 'profile', :action => 'show_info'
map.show_about 'profiles/:id/about', :controller => 'profile', :action => 'show_about'
Can anyone help me fix this and explain what went wrong?
Both of your routes are incorrect.
If your controller is indeed named ProfilesController (plural) then your routes should use :controller => 'profiles', instead of :controller => 'profile'.

Observe_field in Rails 3

I am trying to accomplish an observe_field in rails 3.
In rails 2.3.5 I have :-
<%= observe_field 'query', :frequency => 2,
:update => "search_results",
:url => {:controller => params[:area], :action => "search",:area => params[:area]},
:method=>:get,
:with => "query" %>
Which works fine, checking my text_field_tag "query" and updating my "search_results" every two seconds. This is what I am trying to simulate using prototype.
At the moment I have in a basic application in Rails 3:-
<script>
document.observe("dom:loaded", function() {
$('search').observe('change',
respondToChange());
});
</script>
or
<script>
document.observe("dom:loaded", function() {
new Form.Element.Observer(
'search',
1,
respondToChange()
) });
</script>
Both of which triggers the respondToChange function when the page loads, despite the "dom:loaded", and then does not observes anymore.
Does anyone have any idea of how I might obtain the repeated observer checks on my "search" text_field_tag.
I think I have now solved the problem of doing an ajax call via programming. I will have to check it out in all types of browsers, but at the moment it is working in Firefox and Safari. I have now also moved both javascript "document.observe("dom:loaded" and the function it calls "respondToChange()" to the application head with content_for. All other files remain the same.
In my index.html.erb I now have:-
<h1>Listing homepages</h1>
<div id = "testsearch">
<%=render :partial => 'homepage'%>
</div>
<%= form_tag homepages_path, :method => 'get', :remote => true do %>
<%= label_tag(:search, "Search for:") %>
<%= text_field_tag :search, params[:search]%>
<%= submit_tag "search", :name => nil %>
<%end%>
<%= set_focus_to_id 'search' %>
<% content_for :search_javascript do %>
function respondToChange() {
var pars = 'search=' + $('search').getValue()
new Ajax.Request("<%= homepages_path %>" ,{
method: 'get',
parameters: pars
});
};
document.observe("dom:loaded", function() {
new Form.Element.Observer(
'search',
2,
respondToChange
)
});
<% end %>
<br />
<%= link_to 'New Homepage', new_homepage_path %>
In my application layout file I now have:-
GardenR3
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
<script>
<%= yield :search_javascript %>
</script>
</head>
<body>
<%= yield %>
</body>
</html>
Here is my full code if anyone is interested. This code observes the text_field_tag "search" every 2 seconds, and if there is a change in the value, it triggers a search automatically. The submit button can now be done away with, I think. I might add :autocomplete => "off", :onKeyPress=>"return disableEnterKey(event)") %> to the text_field_tag to disable the return key, not sure.
In my index.html.erb I have:
<h1>Listing homepages</h1>
<div id = "testsearch">
<%=render :partial => 'homepage'%>
</div>
<%= form_tag homepages_path, :method => 'get', :remote => true do %>
<%= label_tag(:search, "Search for:") %>
<%= text_field_tag :search, params[:search]%>
<%= submit_tag "search", :name => nil %>
<%end%>
<%= set_focus_to_id 'search' %> // I have a helper "set_focus_to_id"
<script>
document.observe("dom:loaded", function() { // ensures the page is loaded first
new Form.Element.Observer( // Observes the text_field_tag every 2 seconds
'search',
2,
respondToChange //refrences the function in the Layout <head>
) // on a change in search calls respondToChange
});
</script>
<br />
<%= link_to 'New Homepage', new_homepage_path %>
In my application Layout head I have:
<script>
function respondToChange() {
$('search').up('form').submit() // The ".up finds the form in the DOM"
};
</script
In my controller#index I have:
def index
#homepages = Homepage.search(params[:search]) //".search method is in the Model"
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #homepages }
format.js
end
end
In my Model I have:
def self.search(search_item)
if search_item
self.where('section LIKE ?', "%#{search_item}%") //Handles the ajax call.
else
self.all //Handles the html call on startup.
end
end
In the helper I have:
def set_focus_to_id(id)
javascript_tag("$('#{id}').focus()");
end
In the "_homepage" partial I have:
<table>
<tr>
<th>Id</th>
<th>Section</th>
<th>Link</th>
<th>Description</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for homepage in #homepages %>
<tr>
<td><%= homepage.id %></td>
<td><%= homepage.section %></td>
<td><%= homepage.link %></td>
<td><%= homepage.description %></td>
<td><%= link_to 'Show', homepage %></td>
<td><%= link_to 'Edit', edit_homepage_path(homepage) %></td>
<td><%= link_to 'Destroy', homepage, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<%end%>
</table>
And in the index.js.erb I have:
$('testsearch').update("<%= escape_javascript(render :partial => 'homepage') %>");
If anyone has any comments on how I could improve this, please contact me or say so.
With thanks to "themiddleman" and "agnaki" somewhere out there in the ether, I have solved the problem:
Use respondToChange, not respondToChange()
As the parentheses () execute the function, whereas without () it references it.
$('search').observe('change', respondToChange);
// only triggers when the focus is moved away from the text_field.
new Form.Element.Observer(
'search',
1,
respondToChange
) });
//................................repeatedly checks the text_field,every 1 second, and call the function if there is a any change.