Edit a post without leaving the page - ruby-on-rails

I am trying to edit a micropost on the page without leaving the physical page.
In my app i have a page the renders all of a user's microposts using the partial below:
microposts/_micropost:
<%= render 'shared/edit_micropost', object: micropost %>
<%= micropost.title %>
<%= micropost.content %>
<%= micropost.url %>
<%= raw "Tags: #{micropost.tag_list.map {|t| link_to t.capitalize, tag_path(t)}}" %>
shared/edit_micropost:
<%= link_to "edit", object, remote: true %>
<%= form_for object do |object| %>
<%= object.text_field :title %>
<%= object.text_area :content %>
<%= object.text_field :url %>
<%= object.text_field :tag_list %>
<%= object.submit "Update", class: "btn btn-mini" %>
<% end %>
When I click "edit" I would like the form to come up so that the title, content, url, and tag_list of the specific micropost is editable.
Right now when I click "edit" I get No route matches [GET] "/microposts/452" I'm not sure how to specify a working path in my link_to. I assume I have to move the form_for to a JS file?
I'm new to programming and would really appreciate some help, thanks.

There is a gem maybe you want to try it, 'Best in Place' is a jQuery based AJAX Inplace-Editor
Also there is a screencast for it by Ryan Bates

A straightforward way to do this would be to go ahead and render the form on the page, but hide it with javascript. Then when the user clicks the button, show it again. This way the form is also available to users that don't have js enabled.
Then it's just a matter of setting the remote: true option in the form to get it to submit via ajax and use an ajax callback to notify the user of success or failure (if you want). Again, this approach will still allow non-js users to submit the form with a normal request, while users with js enabled will get the slick ajax functionality.

Related

form_for was put inside fields_for

