mansim
mansim

Reputation: 727

edit button inside new form

I am making a form where i want to have a @post edit button inside a new form for @article. How to do it? I've looked around but haven't found a solution.

I'm creating a new Post for Banquet hall. The customer presses a "next" button to create a new article where is data of Post and new data to input. Then they proceed to next form with personal information. This is part of my form. All of scaffold system. What do I need to write after both f.submit, or in controllers? It would be most clear to have it all in my view.

Articles/new.html.erb:

<%= form_for @article do |f| %>
  <%= f.datetime_select :begin %> # The default is @post.begin, after saving goes to Article table. Customers can edit the date.
  <%= f.datetime_select :end %>   # The default is @post.end, after saving goes to Article table. Customers can edit the date.
  <%= @price_for_banquet_hall %>  # Big formula, depends on dates difference. This I want to recalculate, but I'm weak on scripting with Ajax, so i need this edit button of @post only that customers here would see the new price.
  <%= f.submit ... %>             # This should edit data of table Post (because @article not yet created, its creating here) and reload this page again(just editing data of @post).
  <%= f.submit, new_contact_path(@article) %> # This should save data to @article and proceed.
<% end %>

Here is my form now:

articles/new

<%= form_for @article, remote: true do |f| %>
<table id="table">
<%= f.datetime_select :arrival,  :default => Post.last.begin.to_datetime %> 
<%= f.datetime_select :departure,  :default => Post.last.end.to_datetime%>
<%= @price_for_banquet_hall %>
</table>
<%= f.submit %>
<% end %>

articles/create.js.erb

$("#table").html("<%=j render partial: "new_article", locals:{@price_for_banquet_hall => @price_for_banquet_hall} %>")

I dont get still

Upvotes: 1

Views: 1125

Answers (1)

Mandeep
Mandeep

Reputation: 9173

If you look at your form

<%= form_for @article do |f| %>
  <%= f.datetime_select :begin %> # The default is @post.begin, after saving goes to Article table. Customers can edit the date.
  <%= f.datetime_select :end %>   # The default is @post.end, after saving goes to Article table. Customers can edit the date.
  <%= @price_for_banquet_hall %>  # Big formula, depends on dates difference. This I want to recalculate, but I'm weak on scripting with Ajax, so i need this edit button of @post only that customers here would see the new price.
  <%= f.submit ... %>             # This should edit data of table Post (because @article not yet created, its creating here) and reload this page again(just editing data of @post).
  <%= f.submit, new_contact_path(@article) %> # This should save data to @article and proceed.
<% end %>

In your form

<%= form_for @article do |f| %>

will generate

<form accept-charset="UTF-8" action="/articles/create" method="post">

so it's from here the path of your form is generated. You can have two submit buttons in your form but they will always take you to the same action in controller.

I need more information on how you are handling your form like is it in modal or what. If you are using modals and you want to have two buttons one to create account and second to render a different form in a separate modal then instead of having a second submit button what you can do is have a link instead which will close your new forms modal and open up your different modals form


About the ajax thing if you want to handle it by ajax you simply need to put remote: true option in your form like:

<%= form_for @article, remote: true do |f| %>
  #your fields
<% end %>

This will take you to create action in your articles controller and you can handle it like this:

def create
  @article = Article.new(article_params)
  respond_to do |format|
    if @article.save
      format.html{redirect_to your_path}
      format.js{} #this will allow rails to looks for a create.js.erb file in your views/article
    else
      format.html{render new}
    end
  end
end

And finally you can render a new partial containing your edit form in your create.js.erb file. For details refer to Working with Javascript in Rails

Update:

Inside your create.js.erb file you can do something like this:

$("#some_id_of_parent_element").html("<%=j render partial: 'your_partial', locals:{:your_variable => partial_variable} %>")

Update:

Your form should be

<table id="table">
  <%= form_for @article, remote: true do |f| %>
    <%= f.datetime_select :arrival,  :default => Post.last.begin.to_datetime %> 
    <%= f.datetime_select :departure,  :default => Post.last.end.to_datetime%>
    <%= @price_for_banquet_hall %>
    <%= f.submit %>
  <% end %>
</table>

and your create.js.erb file will be:

$("#table").html("<%=j render partial: 'your_edit_partial', locals:{@price_for_banquet_hall => price_for_banquet_hall} %>")

also since you are using @price_for_banquet_hall in your js.erb you will have to initialise it in your create action.

Upvotes: 1

Related Questions