html_ruby 应用程序/视图/父母/ new.html.erb

应用程序/视图/父母/ new.html.erb

new.html.erb
<%= form_for(@parent) do |f| %>
	… parent attributes go here …
	<ul>
	<%= f.nested_fields_for :terms do |kids_form| %>
		<li>
			<%= kids_form.text_field :name %>
		</li>
	<% end %>
	</ul>
				
	<%= f.submit "Create Parent" %>
<% end %>

html_ruby _form.html.erb

user_group_dashboard.rb
# example

#...
  ATTRIBUTE_TYPES = {
    group: Field::BelongsTo,
    user: BelongsToScoped
  }
#...
user.rb
# example

class User < ApplicationRecord
  belongs_to :group
  belongs_to :user
  
  # this method is called in form/fields/belongs_to_scoped/_form
  def user_candidates
    group ? User.where(locale: group.locale) : User.all
  end
end
belongs_to_scoped.rb
class BelongsToScoped < Administrate::Field::BelongsTo
  def associated_resource_options(candidate_resources)
    [nil] + candidate_resources.map do |resource|
      [display_candidate_resource(resource), resource.send(primary_key)]
    end
  end
  def to_partial_path
    case page
    when :form; "/fields/belongs_to_scoped/form"
    else "/fields/belongs_to/#{page}"
    end
  end
end
_form.html.erb
# views/fields/belongs_to_scoped/_form.html.erb

<div class="field-unit__label">
  <%= f.label field.permitted_attribute %>
</div>
<div class="field-unit__field">
  <%= f.select(field.permitted_attribute) do %>
    <%= options_for_select(field.associated_resource_options(f.object.send("#{field.name}_candidates")), field.selected_option) %>
  <% end %>
</div>

html_ruby 渲染部分

渲染部分

layoutForm.html.erb
<%= form_for([@article, @article.comments.build]) do |f| %>
  <p>
    <%= f.label :commenter %><br>
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>
layoutComment.html.erb
<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>
 
<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>
articlesShow.html.erb
<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>
 
<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>
 
<h2>Comments</h2>
<%= render @article.comments %>
 
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
 
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>

html_ruby Rails Link Helpers

Rails Link Helpers

links.html.erb
<!-- Gets all tweets with links to some basic CRUD routes -->
<% Tweet.all.each do |tweet| %>
  <tr>
    <td><%= link_to tweet.status, tweet %></td>
    <td><%= link_to tweet.user.name, tweet.user %></td>
    <td><%= link_to "Edit", edit_tweet_path(tweet) %></td>
    <td><%= link_to "Destroy", tweet, method: :delete %></td>
  </tr>
<% end %>

<!-- Confirmation on link -->
<%= link_to tweet.user.name, tweet.user, confirm: "Are you sure?" %>

<!-- Condition if empty -->
<% tweets = Tweet.all %>
<% tweets.each do |tweet| %>
...
<% end %>
<% if tweets.size == 0 %>
<p>No tweets found.</p>
<% end %>

html_ruby 很好的方式在表单之间切换和更改链接文本

很好的方式在表单之间切换和更改链接文本

js.coffee
	# Clone
	# -------------------------------------------------------------------------
	# make sure $(".clone_pricesheet_forms") are hidden
	$clone_pricesheet_links = $("a.clone_pricesheet_links").click (e) ->
		e.preventDefault()
		$link = $(@)
		$form = $link.next()

		# Remove current form that is selected so we can hide the rest
		# Makes toggling the current form easy
		$(".clone_pricesheet_forms").not( "##{$form.attr('id')}" ).hide()

		$form.toggle ->
			if $(@).is(':visible')
				$link.text 'Cancel'
			else
				$link.text 'Clone'
			return
index.html.erb
			<% pricesheets.each do |pricesheet| %>
			<tr>
				<td>
					<%= link_to "Clone", "", class: "clone_pricesheet_links" %>

					<%= form_tag clone_pricesheet_url(pricesheet), method: :get, id: "clone_pricesheet_form_#{pricesheet.id}", class: "clone_pricesheet_forms" do %>
						<%= select_tag :pl_id, options_for_select(Pricelevel.all.map { |p| [p.name, p.id] }) %>
						<%= select_tag :pl_year, options_for_select(PricelevelYear.pluck(:year)) %>
						<%= submit_tag "Proceed to CLONE" %>
					<% end %>
				</td>
				<td><%= pricesheet.id %></td>
			</tr>
			<% end %>

html_ruby Ruby on Rails:for_form示例

Ruby on Rails:for_form示例

for_form.erb
<%= form_for @user do |f| %>
  Name: <%= f.text_field :name %><br />
  Email: <%= f.text_field :email %><br />
  Password: <%= f.text_field :password %><br />
  Confirm Password: <%= f.text_field :password_confirmation %><br />
  <%= f.submit "Create User" %>
<% end %>

html_ruby Ruby on Rails:视图中的循环和其他技巧

Ruby on Rails:视图中的循环和其他技巧

views.html.erb
<!--# Flash notice-->
<% flash.each do |key, value| %>
	<%= content_tag :li, value, class: "flash #{key}" %>
<% end %>

<!--# Flash errors-->
<% flash.each do |key, value| %>
	<%= content_tag :li, value.join(". "), class: "flash #{key}" %>
<% end %>

<!--# Table listing database entries-->
<tbody>
	<% @class.each do |student| %>
		<tr>
			<td><%= student.first_name %></td>
			<td><%= student.last_name %></td>
			<td><%= student.email %></td>
		</tr>
	<% end %>
</tbody>