I'm having a problem with form_for and fields_for.
So, my problem is:
I had a form_for, and inside this form_for, I use a fields_for. Inside this fields_for, I use a form_tag (i used ajax for this form_tag).
But when I view the generated HTML, it didn't display form_tag, it only display form_for. And I didn't understand why.
Please explain for me, why it didn't display form_tag.
Here is my form_for:
<div class="row">
<%= form_for #real_estate, url: admin_real_estate_update_path do |f| %>
<%= f.fields_for(:client) do |client| %>
<%= text_field :real_estate, :assessment_start_at, value: #real_estate.assessment_start_at %>
<%= render partial: "admin/real_estate/form/assessment", locals: {real_estate_id: #real_estate.id} %>
<% end %>
<%= f.submit "Submut", class: "btn btn-primary"%>
<% end %>
</div>
Here is my form_for which i put inside fields_for:
<%= form_tag admin_search_assessment_path(real_estate_id), method: :post, remote: true do %>
<%= text_field_tag :company_name, params[:company_name] %>
<%= submit_tag "Submit" %>
<% end %>
And i tried to add <form></form> follow as:
<div class="row">
<%= form_for #real_estate, url: admin_real_estate_update_path do |f| %>
<form></form>
<%= f.fields_for(:client) do |client| %>
<%= text_field :real_estate, :assessment_start_at, value: #real_estate.assessment_start_at %>
<%= render partial: "admin/real_estate/form/assessment", locals: {real_estate_id: #real_estate.id} %>
<% end %>
<%= f.submit "Submut", class: "btn btn-primary"%>
<% end %>
</div>
And form_tag was display, but form_for didn't display.
Update:
So, i used $("form_2").submit(function() {....}); to solve this problem.
Actually, i still want to use form-nested.
Inside this fields_for, I use a form_tag (i used ajax for this form_tag)
N'est pas possible, mon ami.
--
Here's how it works...
form_for and form_tag both generate pure HTML forms:
<form action="/action" method="POST">
</form>
Many people get confused about how Rails works - it's really quite simple. Rails employs "helper" methods to generate pure HTML which your browser can read.
Browsers only understand HTML/CSS at the moment. Thus, whenever you send a request to Rails - it has to return that data, otherwise a "web page" simply wouldn't be able to be processed.
Thus, when you ask whether you can nest forms, you have to abide by how the forms work in pure HTML (spec):
Note you are not allowed to nest FORM elements!
HTML fill-out forms can be used for questionaires, hotel reservations,
order forms, data entry and a wide variety of other applications. The
form is specified as part of an HTML document. The user fills in the
form and then submits it. The user agent then sends the form's
contents as designated by the FORM element. Typically, this is to an
HTTP server, but you can also email form contents for asynchronous
processing.
In short, it means that everything within a <form> tag is counted as a form by HTTP. This means that if you have another <form> tag, it's going to cause an error, preventing either from working.
You know this already (otherwise you wouldn't have mentioned ajax).
Your main problem is the use of <form></form> inside your current <form> object. This will confuse HTML profusely, and thus I would recommend replicating the submission of a form, without the <form> object itself:
#app/views/admin/real_estate/form/assessment.html.erb
<%= text_field_tag "[company_name]", params[:company_name], id: real_estate_id %>
<%= button_tag "Submit", type: "button" , data: { id: real_estate_id } %>
#app/assets/javascripts/application.js
$(document).on("click", "button", function(e){
e.preventDefault();
var real_estate_id = $(this).data("id");
$.ajax({
url: "path/to/real/estate/form/assessment/" + $(this).data("id")),
data: {company_name: $("input[type=text]#" + real_estate_id).val()}
success: function(data) {
//do something on success
},
error: function(data) {
//do something on error
}
});
});
This code will still output what you need.
The difference will be two-fold:
The user will experience the same functionality (the input will still be present)
The embedded form will be passed to the main "form" submit, but will not be passed through your strong params method (making it
invisible to your controller)
In effect, you're replicating the functionality of an HTML form through Ajax. Whilst I still wouldn't do it this way, it will give you the functionality you require.

Rails : From form_for to form_tag

For one of my projects i'm using https://github.com/rsantamaria/papercrop in addition to paperclip to crop pictures before upload.
It is said in the readme to do :
<%= form_for #user do |f| %>
<%= f.cropbox :avatar %>
<%= f.crop_preview :avatar %>
<%= f.submit 'Save' %>
<% end %>
The issue is that my form i need to adapt those function to is a form_tag.
I usually always manage to navigate between the two as needed but since my form is in my application layout ( needs to be there to be on all pages) i don't really use a controller for it but instead form_tag my_post_route_path :
<%= form_tag products_path, multipart: true do %>
name
<%= text_field_tag(:name) %>
</br>description
<%= text_field_tag(:description) %>
</br>image
</br><%= file_field_tag "image" %>
<%= submit_tag("create product") %>
<% end %>
I would like to know two things :
-How do you translate the methods from f.cropbox etc. to Form tag syntax ? i really don't see a way.
I've tried cropbox_tag but it didn't work ( and i didn't make any sens anyway)
-My second question is kind of different but where , when i have a form that need to pop up on all pages ( i'm using a jquery dialog to make it pop up and a button in the navbar ), should it be placed ? is the fact that i've put it in my application_view completely wrong , i've placed it cause the form is only for logged in users ?
Thanks to anyone who tries to help!

Rails: How do I associate a submit_tag to a search_field_tag?

I have the following code in my view:
<% form_tag(search_path) %>
<%= search_field_tag("search", nil, placeholder: "Book name") %>
<%= submit_tag ("Search") %>
And the following route:
search_path POST /search(.:format) searches#create
The form appears correctly but when I click submit nothing happens. Server logs show absolutely nothing. Chrome Dev tools shows nothing happens on the client side. I think that the way I am constructing this form is incorrect. I think I need to add something to associates the different tags I have, and without this element all these form elements are all disassociated and fragmented. What am I missing here?
You need to add do to the end of your form_tag. This is how Rails associates the different elements of the form.
<%= form_tag(search_path) do %>
<%= search_field_tag("search", nil, placeholder: "Book name") %>
<%= submit_tag ("Search") %>
<% end %>
"Doing-Ending" the form_tag is a good practice, but I'd say your only problem is the lack of the = sign in the embedded Ruby syntax.

submit_tag value not passed as param when remote true

I have a list with results where users have 2 options Save|Delete individual or multiple records. For this I have 2 submit_tags
So When I do
<%= form_tag some_path do %>
<%= submit_tag "Hello World" %>
<% end %>
the submit_tag {...,'commit'=>'Hello World'} is passed to the controller inside the parameters hash
but as soon as do remote true
<%= form_tag some_path, remote:true do %>
<%= submit_tag "Hello World" %>
<% end %>
the commit value is ommitted!
Any idea how can I fix it? I need to the user to stay in the same page whilst saving/deleting records... so remote: true is mandatory and since there are 2 submits I need some logic to do different things in the controller.
Thanks,
I have just had the same or a similar problem and I want to share my solution. In my case, I had a form defined like this:
<%= form_tag url, remote: true do %>
...
<%= submit_tag t('.simulate'), id: 'btn_simulate', onClick: 'showSpinner()' %>
<% end %>
When the button was clicked, the request was sent without any parameters.
As it turned out the problem was caused by the onClick option. As soon as I removed it, the form worked as expected. To re-enable the onClick behavior, I instead added an event handler for 'ajax:before' to the form.

Rails 3 submit form with link

How I can submit form with link on correct rails 3 format?
Thanks.
<%= form_for #post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
My code sample.
For people who came here via Google, I have an improvement on Zequez's answer. Instead of the method that he gives, add this method to the application helper instead:
def link_to_submit(*args, &block)
link_to_function (block_given? ? capture(&block) : args[0]), "$(this).closest('form').submit()", args.extract_options!
end
Then, as Zequez stated, for simple links you can just do this in your view:
<%= link_to_submit 'Submit Form' %>
...and for more complicated buttons you can pass HTML options and a block to be used inside the link. If you use Twitter Bootstrap, for example, this lets you add CSS classes, formatting and icons:
<%= link_to_submit( class: 'btn btn-primary' ) do %>
<strong>Submit</strong> the Form <i class="icon-arrow-right"></i>
<% end %>
The JQuery code will work as long as the link is a child of the form (that is, as long as link_to_submit is called from somewhere within the form_for block).
"Correct" is a tricky word in this context ;) . One could ask why you're not just taking a button element and make it look like a link?
Anyways — you can't achieve this with plain HTML (at least not to my knowledge). With a Javascript framework like e.g. jQuery you could simply do something like this:
$('a').click(function(){
$('form').submit();
return false;
});
Rails 2.3.x had a link_to_remote helper which let's you specify a :submit parameter (= DOM element's ID, default is the parent form). So you were be able to write:
link_to_remote 'submit', :url => {…}, :submit => "my_form"
But with Rails 3's push to UJS, this helper is gone.
You can add the following to the application helper:
def link_to_submit(text)
link_to_function text, "$(this).closest('form').submit()"
end
Then inside your view files you can just call
link_to_submit 'Submit Form'
And the link must be child of the form.
With jquery, this one-liner will work fine for a simple form.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"$('form').submit()" %>
Of course you don't really have to use jquery, just finding the dom element for your form will work fine as well.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"document.getElementById('your_form_id').submit()" %>
This way you don't use any ajax, just plain form submit.
In Rails 3, the link_to_remote helper is gone, but it's replaced with
link_to 'submit me', url_for(#post), {:remote => true, :class => 'submit_me'}
In your case, you likely want your form to do the AJAX, like so:
<%= form_for #post, :remote => true do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
With a companion link:
link_to 'submit me', '#', :class => 'submit_me'
Then, in an .js file included in the page body:
$('.submit_me').click(function() {
$('form').submit();
return false;
});
The idea is that anything more complicated than turning a link or form into an ajax request should be done with the jQuery callbacks, as listed here:
https://github.com/rails/jquery-ujs/wiki/ajax
And if you want to really get into interactive AJAX requests, go here for a great 2-part article on it.

Resources