Using placeholder in a update form - ruby-on-rails

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

Related

Rails - Save parameters between actions

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

How to put nested fields_for code directly in parent _form?

How can I rewrite the code in duels/_form so I can put the code from dueler_fields directly in there?
duels/_form
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler| %>
<%= render 'dueler_fields', :f => dueler %>
<% end %>
<%= link_to_add_association f, :duelers do %>
<span class="glyphicon glyphicon-plus"></span> Dueler
<% end %>
<% end %>
_dueler_fields
# I want to place these two lines of code in the _form
<%= f.number_field :user_id, placeholder: "Enter User ID" %>
<%= f.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
duels_controller
def new
#duel = Duel.new
respond_with(#duel)
end
duels has_many duelers.
change f to dueler and you should be good to go. or:
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler_form| %>
<%= dueler_form.number_field :user_id, placeholder: "Enter User ID" %>
<%= dueler_form.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
<% end %>
<% end %>

Passing information to a controller through params (not saving it to the db). How to put it inside a hash like the users params hash

I have information that is passed to a controller method but isn't saved to the DB. I want to access this information, that is passed to the controller method as a whole hash, but it is all individual data as you will see below.
Here is the params:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0O7pbNNrddHCyPL9B/avUUD85574rFBfS57h+aWKK/mBakPSn5iHJKHhPmvuJVfyWxjBsAQn2kagwkTOALEKRg==", "page"=>{"content_top"=>"", "content_bottom"=>""}, "header1"=>"iijijij", "column1"=>"ijijijij", "header2"=>"", "column2"=>"", "header3"=>"", "column3"=>"", "header4"=>"", "column4"=>"", "commit"=>"Save", "guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"link-skill"}
As you can see there is a page hash and after, it is header1 column1 header2 column2... and so on. With the header1 info, I'm trying to put it inside a params hash like the page hash has for the values passed in it. So it's like "table" =>{"header1"=>"iijijij", "column1"=>"ijijijij", "header2"=>"", "column2"=>"", "header3"=>"", "column3"=>"", "header4"=>"", "column4"=>""}
I'm sure there is something I need to add to the form so it know to group them like this. Here is the form I currently have
<% if (current_user.mod_of_game?(#guide) unless current_user.nil?) %>
<%= form_for([#category, #page], url: update_pages_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :content_top, "Top Content" %>
<%= f.text_area :content_top, :class => 'editor' %>
<%= f.label :content_bottom, "Bottom Content" %>
<%= f.text_area :content_bottom, :class => 'editor' %>
<!-- to be in one hash when passed -->
<%= text_field_tag :header1 %>
<%= text_field_tag :column1 %>
<%= text_field_tag :header2 %>
<%= text_field_tag :column2 %>
<%= text_field_tag :header3 %>
<%= text_field_tag :column3 %>
<%= text_field_tag :header4 %>
<%= text_field_tag :column4 %>
<!-- end -->
<%= f.submit "Save" %>
<% end %>
I cant find what I need to add to make the text_field_tag data all be in one hash when passed.(the text_field_tag is purposely not being saved to the DB on form submit, it just needs to be passed to the method and grouped inside a hash)
how about using fields_for like this
<%= form_for([#category, #page], url: update_pages_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :content_top, "Top Content" %>
<%= f.text_area :content_top, :class => 'editor' %>
<%= f.label :content_bottom, "Bottom Content" %>
<%= f.text_area :content_bottom, :class => 'editor' %>
<!-- to be in one hash when passed -->
<%= f.fields_for :table do |t| %>
<%= t.text_field_tag :header1 %>
<%= t.text_field_tag :column1 %>
<%= t.text_field_tag :header2 %>
<%= t.text_field_tag :column2 %>
<%= t.text_field_tag :header3 %>
<%= t.text_field_tag :column3 %>
<%= t.text_field_tag :header4 %>
<%= t.text_field_tag :column4 %>
<% end %>
<!-- end -->
<%= f.submit "Save" %>
<% end %>
You can simply use the array way if you want to send the data that doesn't belong to the model, in params hash inside page key. Here's how:
<%= text_field_tag 'page[header1]' %>
Not only this, if you would like to use another key, you can use that as well like <%= text_field_tag 'custom_key[header1]' %>

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" %>

Rails: best way to validate and display error messages for fields not in model

In my Rails 3 application I have a typical 'Email this to a friend' form that has yours/friend's email and name fields. Each of them must be validated and error messages must be displayed, in a fashion similar to ActiveRecord forms:
<%= form_tag %>
<div class="fields">
<%= label_tag :friends_name %>
<%= text_field_tag :friends_name, params[:friends_name] %>
<%= label_tag :friends_email %>
<%= text_field_tag :friends_email, params[:friends_email] %>
</div>
<div class="fields">
<%= label_tag :your_name %>
<%= text_field_tag :your_name, params[:your_name] %>
<%= label_tag :your_email %>
<%= text_field_tag :your_email, params[:your_email] %>
</div>
<div class="fields">
<%= label_tag :message %>
<%= text_area_tag :message, params[:message] %>
</div>
<%= submit_tag 'Send'%>
</form>
What would be the 'Rails way' of doing this?
I found the answer in the episode #219 - Active Model on railscasts.com

Resources