<!--# Form list with selected value-->
<% @dojos.each do |dojo| %>
	<% if dojo == @current_dojo %>
    		<option selected value="<%= dojo.id %>"><%= dojo.branch %></option>
	<% else %>
    		<option value="<%= dojo.id %>"><%= dojo.branch %></option>
  	<% end %>
<% end %>

<!--# Form input with existing value-->
<input type="text" name="student[name]" value="<%= @student.name %>" />

<!--# Navigation using routes-->
<%= link_to "Home", root_path %> |
<%= link_to "#{@student.name}", dojo_student_path(@student.dojo_id, @student) %>

<!--# Conditional on relationship count-->
<% if user.tweets.count > 1 %>
<% end %>

html_ruby 使用link_to_if代替许多link_to

使用link_to_if代替许多link_to

_navbar.html.erb
<%=
link_to_if(action_name != 'index', "Back to #{controller_name.pluralize}", { controller: controller_name }, class: 'btn btn-link float-right') do
  link_to("Add #{controller_name.singularize.titleize}", { controller: controller_name, action: "new" }, :class => "btn btn-success float-right")
end
%>

<!-- Replaces -->

<% if controller_name == 'newspapers' && action_name != 'index' %>
  <%= link_to 'Back to Newspapers', newspapers_path, :class => 'btn btn-link float-right' %>
<% elsif controller_name == 'newspapers' && action_name == 'index' %>
  <%= link_to 'Add Newspaper', new_newspaper_path, class: 'btn btn-success float-right'%>
<% elsif controller_name == 'settings' && action_name != 'index' %>
  <%= link_to 'Back to Settings', settings_path, :class => 'btn btn-link float-right' %>
<% end %>

html_ruby 更新表单示例

更新表单示例

edit.erb
<form method="post" action="/entries/<%= @entry.id %>">
  <input type="hidden" name="_method" value="put">

  <label for="title-input" class="block mar-b-1">Title:</label>
  <input id="title-input" name="entry[title]" type="text" value="<%= @entry.try(:title) %>" tabindex="1" class="block w-100 no-outline no-border pad-1 mar-b-2">

  <label for="body-textarea" class="block mar-b-1">Body:</label>
  <textarea id="body-textarea" name="entry[body]" tabindex="2" class="block w-100 h-10 no-resize no-outline no-border no-resize pad-1 mar-b-2"><%= @entry.try(:body) %></textarea>

  <input type="submit" value="Update" tabindex="3" class="block button w-100 mar-t-4 mar-b-3 pad-2 round-1 text-c center no-border no-outline">
</form>

html_ruby 主要观点

主要观点

show.erb
to do
login2.erb
<container class='login'>

  <%= erb :'_error' %>

  <form class="login-form" method="post" action="/sessions">
    <label>Email or Username:</label><br>
    <input id="email" type="email" name="email" /><br>
    <label>Password:</label><br>
    <input id="password" type="password" name="password" /><br>
    <input type="submit" value="submit"/>
  </form>
</container>
login1.erb
<div class="container">

  <% if @errors %>
    <% @errors.each do |error| %>
      <p class="error"> <%= error %> </p>
    <% end %>
  <% end %>

  <h1>Log in</h1>
  <form action="/sessions" method="post">
    <label for:"email"> Email address </label>
    <input type="text" name="account[email]" value="<%= @user.try(:email) %>"><br>
    
    <label for:"password"> Password </label>
    <input type="password" name="account[password]"><br>
    
    <input type="submit" value="Submit">
  </form>

</div>
layout.erb
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- 
    normalize.css removes cross-browser differences in defaults, e.g.,
    differences in how form elements appear between Firefox and IE
    See: http://necolas.github.com/normalize.css/
  -->
  <link rel="stylesheet" href="/css/normalize.css">
  <!--
    application.css is where you put your styles
  -->
  <link rel="stylesheet" href="/css/application.css">

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script src="/js/application.js"></script>

  <title></title>
</head>
<body>
  <%=erb :_nav %>
  <%= yield %>
</body>
</html>
index_basic_list.erb
<div class="container">
  <h1>Channels</h1>
  <ul>
    <% @channels.each do |channel| %>
      <li><a href='/channels/<%= channel.id %>'><%= channel.list_display %></a></li>
    <% end %>
  </ul>
</div>
create_user2.erb
<container class='registration'>

  <%= erb :'_error' %>

  <form class="new-user-form" method="post" action="/users">
    <label>Username:</label><br>
    <input id="first-name" type="text" name="user[username]" /><br>
    <label>Email:</label><br>
    <input id="email" type="email" name="user[email]" /><br>
    <label>Password:</label><br>
    <input id="password" type="password" name="user[password]" /><br>
    <input type="submit" value="submit"/>
  </form>
</container>
create_user1.erb
<div class="container">

  <% if @errors %>
    <% @errors.each do |error| %>
      <p class="error"> <%= error %> </p>
    <% end %>
  <% end %>

  <h1>Create account</h1>
  <form action="/users" method="post">
    <label for:"first_name"> First name </label>
    <input type="text" name="account[first_name]" value="<%= @user.try(:first_name) %>"><br>

    <label for:"last_name"> Last name </label>
    <input type="text" name="account[last_name]" value="<%= @user.try(:last_name) %>"><br>

    <label for:"email"> Email address </label>
    <input type="text" name="account[email]" value="<%= @user.try(:email) %>"><br>
    
    <label for:"password"> Password </label>
    <input type="password" name="account[password]"><br>
    
    <input type="submit" value="Submit">
  </form>

</div>