rails simple_form i18n translate fieldset legend - ruby-on-rails

Is there a posibility to use i18n-translation outside a input-field ?
I want to group my input-fields with h6-headers like:
<%= simple_form_for #table, :html => { :class => 'form-horizontal' } do |f| %>
<hr class="rw_section_top">
<h6><%= t('.header_1') %></h6>
<hr class="rw_section_bottom">
<%= f.input :name %>
The problem is, that every translation I stored in de.yml doesn't work in the _form.html.erb. When I store it in the simple_form.de.yml it doesn't work too. Only the f.input translations are working.

Related

Remote form stopped working after update to Rails 3

I've recently updated one Rails 2.x applications to Rails 3 (3.0.20 at the moment, but the ultimate goal is 3.2.x). I noticed that one remote form stopped working properly. It sends requests and new records are created, but to tell that a full page reload is required.
There is absolutely nothing special in its definition:
form_for(#comment, :remote => true, :html => html_options) do |f|
<%= f.label :username, 'Name/nickname' %>
<div class="text">
<%= f.text_field :username, :maxlength => '60' %>
</div>
<%= f.label :email, 'E-mail' %>
<div class="text">
<%= f.text_field :email, :maxlength => '120' %>
</div>
<%= f.label :content, 'Content' %>
<div class="textinput-longer">
<%= f.text_area :content %>
</div>
<%= f.submit 'Add Comment', :value => 'Add comment' %>
<% end %>
There is also an event bound to the form
$('form#comment').bind({
submit: function() {
// disable inputs and change CSS
},
ajaxComplete: function(event, response, request) {
// insert comment
new Comment(response, this);
}
});
I suppose that some script specific for Rails 2.x might be missing?
try this:
remote_form_for(#comment, :html => html_options) do |f|
or
form_for(#comment,{ :remote => true, :html => html_options}) do |f|

Using form input without updating model Ruby on Rails

I would like to put a form on one of my pages, but don't want to use form_for to update a model. I am basically using this like a filtering/searching system, where the user inputs something, and the page changes based on what the user input. I know this is a pretty simple problem, but I'm also a little new to Rails.
Note: I have the ability to filter the results if I can just get the input value. I just need access to the input value in my Controller.
Just use form_for with a symbol as argument rather than an instance variable.
You can access the form data in your controller by referencing your params, just like you normally would. Let's say you have a form kinda like this:
<%= form_for :search do |f| %>
<%= f.text_field :query %>
<%= f.submit %>
<% end %>
You'll then get the contents of the search form by calling params[:search][:query] in your controller.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
you need meta_search
<%= form_for #search, :url => articles_path, :html => {:method => :get} do |f| %>
<%= f.label :title_contains %>
<%= f.text_field :title_contains %><br />
<%= f.label :comments_created_at_greater_than, 'With comments after' %>
<%= f.datetime_select :comments_created_at_greater_than, :include_blank => true %><br />
<!-- etc... -->
<%= f.submit %>
<% end %>

Ruby On Rails, client side validation, does not work with form_tag

My client side validation doesnt work somehow.
My html.erb:
<%= form_tag what_to_do_file_path, method: :get do %>
<%= submit_tag "Delete selected", :commit =>"delete" %>
<%= submit_tag "Pictures/Info/Raw Int", :commit =>"pictures" %>
<%= submit_tag "File normalize", :commit =>"pictures" %>
<%= form_for Group.new, :validate => true do |f| %>
<div class="field_label">
<%= f.label :group_name%>:
</div>
<div class="field">
<%=f.text_field :group_name %>
</div>
<%end%>
<% #files.each do |file| %>
<p><td> <%= check_box_tag "files[]", file.id %></td><%= file.file_name %></p>
<%=end%>
<%=end%>
My controller:
class Group< ActiveRecord::Base
include ActiveRecord::Validations
attr_accessible :group_name
validates :group_name,
:uniqueness => { :case_sensitive => false}
end
I what_to_do action I catch the ids of the files, that I want to group and params[:commit], meaning what I want to do with the files in this group.
Example
File 1, File 2, File 3.
I select all those Files and type a group name "Tripple" and select what I want to do with this group (either File normalize or whatever..)
(The name of the group should be validated in case this group already exists, in what_to_do I want to catch the ids of the files, params[commit] and the group name)
The validation works if I put a form_for outside form_tag, but I need to have it in form_tag because I want to catch in what_to_do action the name of the group.
How can I do it?
why aren't you using
form_for :something, url: what_to_do_file_path, method: :get
it is always encouraged to use form_for for this kinda tasks

Routing for multiple controllers for a single resource in Rails 3.1

I asked a question similar to this one about a week ago, but this is a slightly different perspective on it. The nature of the question involves being redirected to the correct controller.
I have a single resource, posts, and I have 4 different categories these posts can be under. I want each of these categories to be particular to a single controllers, and so I have the following in my routes.rb:
resources "code", :controller => :code_posts, :as => :code
resources "sports", :controller => :sports_posts, :as => :sports
resources "gaming", :controller => :game_posts, :as => :gaming
resources "the-nation", :controller => :personal_posts, :as => :the_nation
So now I can access posts through URLs like, for example, /code/1, /sports/34 to access the same post resource, but with each controller focusing on a single scope, namely a particular category.
This is all well and good, but my issue comes up when I try to edit or save particular posts. I have the following partial _form.html.erb (rendered in the new and edit views) in all the view folders for their particular controller:
<%= form_for #post do |f| %>
<div class="field">
<%= f.label :author %><br/>
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :category %>
<%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
</div>
<div class="field">
<%= f.label :summary %><br/>
<%= f.text_area :summary, :rows => 5 %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :tag_tokens %><br/>
<%= f.text_field :tag_tokens, "data-pre" => #post.tags.map(&:attributes).to_json %>
</div>
<div class="field">
<%= f.submit "Submit" %>
</div>
<% end %>
So whenever I create or update a post, through whichever controllers, I always get redirected back to /posts/4, /posts/123, /posts/:id, whatever. I want to get redirected to the particular controller the post being edited or created lives under. So if I go to /code/new, and submit the new post, I want to be redirected to /code/1234, and not /posts/1234. How can I do this? For some reason I'm just having major mental mind blocks this morning. Thanks.
EDIT Updated <%= form_for #post do |f| %> to <%= form_for #post, :url => code_url(#post) do |f| %> and it works for /code/1/edit but not /code/new. When trying to access a new post form, I get the following error:
No route matches {:action=>"show", :controller=>"code_posts", :id=>#<Post id: nil, author: "Les Peabody", summary: nil, body: nil, created_at: nil, updated_at: nil, title: nil, category_id: 1, slug: nil>}
This is my CodePostsController#new method
def new
#post = Post.new(:category => Category.find_by_name("Programming"), :author => current_user.full_name)
end
You may specify the url in the form
<%= form_for #post, :url => gaming_path do |f| %>
You may use inheritance on the model.
The path in you form is determined by the class name, and in this case it is post.
If they mach with resources naming it should generate proper paths as well.
The dirty hack may be keeping objects path in it, I saw someone do that, but I do not recommend it too much.
I think the reason is the form_for method which takes for the update action as default the name of the parameter (here post) it gets.
So to change that, you have to add at the beginning (for the example resource code) the following:
<%= form_for #post, :url => code_path(#post) do |f| %>
This is of course only the URL for an existing object, the URL for a new object should be different. It should be there new_code_path (and no argument). So your partial should only contain the fields and labels, not the form_for call, because the URL should be different then.
You should look at the output of the call in the shell: bundle exec rake routes and search for the correct paths in the output.
So, ultimately what is important is how the form gets turned into HTML. If you look at the differences between a form that is meant for editing, and a form that is meant for a new object, there is only one thing that is ever really different that matters - the action URL.
In the case of a new form, the form tag should look something like:
<form accept-charset="UTF-8" action="/code" class="new_post" id="new_post" method="post">
and in the case of an edit form:
<form accept-charset="UTF-8" action="/code/1" class="edit_post" id="edit_post_1" method="post">
The only thing that matters to rails however is the name of the input elements (which are constant in both forms) and the action attribute in the form tag. That tells Rails whether or not it's rendering the edit or create action.
Since we're splitting up control of a single resource through multiple controllers, the standard form_for #post will not suffice since Rails can no longer automate the rendering process through convention (as we're doing a very unconventional thing). It is necessary to do some manual labor. The following will do the trick.
Convert the partial to the following:
<%= form_for #post, :url => path do |f| %>
<div class="field">
<%= f.label :author %><br/>
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :category %>
<%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
</div>
<div class="field">
<%= f.label :summary %><br/>
<%= f.text_area :summary, :rows => 5 %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :tag_tokens %><br/>
<%= f.text_field :tag_tokens, "data-pre" => #post.tags.map(&:attributes).to_json %>
</div>
<div class="field">
<%= f.submit "Submit" %>
</div>
<% end %>
The path variable in there is a variable passed in through the :locals mechanism in the partial render, like so:
new.html.erb
<%= render :partial => "form", :locals => {:path => code_index_path} %>
and edit.html.erb
<%= render :partial => "form", :locals => {:path => code_path(#post)} %>
The nice thing with this solution is you can DRY up the code too by placing _form.html.erb in app/views/layouts or app/views/posts and reuse it in all of the new and edit views for all controllers that manipulate the Post resource in a consistent fashion. So rather than having:
<%= render :partial => "form", :locals => {:path => code_path(#post)} %>
we have:
<%= render :partial => "layouts/form", :locals => {:path => code_path(#post)} %>

Turning off auto-complete for text fields in Firefox

Am supposed to be learning French at the moment, but rather than learning any vocab, I've been mucking around with a rails app that tests vocab - so it displays a word, and I have to type its translation.
Unfortunately, Firefox remembers everything I've already type there, so which diminishes its usefulness somewhat.
Is it possible, through the options for form_for or otherwise, to turn this normally useful behaviour off?
So it turns out it's pretty simple. Rather than
<%= f.text_field :fieldname %>
put
<%= f.text_field :fieldname, :autocomplete => :off %>
You can also turn off autocomplete at the form level by using the :autocomplete attribute in the :html collection, which will generate the HTML that Erv referenced. The syntax is
<% form_for :form_name, #form_name, :html => {:autocomplete => "off"} do |f|%>
...
<% end %>
Add autocomplete="off" as an attibute on your form tag:
<form action="..." method="..." autocomplete="off" >
</form>
I was using the "tag" variant of forms and found this to work:
<%= text_field_tag('favorite animal', nil, :options => {:autocomplete => 'off'}) %>
Below the tag variant for Rails 3 applications:
<%= text_field_tag :search, nil, :autocomplete => 'off' %>
This worked for me in Rails 4+
<%= f.text_field :name, autocomplete: :off %>
Nice and simple
Simple solution for me in Rails 4+
In the form i added:
:autocomplete => "off"
And in the field:
:autocomplete=>"none"
Example:
<%= form_for(#user.address_detail,
:url => {:action => :update_address},
:validate => true,
:method => :put,
:html => {:class => "form-horizontal",:autocomplete => "off"},
) do |f| %>
<div class="controls">
<%= f.text_field :address_line_1,:autocomplete=>"none" %>
</div>
If you use simple_form_for you can use autocomplete also bur only with input_html if you use it on a specific field.
For example :
<%= f.input :password, input_html: { autocomplete: "new-password" } %>

Resources