I have two forms in my rails app. They both exist in separate tabs and I when I submit one form I want the data in the other form to be saved as well. How should I do that? Or Is there a better way of doing this instead of using two separate forms? Is there a better way to spread a long form into multiple tabs and when I press submit all the data from all the tabs should reach my action. Thanks.
You can expand a single form to cover all elements of both forms.
This is perfectly ok, as long as the form tags fit validly into the X/HTML.
<form action='action1'>
<!-- All elements from both forms, plus tabs, etc. -->
</form>
The only thing to consider is if there will ever be another form inside that area that will need to go to another action. For example, if you add a third tab, in between the other two, that will have a different action.
You don't want to end up like:
<form action='action1'>
<!-- elements from the combined forms -->
<form action='action2'>
<!-- elements for a totally different form, not valid inside another form -->
</form>
<!-- more elements from the combined forms -->
</form>
In that case, it would be better to consolidate the two forms at submit time using JavaScript.
jQuery (jquery.com) would make this pretty easy. For example, you could serialize both forms, and then concatenate them and send the result to the server via post or get.
See http://docs.jquery.com/Ajax/serialize.
There might be better ways to do this, but I can't think of any off the top of my head.
I can't test if the concept work right now, but I believe that you could use jQuery to achieve that, its submit function will intercept all form submissions on any tab, something along these lines
$("form").submit(function(event){
event.preventDefault();
//serialize forms here and submit using jquery post
});
You should look into the fields_for method. You can do something like this:
<% form_for #house do |f| %>
<%= f.text_field :square_feet %>
<% fields_for #listing do |s| %>
<%= s.hidden_field :id %>
<%= s.text_field :asking_price %>
<% end %>
<%= f.submit %>
<% end %>
And then in your controller you'd do something like this:
house = House.find(params[:id])
house.update_attributes(params[:house])
listing = Listing.find(params[:listing][:id])
listing.update_attributes(params[:listing])
That's for an update, but you can do whatever you want in there.
Related
I have a rails form with checkboxes. What I am trying to do is when the user clicks on a check box the app deletes that entry from the database. What I cannot figure out is how to have the app react when the user checks on the check box (similar action to having a submit button clicked). What should I include in my html so when the user click on a check box the app does something?
Here is my code
<% #items.each do |item|%>
<%if item.email == #user.email%>
<%= form_for #item do |f| %>
<%= f.check_box :to_do %>
<%= f.label item.to_do %><br />
<% end %>
<%end%>
<%end%>
I am iterating through items and if there is a match between the item.email and user email print out the item with a check box next to it. I can include more code if needed. Is there an Ajax call I can make?
As RedBassett said, I'm not 100% sure I agree with the strategy of having a checkbox instantly delete an entry. However, if that form you have is already set up to delete things, you can do it like this:
You don't even need an AJAX call. Just regular Jquery. Add a class to the form, as well as to the checkbox
<%= form_for #item do |f|, :class => "form-class" %>
<%= f.check_box :to_do, :class => "checkbox-class" %>
and in your JS file, use Jquery to tell the form to submit when the user clicks the checkbox.
$(".checkbox-class").on("click", function (){
$(".form-class").submit();
});
Note: this will actually submit the form, it will be as if you checked the box and then pressed a 'submit' button, so you will need to handle the form submission in the controller, if you want to stay on the same page, you will need to redirect back to the same page as this action will cause a reload/redirect
Yes. You can write an AJAX call.
<script>
$('#checkbox_id').change(function(){
$.get('controller/action?item_id='+$(this).val(), function(data, status){
/* Call back */
})
})
</script>
Use borbesaur answer, but remember you can just add the remote: true option to your form so Rails can submit the form using ajax and you avoid reloading all the page again. :)
Hi I'm working on a project in which I have a form which consists of a list of posts each with a checkbox and two submit buttons. One submit button is view, which takes the user to a template containing all the posts checked. The other is a delete button, which should keep the user on the page, delete the posts and refresh the list.
The problem is that I need to be able to change the submit format depending on which button the user pushes, HTML for view and js for delete.
Originally I had:
<%= form_tag multi_posts_path(format: :html) do %>
#checkboxes
<%= button_tag('View Checked', name: 'commit', value: 'view') %>
<% end %>
for just viewing, but obviously that wont work for deleting and I'm not sure how to change it to work.
Any help would be great!
You could use a link_to to the deleting action, and then, give it the appearance of a button.
I am trying to design a webpage that contains both a radio button field and a list of links. When one of the links is clicked, I would like to pass the associated value of the button to a controller. So far I have the following code:
In my views page:
<div id="radio_buttons">
<%= radio_button_tag 'radioGroup', "1" %> 1
<%= radio_button_tag 'radioGroup', "2" %> 2
<%= radio_button_tag 'radioGroup', "4" %> 4
</div>
...
<div class = "text">
<%= link_to "mylink", search_path(:radioGroup => #radioGroup)%>
</div>
In my Controller:
#radioGroup = params[:radioGroup]
When I add the line 'puts #radioGroup' to my controller, it prints nothing. I have been able to pass different values to a new web page upon a link click (i.e those contained in a text field) but for some reason I can't figure out how to do the same thing with buttons. Does anyone know how to accomplish this?
Thanks!
If you want to submit multiple values to another page you should use an HTML form for it. Check this page for more related info: http://guides.rubyonrails.org/form_helpers.html.
Is it possible to display certain elements within a partial for a particular view in Rails? For example I'd like the submit button's text to change depending on the view: so if I'm in the new.html.erb I'd like the submit button to appear as, <%= f.submit 'Create Account' %> and <%= f.submit 'Update Account' %> for edit.html.erb. The unconventional way would be to manually add the custom code into the each view but is there a clever way to do this in my _form.html.erb partial?
First of all, I would recommend putting it into the new and edit views. However, you can switch off of params[:action] if you want to. As in
<%= f.submit(((params[:action] == 'new') ? 'Create' : 'Update') + ' Account') %>
Use simple_form with i18n for that. SimpleForm make it automatically.
Example:
<%= simple_form_for(#message) do |f| %>
<%= f.error_notification %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.button :submit, class: "btn btn-primary" %>
<% end %>
I second kdeisz's answer if your intention is to use a single partial. The line he wrote will not be necessary if you use two separate views - You can just use different names on the same button in each view without any need for conditional logic.
To answer your supplemental questions: There is a tradeoff here between future changeability and DRY code. If your new and edit needs will start to differ significantly, you will have a lot of bloated, difficult-to-change conditional logic in your partial if you use it to render major features.
If you keep the views separated, this may repeat a significant amount of code, but it will also make the individual pages easier to change; the functions of each view will be tailored very specifically to the needs of each HTTP verb.
The answer isn't to conform completely to REST or to DRY "just because", but to ask yourself what will result in more work down the road. If your new and edit pages will be basically the same but for a few very minor features, the single partial (DRY) is more practical. If you see them diverging significantly in the future, keep them separated into two views (less DRY but more changeable).
Params. Each request made to Rails will automatically include an action and a controller based on the route the user requests; for example, navigating to /foo/bar might trigger action bar for controller foo, depending on how you've set up config/routes.rb. Rails fills in params[:action] and params[:controller] with these automatically. A good explanation of how this works, and how to access path and request params, can be found here.
I want to have a few check boxes on my page, and a link that accesses the value of these check boxes to pass in a link_to helper. I did not want to use a form because the view essentially has a number of links interspersed, and it doesn't naturally seem to be a form with one logical submit button.
I have something like
<% for p in #some_array %>
<!--other stuff .... -->
<input value=<%= p.id %> id=<%= p.id %> name="selected[]" type=checkbox>
<!--other stuff .... -->
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
But it doesn't seem to recognize the variable selected which stores the inputs. It raises
undefined local variable or method `selected' for #<#<Class:0x000001021b4a38>:0x00000102319a90>
I would say the last line in your code snippet is causing the error:
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
As ruby complains about a selected not being defined.
(Although I can't see why you don't want to use a form, as forcing the selections into something you can pass to the link would require some javascript magic.)