I am trying to access the value of an element in a different form. More specifically I have two forms, one normal html form which submits the information, and an ajax form that updates a field in the database. I want the ajax form to take the value of an element in the normal form, but I have no idea how to accomplish that. Here is my current setup:
Message View
<%= form_for #message do |f| %>
<%= f.text_field :to %>
<%= f.text_area :body %>
<%= f.submit "Send Message" %>
<% end %>
<%= form_for :save, :url => save_message_path(), :remote => :true do |f| %>
<%= f.submit "Save" %>
<% end %>
Message Controller
def save
account.message = params[:body]
end
But the :body param is in the other form so it sets account.message to nil because the ajax form did not have a body parameter. I would like it so the ajax form can submit the value of the body element.
Since the form is edited on the client side, you'll have to write this functionality in javascript.
It is relatively straightforward to copy the value of the message body into a hidden field and submit the Ajax form. This would be most easily accomplished by adding an id your ajax form as follows:
<%= form_for :save, :url => save_messages_path(), :remote => true, :id => 'ajaxform' %>
<%= hidden_field_tag 'hidden_message' %>
Then in javascript you could do the following:
$(document).ready(function(){
$("#ajaxform").submit(function() {
$("#hidden_message").val($("#message_body").val());
return true;
});
}
You can add a hidden field body in your ajax form, and use an onsubmit event to populate it with the value of the body field from the normal form before the ajax request is sent. It would look more or less like this:
<%= form_for #message do |f| %>
<%= f.text_field :to %>
<%= f.text_area :body, :class => 'main_body_field' %>
<%= f.submit "Send Message" %>
<% end %>
<%= form_for :save, :url => save_message_path(), :remote => :true, :id => 'ajax_form' %>
<%= f.hidden_field :body, :class => 'hidden_body_field' %>
<%= f.submit "Save" %>
<% end %>
javascript (with jQuery):
$(function() {
$("ajax_form").submit(function() {
// copy body value from first form to the second one
$('.hidden_body_field').val($('.main_body_field').val());
return true;
}
}
Related
I can send the param x=y in a form with a hidden field like this
<%= form_with(url: '/search', method: 'get') do %>
<%= text_field_tag(:query) %>
<%= hidden_field_tag(:x, 'y') %>
<%= submit_tag("Search") %>
<% end %>
Is it possible to send params in the URL? The following doesn't work
<%= form_with(url: '/search?x=y', method: 'get') do %>
<%= text_field_tag(:query) %>
<%= submit_tag("Search") %>
<% end %>
As you can read here, the query string that you might have in a HTML form action attribute will not taken into account.
Translated into your form_for problem, it means that the query string that you have in your url: are not taken into account.
Hence, your hidden_field_tag is the right solution for this problem.
My problem is that I have a form that like:
<select name="Users">
<option value=""selected>Select user to add</option>
<% notShareWith.each do |user| %>
<option value=<%user.id%>><%=user.name+" "+user.email%></option>
<% end %>
</select>
<% form_for #imageuser do |f| %>
<%= f.hidden_field :user_id ,:value => 5 %>
<%= f.hidden_field :image_id ,:value => #image.id %>
<% end %>
My drop down menu works fine and it list the users that have access. I can revoke access and everything. I have no clue how to know what option is selected from the drop down menu or how to submit that to the image users form. I am using:
<% form_for #imageuser do |f| %>
<%= f.hidden_field :user_id ,:value => 5 %>
<%= f.hidden_field :image_id ,:value => #image.id %>
<% end %>
to submit the user that is selected but I need to know what the value of the selected item is so I can pass said value into:
<%= f.hidden_field :user_id ,:value => 5 %>
Thanks again for the help.
You can also use options_from_collection_for_select
Something like this should work
<% form_for #imageuser do |f| %>
<%= f.select :user_id, options_from_collection_for_select(notShareWith, 'id', 'name'), prompt: 'Select user to add' %>
<%= f.hidden_field :image_id ,:value => #image.id %>
<% end %>
Use collection_select helper instead, something like this:
<%= f.collection_select(:user_id, notShareWith.map { |user| user.id }, :id, :id, :prompt => 'Select') %>
Then, grab the selected value like: params[:user_id]
To fill the selected id of user in your form you have to do several steps
First, add id attribute in your option
<select id="user_id" name="Users">
<option value=""selected>Select user to add</option>
<% notShareWith.each do |user| %>
<option value=<%user.id%>><%=user.name+" "+user.email%></option>
<% end %>
</select>
Then, you can use javascript to pass into imageuser form:
<script type="text/javascript">
$( document ).ready(function() {
$("#user_id").change(function(){
selected = $(this).val();
$("#imageuser_user_id").val(selected);
});
});
</script
NOTE:
You'd better use select_tag helper to create select option
<%= select_tag 'user_id', options_for_select(notShareWith.collect{ |u| ["#{u.name} #{user.email}", u.id]}, prompt: "Select user to add") %>
I have a pair of radio buttons that I want to pre-assign a checked value to only my new action. Right now I'm conditionally rendering two partials. One partial that has radio buttons with checked attributes and the other with not attributes at all:
<%= form_for([#restaurant, #dish_review], url: :restaurant_dish_reviews) do |f| %>
<% if action_name == "new" %>
<%= render "status_buttons_checked", f: f, dish: #dish %>
<% else %>
<%= render "status_buttons", f: f %>
<% end %>
<% end %>
_status_buttons_checked
<div class="field">
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on?(dish) %>
<%= f.label :status, value: :upvoted %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on?(dish) %>
<%= f.label :status, value: :downvoted %>
</div>
_statsus_buttons
<div class="field">
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on?(dish) %>
<%= f.label :status, value: :upvoted %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on?(dish) %>
<%= f.label :status, value: :downvoted %>
</div>
I was wondering if there was any way in Rails where I can insert the conditional in the radio_button parameter instead of creating two partials. I'd like to something similar to what's show below but run into a template error:
<%= f.radio_button :status, :downvoted, if action_name == "new" current_user.voted_down_on?(dish) %>
When using form_for, the form methods you use are automatically populated with the appropriate data for your attributes. Whilst I don't know if this works with the checked value, it means that if you have the following:
<%= form_for #user do |f| %>
<%= f.text_field :name %>
<% end %>
... :name will be populated from your #user object (if it's new, no data will be inserted).
--
This means that if you're using form_for, you should be able to populate the checked value with the data passed to the view:
<%= form_for [#restaurant, #dish_review] do |f| %>
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on? #dish %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on? #dish %>
<% end %>
I don't see what you're trying to achieve from your partials (they're both the same) -- but if you wanted to create "checked" on conditions, you'd be able to use the following:
<%= checked = action_name == "new"
<%= f.radio_button :status, :downvoted, checked: checked %>
This will set the value as "true" or "false" depending on whether the action is new or not.
I know that if you just type something like <button>Something</button> outside a form_for in rails, it will create a useless button.
But I want to create buttons within this form_for to be handled by JavaScript.
Is there a way to create it?
This will create useless buttons that can be handled by JavaScript.
Plain HTML:
<input type="button" onclick="alert('Hello.')" value="Click Here" />
Rails:
<%= submit_tag "Click Here", :type => 'button', :onclick => 'alert("Hello.")' %>
If you're not looking for Rails to use it, why not just use the plain html inside the form_for?
<%= form_for #record do |f| %>
## inputs ##
<button>Something</button>
<%= f.submit %>
<% end %>
Check out this answer: How do I create multiple submit buttons for the same form in Rails?
<% form_for(something) do |f| %>
..
<%= f.submit 'A' %>
<%= f.submit 'B' %>
..
<% end %>
I followed the source code form the rails3-devise-bootstrap-example to get my devise-login-page (that normally - without AJAX - works fine) in a popup window.
When I click on the login link nothing happens. The logfile tells me
Started GET "/facebox/fb_login" for 127.0.0.1 at 2014-01-09 19:15:0
Processing by FaceboxController#fb_login as JS
Rendered devise/shared/_links.erb (3.0ms)
Rendered devise/sessions/_new.html.erb (1155.0ms)
Rendered facebox/fb_login.js.erb (1251.0ms)
Completed 200 OK in 1509ms (Views: 1508.0ms | ActiveRecord: 0.0ms)
But no login-pop-up-window appears.
Here is the login link in my index.html.erb file
<%= link_to 'Login', fb_login_path, :remote => true %>
That starts my fb_login.js file
$.facebox('<%= escape_javascript(render :partial => 'devise/sessions/new') %>')
and should open _new.html.erb that looks like this:
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="form-inputs">
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password, :required => false %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
but it doesn't. What went wrong?
I got the answer by myself . Under assets/javascripts I had to define
jQuery ->
$('a[rel*=facebox]').facebox()
# facebox - make modal
$(document).bind "loading.facebox", ->
$(document).unbind "keydown.facebox"
$("#facebox_overlay").unbind "click"
in a js.coffee file.