how can I send a param to maintain some checkboxes - ruby-on-rails

There were some similar topics and I tried them but I couldn't resolve.
code
<td><%= check_box_tag "words_info[]",word.id,false,{id:id} %><label for="<%=id%>" >追加</label></td>
The above code is called by below one which is a search form.
<h2>フラッシュカードを作成してください</h2><br>
<div class="col-md-12">
<h4>検索</h4>
<%= search_form_for #q,url:start_words_path do |f| %>
<%= f.label :title_cont, 'タイトル' %>
<%= f.text_field :title_cont %>
<%= f.label :user_name_cont,"作者"%>
<%= f.text_field :user_name_cont%>
<%= f.label :answer_cont,"答え"%>
<%= f.text_field :answer_cont%>
<%= f.label :question_cont,"問題"%>
<%= f.text_field :question_cont%>
<%= button_tag :type => "submit", :class =>"btn btn-send" do %>
<i class="material-icons left">search</i>検索
<% end %>
<%end%>
<%= form_tag({:action=>"practice"}, {:method=>"get"}) do %>
<%=render "words/words_index"%> #rendered here
<hr>
<%= submit_tag "完了" , class:"btn btn-large btn-send"%>
<% end%>
</div>
Everything that I tried
<td><%= check_box_tag "words_info[]",word.id,params[:words_info]==word.id,{id:id} %><label for="<%=id%>" >追加</label></td>
<td><%= check_box_tag "words_info[]",word.id,params[:words_info].include?(word.id),{id:id} %><label for="<%=id%>" >追加</label></td>
<td><%= check_box_tag "words_info[]",word.id,params[:words_info].to_s==word.id.to_s,{id:id} %><label for="<%=id%>" >追加</label></td>
environment
ruby 2.3
rails 4.2.7
materialize css

Related

why the parameter come out in my UI page?

i have no idea why the parameter suddenly come out at my role create page. After i add the permission checkbox there then it appear.
new.html.erb
<% provide(:title, "Create Roles") %>
<h1 class="dashboard">Create Role</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #role, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= #permissions.each do |permission|%>
<%= check_box_tag 'permission_ids[]', permission.id %>
<%= f.label :permission_name, permission.name %>
<% end %>
<%= f.hidden_field :company_id , value: 2%>
<%= f.submit "Create Role", class: "btn btn-primary bottom" %>
<% end %>
</div>
</div>
Parameter show at role create page
You are currently seeing the string output of the each method because you're using <%= #permissions.each ...
Instead use the "silent" <% as you do for end:
<% #permissions.each do |permission| %>

rails cant create an object

So I am making a colors palette manager and both tables requested on the form are nested and properly related(I would like to also point out that I am a rookie to rails)
This whole code was working perfectly fine when in line 4 was requesting links instead of just the names and suddenly wont work anymore, thank you for the help in advance
<div class='nav_bar'>#</div>
<% if #projects %>
<% #projects.each do |project| %>
<li><div class='container'><h1><%= project.name project_path(:id => project.id) %></h1>
<ul>
<%= form_for project do %>
<%= fields_for :palette, project.palette do |palette| %>
<%= palette.label 'background_dark_color' %>:
<%= palette.text_field :background_dark_color, placeholder: '#palette.background_dark_color' %><br/>
<%= palette.label 'background_light_color' %>:
<%= palette.text_field :background_light_color, placeholder: "#palette.background_light_color" %><br/>
<%= palette.label 'dark_color1' %>:
<%= palette.text_field :dark_color1, placeholder: "#palette.dark_color1" %><br/>
<%= palette.label 'dark_color2' %>:
<%= palette.text_field :dark_color2, placeholder: "#palette.dark_color2" %><br/>
<%= palette.label 'light_color1' %>:
<%= palette.text_field :light_color1, placeholder: "#palette.light_color1" %><br/>
<%= palette.label 'light_color2' %>:
<%= palette.text_field :light_color2, placeholder: "#palette.light_color2" %><br/>
<%= palette.submit %>
<%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</div>
</ul>
<% end %></li>
<%end%>
<%end%>
<div class='circle'><%= link_to "+", new_project_path %></div>
<button type="button"><%= link_to "+", new_project_path %></button>
I think you are referring to the following line,
<%= project.name project_path(:id => project.id) %>
There is a mistake in this code. There are two ways to fix this.
Solution 1
If you want to display a link or an anchor tag this use replace this with the following code,
<%= link_to project.name, project_path(:id => project.id) %>
Solution 2
If you don't want a link but, just the name to appear then, do the following,
<%= project.name %>

If statement preventing submit button to work

