How can I set up collection select correctly? - ruby-on-rails

With the code below, if I hit Search it takes me to these url
With FireFox > http://example.com/shop?utf8=%E2%9C%93&genre[]=1
With Safari > http://example.com/shop?utf8=✓&genre%5B%5D=1
How can I remove this "[]" and "%5B%5D"?
Only I want is "&genre=1"
View code
<%= form_tag communities_path, :method => :get, :class => 'form-search' do %>
<div class="input-append">
<%= collection_select :genre, nil, Genre.all, :id, :name %>
<button type="submit" class="btn">Search</button>
</div>
<% end %>

<%= select_tag :genre, options_for_select(Genre.all.map{ |g| [g.name, g.id] }) %>

Related

setting a select tag on Ruby on Rails

<%= form_tag("/posts/create") do %>
<div class="form">
<div class="form-body">
<% #post.errors.full_messages.each do |message| %>
<div class="form-error">
<%= message %>
</div>
<% end %>
<div class="continent">
<label>continent<br>
<%= #post.continent %>
<%= select :continent,
[["Africa","AF"],["North America","NA"],["Oceania","OC"],["Asia","AS"],["Europe","EU"],["South America","SA"]],
:prompt => "Select" %>
</label>
</div>
<div class="country">
<label>country<br>
<%= #post.country %>
<%= select :country,
[["Japan","JP"],["China","CH"]],
:prompt => "Select" %>
</label>
</div>
<div class="title">
<label>title<br>
<textarea name="title"><%= #post.title %></textarea>
</label>
</div>
<input type="submit" value="POST">
</div>
</div>
<% end %>
I want to set a select tag for choosing user's continent and country in the form
But It doesn't work well
I've tried some way to solve this problem.
And I just got the shape which is like "Select tag" but it's only included "prompt"
Please give me some advice.
FormOptionsHelper`s select takes these args
select(object, method, choices = nil, options = {}, html_options = {}, &block)
Try explicit putting options like this:
select :post, :country, [["Japan","JP"],["China","CH"]], { :prompt => "Select" }, { other_html_options }
If you are in a form context make this
<%= form_tag("/posts/create") do |form| %>
<%= form.select :country, [["Japan","JP"],["China","CH"]], { :prompt => "Select" }, { other_html_options } %>
<% end %>
First start off by creating the correct routes:
resources :posts
In Rails you create a resource by sending a POST request to the collection path ('/posts').
Then create the form with form_for(#post) or form_with(model: #post) (Rails 5.1+) and bind the model instance to the form builder.
<%= form_for(#post) do |f| %>
<div class="form">
<div class="form-body">
<% f.object.errors.full_messages.each do |message| %>
<div class="form-error">
<%= message %>
</div>
<% end %>
<div class="continent">
<%= f.label :continent do %>
<%= f.select :continent,
[["Africa","AF"],["North America","NA"],["Oceania","OC"],["Asia","AS"],["Europe","EU"],["South America","SA"]],
prompt: "Select" %>
<% end %>
</div>
<div class="country">
<%= f.label :country do %>
<%= f.select :country,
[["Japan","JP"],["China","CH"]],
:prompt => "Select" %>
<% end %>
</div>
<div class="title">
<%= f.label :title do %>
<%= f.text_area_field :title %>
</div>
<%= f.submit %>
</div>
</div>
<% end %>
This lets you reuse the same form for updating the resource. Also when you use the input helpers rails will properly name the inputs so that you can whitelist them with:
params.require(:post).permit(:continent, :country, :title)

Prepopulate complex form values in rails edit view

I have an assessment form that renders a list of questions, and accepts answers for each question. The form data is submitted manually via ajax.
I'm having an issue when trying to pre-populate the data in my edit view. Here's how my views look:
edit.html.erb:
<div class="container">
<%= render 'assessment_form', :locals=> {:assessment => #assessment} %>
</div>
_assessment_form.html.erb:
<%= form_for #assessment do |f| %>
<%= f.submit 'Save', :class=> "btn btn-primary", :style=>"width: 100%;" %>
<div class="col-sm-2 col-sm-offset-10">
<h4>Show on Followup?</h4>
</div>
<% #assessment.questions.where(category:"S").each do |question| %>
<%= render 'question_fields', :question=> question, :f=> f %>
<% end %>
<%= f.hidden_field :patient_id, :value=> #assessment.patient.id %>
<%= f.hidden_field :template_id, :value=> #assessment.template_id %>
<% end %>
_question_fields.html.erb:
<p><%= question.content %></p>
<%= f.fields_for :answer do |builder| %>
<div class="row question">
<div class="col-sm-2 pull-right toggle">
<%= builder.check_box :tracking, {:checked=> false, :class=>"trackable", :data => {:'on-text' => "Yes", :'off-text' => "No", :'on-color'=> "success", :question => question.id}}, 0 , 1 %>
</div>
<div class="col-sm-10">
<%= builder.text_area :content, :class=>"form-control question-field", :data => {:question => question.id} %>
</div>
</div>
<% end %>
But none of the answers content are rendering in the view, the form fields are blank.
Here's an example call: Assessment.last.answers.first.content will give me the string "test". That should be showing up in my text_area box but it's not.
Just do, the instance variable #assessment is passed to the partial.
<%= render 'assessment_form' %>

Rails 4 - How to populate remote content in a form?

Here is what I have
I have a method like this in my bookmarks_controller.rb
def fetch
#result = LinkThumbnailer.generate(params[:url])
end
I have the following form fields to create new bookmarks
url
title
description
thumbnail
tags
So my new.html.erb looks like this
<%= form_for(#bookmark, :html => {class: "panel-body"}) do |f| %>
<% if #bookmark.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#bookmark.errors.count, "error") %> prohibited this bookmark from being saved:</h2>
<ul>
<% #bookmark.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="block">
<%= f.label :url, :class => 'control-label' %>
<%= f.text_field :url, :class => 'form-control', :placeholder => 'URL' %>
</div>
<div class="block">
<%= f.label :title, :class => 'control-label' %>
<%= f.text_field :title, :class => 'form-control', :placeholder => 'Title' %>
</div>
<div class="block">
<%= f.label :description, :class => 'control-label' %>
<%= f.text_area :description, rows: 6, :class => 'form-control', :placeholder => 'Description' %>
</div>
<div class="block">
<%= f.label :thumbnail, :class => 'control-label' %>
<%= f.text_field :thumbnail, :class => 'form-control', :placeholder => 'Thumbnail url' %>
</div>
<div class="block">
<%= f.label :tag_list, :class => 'control-label' %>
<%= f.text_field :tag_list, :class => 'form-control', :placeholder => 'Tags (separated by commas)' %>
</div>
<div class="actions">
<%= f.submit :class => "btn btn-info" %>
</div>
<% end %>
I would like to have a button called Fetch right next to url field.
When user click Fetch button, I would like to populate the form fields using ajax.
I viewed rails documentation. It says to add remote: true attribute to the form.
But I wanna use ajax function only when the user click fetch button.
Can someone help me with this?
Thankyou
You can generate an object using the LinkThumbnailer gem and than convert it to json
with this action:
def fetch
#result = LinkThumbnailer.generate(params['given_url'])
respond_to do |format|
format.json do
result.to_json
end
end
end
And in your view you could use the Jquery getJSON method to access the #fetch action and pass the given url as a parameter:
<script type="text/javascript">
require(['jquery'], function($) {
$('.fetch-button-class').change(function() {
var given_url = $('.url-class').val();
$.getJSON('<%= fetch_action_url %>', {'given_url': given_url }, function(obj) {
$('.title-class').val(object.title);
$('.description-class').val(object.description);
});
});
});
</script>

ruby: updating file upload markup - cannot work it out

i have only been introduced to ruby this week on a new clients project and been updating some file inputs however i have got to one that is different and cannot work out how to write into the new markup i need. can anyone lend a hand?
currently it is:
<div class="input inline">
<% if host_group %>
<%= label :upload_csv_of_hosts, t(:upload_csv_of_hosts), t(:upload_csv_of_hosts) %>
<% else %>
<%= label :upload_csv_of_participants, t(:upload_csv_of_participants), t(:upload_csv_of_participants) %>
<% end %>
<div class="markup">
<%= file_field_tag "csv" %>
<%=show_required %>
<%= host_group ? show_info('group/hosts_csv') : show_info('group/participants_csv') %>
</div>
</div>
which is just using file_field_tag "csv" but i need to build that into this markup like i have for the others i converted, the others i converted was done in a different way that the one above so unsure what that one does and how to implement it into the required markup below.
<div class="input inline">
<%= f.label :filename, t(:uploaded_data), :class => "double" %>
<div class="markup">
<%= f.text_field :filename, {:size => 20, :readonly => true, :class => "file_input_textbox", :value => "No File Selected"} %>
<div class="file_input_div">
<%= f.button :browse, :class => "button button-red file_input_button" %>
<%= f.file_field :filename, {:size => 20, :class => "file_input_hidden"} %>
</div>
</div>
<%=show_required%>
<%=show_info('document/uploaded_data') %>
</div>
i have tried this....
<div class="input inline">
<% if host_group %>
<%= label :upload_csv_of_hosts, t(:upload_csv_of_hosts), t(:upload_csv_of_hosts) %>
<% else %>
<%= label :upload_csv_of_participants, t(:upload_csv_of_participants), t(:upload_csv_of_participants) %>
<% end %>
<div class="markup">
<%= f.text_field :csv, {:size => 20, :readonly => true, :class => "file_input_textbox", :value => "No File Selected"} %>
<div class="file_input_div">
<%= f.button :browse, :class => "button button-red file_input_button" %>
<%= f.file_field :csv, {:size => 20, :class => "file_input_hidden"} %>
</div>
</div>
<%=show_required %>
<%= host_group ? show_info('group/hosts_csv') : show_info('group/participants_csv') %>
</div>
but i get the following error:
undefined local variable or method `f' for #<#<Class:0x00000007672a58>:0x00000007631a30>
its 1:25am and really want to crack this, so very thankful to any helpers!
Thanks
If you are getting this error:
undefined local variable or method `f' for #<#:0x00000007631a30>
When you run this line:
<%= f.text_field :csv, {:size => 20, :readonly => true, :class => "file_input_textbox", :value => "No File Selected"} %>
And you start your form like this:
<%= form_for(:group, :url => url, :html => { :snipped => true }) do %>
Then you need this instead:
<%= form_for(:group, :url => url, :html => { :snipped => true }) do |f| %>
You can't use the form builder object if your form_for block doesn't yield it!
Also do you see how much of relevant bits of code here were not in your original version of the question? :)

ruby on rails select not defaulting to current value

I'm currently using a select as follows (within a form):
<% form_for :search, :url => search_path, :html => {:method => :get} do |f| %>
<%= select('search', :type, options_for_select(['Artist', 'Track'])) %>
<%= f.text_field :query %>
<% end %>
This works, but when I perform a search, it defaults back to artist even if the user selected Track before searching. How can I correct this?
Automatically selecting the current value works for radio buttons:
<% form_for #search, :url => search_path, :html => {:method => :get} do |f| %>
<p class="radio_button">
<%= f.label :type_track, 'Search tracks' %>
<%= f.radio_button :type, 'Track' %>
</p>
<p class="radio_button">
<%= f.label :type_artist, 'Search artists' %>
<%= f.radio_button :type, 'Artist' %>
</p>
<p class="text_field">
<%= f.label :query, 'Search query' %>
<%= f.text_field :query, :class => 'auto_focus' %>
</p>
<p class="submit">
<%= submit_tag 'Go' %>
</p>
<% end %>
Any help getting this to work will be greatly appreciated!
You must use the select form builder:
<%= f.select(:type, [["text1", "value1"], ["text2", "value2"], ...]) %>

Resources