Rails 6 pass variable to partial - ruby-on-rails

I want to render a partial for each conversation. I have the following code:
# app/views/messages/_new.html.erb
<%= simple_form_for [#conversation, #message] do |f| %>
<%= f.text_area :body %>
<%= f.text_field :messageable_id, value: current_user.id, type: "hidden" %>
<%= f.text_field :messageable_type, value: "#{current_admin_user.class.name}", type: "hidden" %>
<%= f.submit "Send Reply" %>
<% end %>
I want to change value of both hidden text fields depends on passed variable. Something like:
# app/views/admin/conversations/_show.html.erb
<%= render 'messages/new', value: current_admin_user.id %>
With below code I'm getting an error Validation failed: Messageable must exist which means messageable_id = nil

Because you are not using the value you are passing to the partial.
Also, you need to pass both the variables
Try replacing the partial with the following
# app/views/admin/conversations/_show.html.erb
<%= render 'messages/new', messageable_id: current_admin_user.id, messageable_type: "#{current_admin_user.class.name}" %>
and use them
# app/views/messages/_new.html.erb
<%= simple_form_for [#conversation, #message] do |f| %>
<%= f.text_area :body %>
<%= f.text_field :messageable_id, value: messageable_id, type: "hidden" %>
<%= f.text_field :messageable_type, value: messageable_type, type: "hidden" %>
<%= f.submit "Send Reply" %>
<% end %>
NOTE: If you want the _new partial to render on its own as well
replace it with something like this:
<% messageable_id ||= current_admin_user.id %>
<% messageable_type ||= "#{current_admin_user.class.name}" %>
<%= f.text_field :messageable_id, value: messageable_id, type: "hidden" %>
...

Related

How to insert new line breaks using form_for with collection_check_boxes

I use the following form_for at one point in my code to display a series of topics for users to select from.
<%= form_for #user do |f| %>
<%= f.collection_check_boxes(:topic_ids, Topic.all.sample(50).each, :id, :topic_name) %>
<%= f.submit %>
<% end %>
When it renders on the page, the boxes are all one-after-the-other. How can I separate them so that there is one check box per line?
from reference here
It is possibly to customize the way the elements will be shown by giving a block to the method as sample below from your code above
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name do |b| %>
<%= b.label(class: "check_box") do %>
<%= b.check_box(class: "check_box") %>
<%= b.object.topic_name %></br>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
You may of course render HTML inside the block:
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, : topic_name do |b| %>
<div class="my-checkbox-wrapper">
<%= b.label(class: "foo") do %>
<%= b.object.topic_name %>
<%= b.check_box(class: "bar") ></br>
<%end%>
<%= f.submit %>
<%end%>
You can have a look at this example
It is very broad question. In short, using CSS.
For example using Bootstrap 4:
<%= form_for #user do |f| %>
<div class="form-check">
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name, class: 'form-check-input' %>
</div>
<%= f.submit %>
<% end %>

Rails :How to move code from view to partial file

In edit.html.erb
<% if material.look %>
<%= form_for material do |f|%>
<% if material.approval = "1" %>
<%= f.hidden_field :date,value: Date.today %>
<% end %>
<%= f.hidden_field :approval_amount, value: 0 %>
<%= f.hidden_field :approval_amount_percentage, value: 0 %>
<%= f.submit 'yes' %>
<% end %>
<%= form_for material do |f|%>
<%= f.hidden_field :approval_amount, value: 0 %>
<%= f.hidden_field :approval_amount_percentage, value: 0 %>
<%= f.submit 'no' %>
<% end %>
<% end %>
Code in two forms are duplicate, so I want to move them to partial file,
In _material_form.html.erb I code like this
<%= f.hidden_field :approval_amount, value: 0 %>
<%= f.hidden_field :approval_amount_percentage, value: 0 %>
In edit.html.erb, I modifies duplicate code
<% if material.look %>
<%= form_for material do |f|%>
<% if material.approval = "1" %>
<%= f.hidden_field :date,value: Date.today %>
<% end %>
<%= render 'material_form'%>
<%= f.submit 'yes' %>
<% end %>
<%= form_for material do |f|%>
<%= render 'material_form'%>
<%= f.submit 'no' %>
<% end %>
<% end %>
However, it shows an error
undefined local variable or method `f' for
What causes the error and how to fix it ? Thanks.
Your form, assigned to the variable f is out of scope in your partial. To use it, you'll need to pass f in as a local value to your partial:
<%= render 'material_form', f: f%>
The rails documentation on partials is quite helpful; you can also dig into the action renderer documentation if you want to go deeper.

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]' %>

Ruby Rails is it possible to check before displaying form?

Is it possible to check submission before showing this simple form:
<%= form_for #article, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, size: "60x12" %>
<%= f.submit "Create" %>
<% end %>
like if its already submitted first time show other html content instead ?
Test the object. If you're using ActiveRecord/Mongoid:
<% if #article.new_record? %>
... display form ...
<% else %>
... do something different ...
<% end %>

RoR field set on form_for

How do you add a field set to a form_for method?
You can use field_set_tag. For example, using a generic 'user' object
For Rails 2.3.x
<% form_for(#user) do |f| %>
<% field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
And for Rails 3.0.0:
<%= form_for(#user) do |f| %>
<%= field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
You need to have a new object or get an existing object from your dc since it is a 'form for' and then you create a form builder f and call methods on that form builder such as the following:
<% form_for(#object) do |f| %>
<%= f.text_field :method_name %>
<% end %>

Resources