In Michael Hartl's tutorial, it is written
<%= f.label :name %>
<%= f.text_field :name %>
it becomes
<label for="user_name">Name</label>
<input id="user_name" name="user[name]" type="text" />
and
<%= f.label :email %>
<%= f.email_field :email %>
becomes
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="email" />
Was looking it for so long but I really could not understand how the code can translate to html. Can anyone explain?
These are all enclosed in the following code
<%= form_for(#user) do |f| %>
.
.
.
<% end %>
The label, <%= f.label :email %> is really just a method-call: You call form.label(:email). The <%= is short for <% print.
These methods are form-helpers, and included in the form object from FormHelper.
If you look at the code in the label method you'll see it instantiates a Tags::Label object.
Looking deeper at that Tags::Label it does a lot, but the most important part is the call to label_tag. Which renders a label tag.
The contents of the label tag is changed from :email to Email by using the humanize helper. (We ignore all the localisation and translation for now). You can use these helpers everywhere yourself. Open a console with rails console:
:email.to_s.humanize #=> "Email"
:postal_code.to_s.humanize #=> "Postal code"
In rails most of these stacks are hard to follow, because it is both abstracted into models (to allow re-use) and a lot of edge-cases such as localisation or XSS security mix in a lot of additional "cruft" that makes the path the code follows harder to understand.
But, in short, <%= f.label(:email) %> could roughly be translated to print label_tag(:email.to_s.humanize, id: :email).
Related
I'm attempting to update an existing team name. The user goes to the website, enters what they want it to be in a text box and clicks 'Enter' to submit the change. However, I'm getting a params error, even though I believe I'm sending in the id.
I've searched the internet, tried pry, deleted/altered/and updated everything I can think of, but no luck. I know this is programming 101, but I'm at a complete wall.
I've included (what I hope is) the pertinent coding, starting with the controller:
def update
#binding.pry
team = Team.find(params[:id])
team.update!(team_params)
...........
private
def team_params
params.require(:team).permit(:team_name)
end
*****html:
<div class="column">
<h1>Update Team Name</h1>
<br>
<%= form_for :team, url: team_path, method: :patch do |team| %>
<input class="text_field" value="<%= #team.name %>" team.text_field :team_name, required: true %>
<br>
<p>
<input type="submit" name="commit" value="Submit Changes" class="btn btn-success" />
</p>
</div>
I expected for the user to type what they want the team name to be updated to, and it would stick. However, I get the following error message:
ActionController::ParameterMissing in TeamsController#update
param is missing or the value is empty: team
Extracted source (around line #99):
team_path neads an id to work
<%= form_for :team, url: team_path(#team.id), method: :patch do |team| %>
You should however be good with this also
<%= form_for #team, method: :patch do |team| %>
or even
<%= form_for #team do |team| %>
I have this form:
<%= form_for(#building_shared_space, data: {abide:''}) do |f| %>
...
<div class="field">
<%= f.label :room_type, 'Room Type' %>
<%= f.text_field :room_type, placeholder: 'Room Type', required: '' %>
<%= content_tag(:small, 'Please enter a room type', class: 'error') %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :default -%>
<%= f.check_box :default %>
</div>
...
When I submit, it correctly highlights and displays a warning next to Room Type. However, it also makes the other form labels red. Why is this happening?
I'm using Foundation and Rails 4.
In order to make the <small class="error"> ... </small> work, it must be a sibling of the input. While it may look like putting the element after the input, which would be a sibling, would work, the code is affecting -all- siblings. (Which makes sense in 20/20 hind-sight.)
The wording of the documentation, can be interpreted (especially with little sleep) that the wrapping <div> is just for the sake of the example. However, having a parent element for the input control and the error message is mandatory. <DIV> just works incredibly well.
The Custom Named Patterns code example actually doesn't show a wrapping element. It also, to be fair, doesn't show an error message element. But this can mislead one to think the wrapping <div> is superfluous and just for the sake of explaining what's going on, rather than being a necessary component of making the right tree structure in the DOM to isolate validation behaviors.
I'm trying to create only a 1 page index site.
I Want to handle creating, deleting, editing all within my 1 index. (very basic)
However, I followed the beginners for getting started:
http://guides.rubyonrails.org/getting_started.html
Example of my article controller:
def index
#articles = Article.all #Create Articles and then add the for loop inside the index.html
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
end
end
def new
#article = Article.new
end
...
private
def article_params
params.require(:article).permit(:title, :text)
end
I Use #articles to populate all articles ever added. And then #article to for CRUD. Except I can't get full control to CRUD from the index.html page.
<form role="form" class="form-inline">
<div class="form-group">
<%= form_for #article do |f| %>
<div class="form-group">
<label for="textUserInputDescription"><%= f.label :title %><br/><%= f.text_field :title %>
</label>
<label for="textUserInputDescription"><%= f.label :text %><br/><%= f.text_area :text %> </label>
<button type="submit" class="btn btn-default"><%= f.submit %></button>
</div>
<% end %>
</div>
When I click Submit, It does not add to my database.
My Question is: How do I get access to CRUD all from within the index.html?
Thanks for your time, I'm still learning Rails/CRUD/ActiveRecord and following tutorials.
I changed f.submit to:
<%= button_to "Create", new_article_path(#article[:title], #article[:text]), method: :post %>
I want to create a form that does not have a label, and instead puts the directions inside the actual input text field. I also want the directions to disappear when you click the text field.
Here is an example
http://www.dailyblogtips.com/wp-content/uploads/searchform.png
I know in html, you do something like this
<input type="text" name="s" id="s" value="Text to be displayed here" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
But how can I accomplish this using rails form helpers.
Here is what I have so far
<div class="row">
<div class="small-12 columns">
<div class="panel">
<%= form_for(#message) do |f| %>
<%= f.label :body, "Description" %>
<%= f.text_area :body %>
<%= f.submit 'Create message', class: 'button small secondary' %>
<% end %>
</div>
</div>
</div>
I'd give the placeholder attribute a try, it'll only work with newer browsers but there are a few jQuery libraries that will automatically detect the ones that don't support it and perform more or less the same thing that your javscript snippet is doing. Here is an example of using the placeholder field:
<%= f.text_field :field_name, :placeholder => "Disappearing Text" %>
Edit: Here is a solid jQuery fallback plugin:
http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
In html, the pre-text in an input is called the placeholder attribute.
You can add that attribute into form_for looped fields like so:
<%= f.text_field :some_data, :placeholder => "value" %>
BUT, when doing the placeholder attribute, beware that its not completely supported across older versions of some browsers. So often some JS is required to polyfil this functionality...you should care about supporting this if you don't use traditional labels next to your input fields.
I've been converting my site from Rails 2.x to Rails 3.0, and so far so good, except for a problem I'm having with a form_for. I know to switch from remote_form_for to form_for, :remote => true, and have the new-style AJAX working fantastically well for normal hyperlinks.
With forms, though, I'm hitting a strange problem, which is that the forms are being submitted as HTML rather than JS, and are getting handled incorrectly by the controller as a result. Here's what I've got.
<% form_for (AuthorSubscription.new), :remote => true, :id => "subscribe_form" do |f| %>
<%= f.submit "Subscribe" %>
<% end %>
which results in the HTML of
<form accept-charset="UTF-8" action="/author_subscriptions" class="new_author_subscription" data-remote="true" id="new_author_subscription" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="NxCF177nMDfL6QsYjesBUOUUJ9QdzKIdZYQjGAaGYmA=" />
</div>
<input id="author_subscription_submit" name="commit" type="submit" value="Subscribe" />
</form>
You can see that the data-remote attribute is there, and all seems good. But the logger is showing me
Started POST "/author_subscriptions" for 127.0.0.1 at 2011-06-27 16:21:29 -0400
Processing by AuthorSubscriptionsController#create as HTML
Any ideas for what I'm doing wrong?
I think, you should give the format in controller:
class AuthorSubscriptionsController < ApplicationController
##ajax_calls = [:your_ajax_method]
respond_to :html, :except => ##ajax_calls
respond_to :js, :only => ##ajax_calls, :layout => false
def your_ajax_method
etc...
This happened to me, and I found that if I specifically include
<%= javascript_include_tag "jquery-1.6.2.min.js" %>
in application.html.erb, then it breaks my remote forms. I'd suggest you try removing one js script at a time until it works again. You will also want to avoid:
<%= javascript_include_tag :all %>
if you've installed jQuery.