Rails - Save parameters between actions - ruby-on-rails

I have an app that books spaces for certain dates. In the index page, I have a search form to display only the spaces that are available from params[:query1] to params[:query2] certain dates. Then in the space show page, I have a form for booking the space.
I'd like to maintain the params[:query1] and params[:query2] also in the space show page as the default value for the booking form.
index.html.erb:
<%= form_tag spaces_path, method: :get, class: "form-inline" do %>
<%= label_tag :query1, "Check in" %>
<%= date_field_tag :query1, params[:query1], class: "form-control" %>
<%= label_tag :query2, "Check out" %>
<%= date_field_tag :query2, params[:query2], class: "form-control" %>
<%= submit_tag "Search", class: "btn" %>
<% end %>
show.html.erb
<%= form_with(model: [#space, #booking], local: true) do |f| %>
<%= label_tag :check_in, "Check in" %>
<%= f.date_field :check_in, value: params[:query1], class: "form-control" %>
<%= label_tag :check_out, "Check out" %>
<%= f.date_field :check_out, value: params[:query2], class: "form-control" %>
<%= submit_tag "Reserve", class: "btn" %>
<% end %>

You can use a session to store your queries and use them throughout your app. You can store your queries in a session in your index action like this:
def index
session[:query1] = params[:query1]
session[:query2] = params[:query2]
end
Then in your show action, you can access the session like this:
def show
#query1 = session[:query1]
#query2 = session[:query2]
end
In your form on your show page, you can then use the instance variables:
<%= form_with(model: [#space, #booking], local: true) do |f| %>
<%= label_tag :check_in, "Check in" %>
<%= f.date_field :check_in, value: #query1, class: "form-control" %>
<%= label_tag :check_out, "Check out" %>
<%= f.date_field :check_out, value: #query2, class: "form-control" %>
<%= submit_tag "Reserve", class: "btn" %>
<% end %>
Learn more about session

Related

ActionView::Template::Error (undefined method `product' for :outlet_product

i'm doing the edit page for OutletProduct which store product_id and outlet_id.
At the edit page the product name and outlet name should show original one. But now i can't show the product and outlet name out.
Also it show me some error.
OutletProduct controller
def edit
#outlet_product = OutletProduct.find(params[:id])
end
def update
#outlet_product = OutletProduct.find(params[:id])
if #outlet_product.update(outlet_product_params)
flash[:success] = "Outlet Product updated"
redirect_to #outlet_product
else
render 'edit'
end
end
edit.html.erb
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #outlet_product, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :product_name %>
<%= f.text_field #outlet_product.product.name, class: 'form-control'%>
<%= f.label :quantity %>
<%= f.number_field :quantity%>
<%= f.label :selling_price %>
<%= f.number_field :selling_price, class: 'form-control' %>
<%= f.label :last_cost %>
<%= f.number_field :last_cost, class: 'form-control' %>
<%= f.label :outlet_id %>
<%= f.text_field #outlet_product.outlet.name, class: 'form-control'%>
<%= f.submit "Save Changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
error in website
UI edit page
Update
OutletProduct call in console
You have an extra ':' here: <%= f.text_field : #outlet_product.product.name%>, and the error is telling you it thinks #outlet_product is a symbol, but you are treating it like an object!
You are doing it correctly slightly lower down:
<%= f.text_field: #outlet_product.outlet.name, class: 'form-control' %>
so just do the same and remove the ':'

How do I reload my comments partial with ajax?

So I'm a beginner in Rails and I'm trying to reload a partial when I create a Comment.
I am creating a sort of forum where I have Posts and Comments on Posts.
Here is the situation :
/views/posts/show.html.erb
<% #posts.each do |post| %>
<%= post.title.capitalize %>
<%= post.content %>
<%= render 'comments/comment', post: post, comments: post.comments %>
<%= form_tag(category_forum_post_comments_path(#category, #forum, post), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :content, nil, placeholder: 'Commenter', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
<% end %>
<%= form_tag(category_forum_posts_path(#category, #forum), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :title, nil, placeholder: 'Titre de votre Post', class: 'form-control' %>
<%= text_area_tag :content, nil, placeholder: 'Description', id: 'desc', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
and then in /views/comments/_comment.html.erb
<% comments.each do |comment|%>
<%= comment.content %>
<%= link_to 'Supprimer', category_forum_post_comment_path(#category, #forum, post, comment),
method: :delete, class: "btn-danger btn-sm", data: { confirm: 'Etes-vous sûr?' } %>
<% end %>
I would like to reload my comment partial on post.
I tried to understand so many posts on stackoverflow and tutorial that I could find online and still have no idea how to do it.
If someone would be kind enough to help me get through this it would be great.
What's the action that you want to trigger that reload? you are showing forms, links, it's not too clear.
Basically, you need to use remote: true on the form/link, then add a view for that action with .js.erb extension, and, on that view, use javascript to change the content of an specific element.
Something like:
1st, you need to give some unique way to reach the element you want with javacsript:
<% #posts.each do |post| %>
<div id="post_<%= post.id -%>">
<%= post.title.capitalize %>
<%= post.content %>
<div class='comments'>
<%= render 'comments/comment', post: post, comments: post.comments %>
</div>
<%= form_tag(category_forum_post_comments_path(#category, #forum, post), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :content, nil, placeholder: 'Commenter', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
</div>
<% end %>
<%= form_tag(category_forum_posts_path(#category, #forum), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :title, nil, placeholder: 'Titre de votre Post', class: 'form-control' %>
<%= text_area_tag :content, nil, placeholder: 'Description', id: 'desc', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
Note the div with id=post_x and the div with class comment
Now you can locate it using javacsript:
#your_view.js.erb
post = document.getElementById('post_<%= #post.id -%>');
comments = post.querySelector('comments');
And finally, also on your view, render the partial and replace comments' innerHTML
comments.innerHTML = '<%= j(render partial: "comments/comment", post: #post, comments: #post.comments) -%>';
Just a suggestion, use "comments" (in plural) for the name of the partial, since you are rendering all the comments and not just one.

Passing Custom Form Values to a Custom Controller Action

In Rails, I have a custom controller action that needs to accept some parameters from a form:
def update_ordid
# Get the active exchange
#exchange = Exchange.find(params[:id])
# Decide which order ID field to update
active_order_field = params[:ordfld]
# Save the order ID
order_id = params[:ordid]
if active_order_field == 1 then
#exchange.order_id_1 = order_id
else
#exchange.order_id_2 = order_id
end
#active_exchange.save
respond_with(#exchange)
end
Because these parameters aren't actual data fields in the exchange table, I would typically invoke the action by using a link such as:
link_to "Update Order ID", update_ordid_exchange(ordfld: value_from_form, ordid: value_from_form), :method => :post
Because in this case the value of these parameters needs to be populated by user input, I created the following form to pass the data:
<%= form_for(#exchange, url: update_ordid_exchange_path) do |f| %>
<div class="field">
<%= f.label :ordid, "Order ID" %><br>
<%= f.text_field :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%f.hidden_field :ordfld, :value => "1" %>
<% else %>
<%f.hidden_field :ordfld, :value => "2" %>
<% end %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
When I attempt to render this form, I receive the error: undefined method `ordid' for #
When researching this issue, I found that I could be able to do this by changing the text_field line to:
<%= f.text_field_tag :ordid, class: "form-control" %>
While this resolves the initial error, it throws a new error: undefined method `text_field_tag' for #
Any ideas as to what I'm doing wrong?
You cannot use form_for since your form elements doesn't represent the attributes of a model. use form_tag instead
<%= form_tag(update_ordid_exchange_path, :method => :patch) do%>
<div class="field">
<%= label_tag "Order ID" %><br>
<%= text_field_tag :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%= hidden_field_tag :ordfld, "1" %>
<% else %>
<%= hidden_field_tag :ordfld, "2" %>
<% end %>
<div class="actions">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<%end%>
Documentation here
form_tag vs form_for
hidden_field_tag
I'm a bit new to this myself, but I believe it's because you're calling "text_field_tag" from a form_for instead of form_tag builder object. Try just leaving off the form for object as such:
<%= text_field_tag :ordid, class: "form-control" %>

Using placeholder in a update form

I want to replace the default value of a text_field in a update form by a placeholder. How can I do that ?
My first thought was the following code but the default value of the text_field is still here :
<%= form_for(some) do |f| %>
<div><%= f.text_field :thing, placeholder: "what an example !" %></div>
<%= f.submit %>
<% end %>
You can use text_field_tag
<%= form_for(some) do |f| %>
<div>
<%= text_field_tag "some_thing", (some.thing unless some.thing=="default value"), :name=> "some[thing]", :placeholder => "what an example !" %>
</div>
<%= f.submit %>
<% end %>
Hope, it will work..
Thanks

Send parameter to controller on submit_tag rails

I have two submit_tag on my form and I would like to send a different parameter on each.
How can I do this?
My form view:
<%= form_tag(some_path, :method => "get") do %>
<%= text_field_tag :number %>
<%= text_field_tag :name %>
<%= submit_tag "Op01", class: "btn_search", my_parameter: 1 %>
<%= submit_tag "Op02", class: "btn_search", my_parameter: 2 %>
<% end %>
And on my controller:
#oper_type = params[:my_parameter]
But when I display the #oper_type it is always nil.
Thanks.
<%= submit_tag "Op01", class: "btn_search", value: 1 %>
<%= submit_tag "Op01", class: "btn_search", value: 2 %>
#oper_type = params[:commit] # 1 or 2
or a little simplier
<%= submit_tag "Op01", class: "btn_search" %>
<%= submit_tag "Op01", class: "btn_search" %>
#oper_type = params[:commit] # "Op01" or "Op02"
Is there a way to accomplish this without revealing the value to the user on the submit button? Say you want to have a different text on the button and a hidden value sent to the controller...

Resources