My submit button won't work because of the first if statement. If I remove the block, the button works. The statement seems to break the view. Why?
<% if params[:action] == "edit" %>
<div class="field">
<%= form_tag :action => "edit" do %>
<%= select_tag :vehicle_id, options_from_collection_for_select(#vehicles, :id, :model, params[:id].to_i), :onchange => "this.form.submit()" %>
<% end -%>
<%= link_to 'Nouvelle voiture', new_vehicle_path %>
</div>
<% end -%>
<div class="field">
<%= f.label "Modèle" %>
<%= f.text_field :model, required: true %>
</div>
<div class="field">
<%= f.label "Immatriculation" %>
<%= f.text_field :license_plate, required: true %>
</div>
<div class="field">
<%= f.label "Complément" %>
<%= f.text_field :complement, required: true%>
</div>
<div class="field">
<%= f.label "Puissance CV" %>
<%= f.number_field :horse_power, required: true %>
</div>
<div class="field">
<%= f.label "Indemnité KM" %>
<%= f.number_field :km_compensation, required: true%>
</div>
<% if params[:action] == "edit" %>
<%= link_to 'Détruire', #vehicle, method: :delete, data: { confirm: 'Êtes-vous sûr ? Les trajets associés seront aussi détruits.' } %>
<h1>Trajets</h1>
<div>
<span>Clients</span>
<span>Kms aller/retour</span>
</div>
<%= f.fields_for :trip_distances do |builder| %>
<div class="field">
<%= builder.text_field :id_contract %>
<%= builder.number_field :length %>
</div>
<% end -%>
<% end -%>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
When you put if statement you don't have form in which your submit button resides. Without if statement your form is in the view, and you can submit to it.
Found the solution. I didn't need the form_tag:
<%= form_tag :action => "edit" do %>

Using nested forms in Rails - How can I make the show page display every new entry in a table format depending on how many presents the user wants?

I have a baby present registry I'm building with Rails 3.2. There is a registry model and a present model nested under the registry model. Everything works 100% except in the show page.
When a user creates a registry he/she can add presents through the add presents tag (as many as he/she wants), but the problem is in the show page it only the first present is displayed as I want it too. The rest is displayed in plain non formatted text.
Any help please? I would like it to show in a table format.
Here is my code:
Here is the Registry Form:
<%= nested_form_for(#registry) do |f| %>
<% if #registry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#registry.errors.count, "error") %> prohibited this registry from
being saved:</h2>
<ul>
<% #registry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :due_date %><br />
<%= f.date_select :due_date, :rows => 5 %>
<%= f.label :theme %><br />
<%= f.text_field :theme, :class => 'field2' %>
<div class="field1">
<%= f.label :gender %><br />
<%= f.text_field :gender %>
</div>
<div class="field1">
<%= f.fields_for :presents do |builder| %>
<%= render 'present_fields', f: builder %>
<% end %>
</div>
<%= link_to_add_fields "Add Presents", f, :presents %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The presents_fields:
<div class="field1">
<table>
<tr>
<td> <%= f.label :type, "Present Type" %> </td>
<td> <%= f.text_field :type, :class => 'field2' %> </td>
<td> <%= f.label :Quantity, "Quantity" %> </td>
<td> <%= f.number_field :quantity, :class => 'field3' %> </td>
<td> <%= f.label :color, "Color" %> </td>
<td> <%= f.text_field :color, :class => 'field2' %> </td>
<td> <%= f.label :brand, "Brand" %> </td>
<td> <%= f.text_field :brand, :class => 'field2' %> </td>
<td> <%= f.link_to_remove "Remove this present" %></td>
</tr>
</table>
</div>
The show page:
<% provide(:title, 'My Party and Present Whishes') %>
<%= notice %>
<p> Baby & Party Details: </p></br>
<li><p> Due date: </p> <%= #registry.due_date %></li></br>
<li><p> Party Theme: </p><%= #registry.theme %></li></br>
<li><p> Gender: </p><%= #registry.gender %></li></br>
<p> Presents Registry: </p></br>
<li>
<% #registry.presents.each do |registry| %>
<%= registry.type %>
<%= registry.quantity %>
<%= registry.color %>
<%= registry.brand %>
</li>
<% end %>
</br>
</br>
<%= link_to 'Edit', edit_registry_path(#registry) %>
<%= link_to 'Back', registries_path %>
<li>
<% #registry.presents.each do |registry| %>
Should not these lines be inverted?
<% #registry.presents.each do |registry| %>
<li>

Rails 3.2 nested_form gem: how to create link_to edit_model_path() in fields_for partial

I have a nested form, that uses the partial method for rendering the fields for the nested modal
<%= nested_form_for [:admin, #proforma] do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :elements %>
<p><%= f.link_to_add "Add element to proforma", :elements %></p>
<p><%= f.submit %></p>
<% end %>
In my partial I want a link to take you to edit the nested model instance e.g.
<div class='element-children'>
<p>
<%= collection_select(:element, :component_id, Component.all, :id, :name) %>
<%= f.text_field :name %>
<%= f.text_field :detail %>
<%= link_to 'Edit', [:admin, edit_element_path(#element)] %> |
<%= f.link_to_remove "Destroy" %>
</p>
</div>
However #element is not available in the partial. How can I reference the correct element?
Edit...
Solution using comment from Bradley
<div class='element-children'>
<p>
<%= collection_select(:element, :component_id, Component.all, :id, :name) %>
<%= f.text_field :name %>
<%= f.text_field :detail %>
<%= link_to 'Edit', edit_admin_element_path(f.object.id) %> |
<%= f.link_to_remove "Destroy" %>
</p>
</div>

Resources