王筝的博客
ruby学习

 

Numeric Inputs

Numbers are even easier to validate than text. For number input types, the HTML5 spec gives youattributes like minmax, and step. Each of these do pretty much what you would expect.

min and max set the minimum and maximum values that the arrows in the input will allow. stepsets the increments for between possible values. There’s also value, which sets the starting value of the input.

Of course, you’ll probably notice that users can still type whatever number they want into numeric inputs. If you want to really limit possible values, consider a range instead.

Range Inputs

The range input type creates a slider on the page. It also has minmaxstep and value attributes. If you want to display the current value of the range when it changes, you’ll need to use some JavaScript to pull the value from the range. Here’s an example:

  // grab <input id="range-example" type="range" min="0" max="5" step="1"> from the page
    var rangeInput = document.querySelector('input#range-example');

    // grab <p id="output"></p> to display the output
    var output = document.querySelector('p#output');

    // update the display when the range changes
    rangeInput.onchange = function() {
        output.innerHTML = this.value;
    };
<input type=number>
<input type=number min=100 max=999 step=5>

 

flash.now[:error] = ""
render :new


flash[:error] = ""
redirect videos_path

http://guides.rubyonrails.org/action_controller_overview.html

The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for passing error messages etc.

It is accessed in much the same way as the session, as a hash (it’s a FlashHash instance).

Let’s use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:

class LoginsController < ApplicationController
  def destroy
    session[:current_user_id] = nil
    flash[:notice] = "You have successfully logged out."
    redirect_to root_url
  end
end

Note that it is also possible to assign a flash message as part of the redirection.

You can assign :notice:alert or the general purpose :flash:

redirect_to root_url, notice: "You have successfully logged out."
redirect_to root_url, alert: "You're stuck here!"
redirect_to root_url, flash: { referral_code: 1234 }

The destroy action redirects to the application’s root_url, where the message will be displayed. Note that it’s entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It’s conventional to display any error alerts or notices from the flash in the application’s layout:

<html>
  <!-- <head/> -->
  <body>
    <% flash.each do |name, msg| -%>
      <%= content_tag :div, msg, class: name %>
    <% end -%>
 
    <!-- more content -->
  </body>
</html>

This way, if an action sets a notice or an alert message, the layout will display it automatically.

You can pass anything that the session can store; you’re not limited to notices and alerts:

<% if flash[:just_signed_up] %>
  <p class="welcome">Welcome to our site!</p>
<% end %>

If you want a flash value to be carried over to another request, use the keep method:

class MainController < ApplicationController
  # Let's say this action corresponds to root_url, but you want
  # all requests here to be redirected to UsersController#index.
  # If an action sets the flash and redirects here, the values
  # would normally be lost when another redirect happens, but you
  # can use 'keep' to make it persist for another request.
  def index
    # Will persist all flash values.
    flash.keep
 
    # You can also use a key to keep only some kind of value.
    # flash.keep(:notice)
    redirect_to users_url
  end
end
5.2.1 flash.now

By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request.

For example, if the create action fails to save a resource and you render the new template directly,

that’s not going to result in a new request, but you may still want to display a message using the flash.

To do this, you can use flash.now in the same way you use the normal flash:

class ClientsController < ApplicationController
  def create
    @client = Client.new(params[:client])
    if @client.save
      # ...
    else
      flash.now[:error] = "Could not save client"
      render action: "new"
    end
  end
end

 

tr的id和data-target不一致

 

<tbody>
        <tr id="homepage_video-13" class="homepage_video" data-sort-path="/homepage_videos/13/sort">
  <td class="sequence">
    <span class="handle">
      <span></span>
      <span></span>
    </span>
    3
  </td>
  <td class="show_id">a803862414c711e5b2ad</td>
  <td class="title">夏洛特烦恼</td>
  <td class="large_horizontal_image"><img src="http://r2.ykimg.com/050C000055E95E6367BC3C11430981EB" alt="050c000055e95e6367bc3c11430981eb"></td>
  <td class="created_by">王</td>
  <td><a class="active_toggle" data-confirm="确定关闭吗?" data-target="#homepage-video-13" data-active="true" data-remote="true" rel="nofollow" data-method="patch" href="/homepage_videos/13/toggle_active.json"><i class="fa fa-check-square-o"></i></a></td>
  <td>
    <a class="btn btn-default btn-xs" href="/homepage_videos/13">详情</a>
    <a class="btn btn-default btn-xs" href="/homepage_videos/13/edit">编辑</a>
    <a data-confirm="Are you sure?" class="btn btn-xs btn-danger" rel="nofollow" data-method="delete" href="/homepage_videos/13">删除</a>
  </td>
</tr>

 

http://api.rubyonrails.org/v3.0.8/

在官网搜索link_to会有一些详细的介绍,

这里只把我自己的例子记下来

        <%= link_to("删除多余视频", delete_useless_video_cibn_videos_path(:module_id => @module_id, :version => @version),
                     method: :post, :class => "button", :confirm => "亲,你将要删除本页面序号大于30的视频,确定删除?") %>

 

 

在model里新建

  CHANNEL_TYPES = { 
    '信号源': :signal_source,
    '直播轮播': :live_and_rotate,
    '我的': :my_channel,
    'C 频道': :c_channel
  }

并增加方法

  def channel_type_human
    CHANNEL_TYPES.invert[channel_type.to_sym]
  end 

在view/form里可以直接用

      <div class="form-group">
        <%= f.label :channel_type,"频道类型", class: "col-sm-2 control-label" %>
        <div class="col-sm-2">
          <%= f.select :channel_type, options_for_select(LauncherChannel::CHANNEL_TYPES, @launcher_channel.channel_type),
            {}, class: "form-control" %>
        </div>
      </div>

之前没有加{}, 由于默认是四个参数的,最后一个才是html_option

select(method, choices = nil, options = {}, html_options = {}, &block)

Wraps ActionView::Helpers::FormOptionsHelper#select for form builders:

<%= form_for @post do |f| %>
  <%= f.select :person_id, Person.all.collect { |p| [ p.name, p.id ] }, include_blank: true %>
  <%= f.submit %>
<% end %>
Please refer to the documentation of the base helper for details.

Source: hide | on GitHub

# File actionview/lib/action_view/helpers/form_options_helper.rb, line 776
def select(method, choices = nil, options = {}, html_options = {}, &block)
  @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options), &block)
end

 

<%= hidden_field_tag :version,@version %>
<%= hidden_field_tag :module_id,@video.parent_module_id%>

{“utf8″=>”✓”, “authenticity_token”=>”fixQgc0R4wHIn6fbE+Kkf+JC7oQP/WuNBBCDID6Q5Fw=”, “version”=>”1″, “module_id”=>”1″, “video”=>{“video_id”=>””, “show_id”=>””, “title”=>””, “res_desc”=>””, “large_horizontal_image”=>””, “middle_horizontal_image”=>””, “large_vertical_image”=>””, “middle_vertical_image”=>””}, “commit”=>”保存”}

==========================================

<%= v.hidden_field :version %>
<%= v.hidden_field :parent_module_id %>

Parameters: {“utf8″=>”✓”, “authenticity_token”=>”fixQgc0R4wHIn6fbE+Kkf+JC7oQP/WuNBBCDID6Q5Fw=”, “video”=>{“version”=>”1″, “parent_module_id”=>”1″, “video_id”=>”qq”, “show_id”=>”qq”, “title”=>”q”, “res_desc”=>”qq”, “large_horizontal_image”=>””, “middle_horizontal_image”=>””, “large_vertical_image”=>””, “middle_vertical_image”=>””}, “commit”=>”保存”}

http://guides.rubyonrails.org/v3.0.8/action_controller_overview.html#the-flash

如何在页面显示controller里的错误提示,

The flash is a special part of the session which is cleared with each request.This means that values stored there will only be available in the next request,which is useful for storing error messages etc. It is accessed in much the same way as the session,ike a hash. Let’s use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:

flash是session特殊的一部分,每次请求的时候都会被清除,这意味着存储的数据只有在下一次请求的过程中才有效.这一特性被广泛的应用于存储错误信息提示,获取它的方式与session近似,类似于哈希.让我们以退出为例子,controller发送的信息将在用户下次请求的时候展示出来.

class LoginsController < ApplicationController

  def destroy
    session[:current_user_id] = nil
    flash[:notice] = "You have successfully logged out"
    redirect_to root_url
  end
end
%11%
Note it is also possible to assign a flash message as part of the redirection.
注意:也可以在页面重定向的时候指定flash的信息
redirect_to root_url, :notice => "You have successfully logged out"
The destroy action redirects to the application’s root_url, where the message will be displayed. Note that it’s entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It’s conventional to display eventual errors or notices from the flash in the application’s layout:
注销登录的操作会使得页面重定向到根目录,这时候信息也会显示在页面上.这完全取决于下一步操作是什么,总之,上一次请求的信息将会被存入flash,通常将会把最终的错误信息或者提示信息展示到页面上.
<html>
  <!-- <head/> -->
  <body>
    <% if flash[:notice] %>
      <p class="notice"><%= flash[:notice] %></p>
    <% end %>
    <% if flash[:error] %>
      <p class="error"><%= flash[:error] %></p>
    <% end %>
    <!-- more content -->
  </body>
</html>

This way, if an action sets an error or a notice message, the layout will display it automatically.

通过这种方式, 如果操作过程中出现的错误提示或者通知,会自动的加载到页面上

If you want a flash value to be carried over to another request, use the keep method:

如果你想把提示信息带到其他请求中使用,使用keep方法

class MainController < ApplicationController
  # Let's say this action corresponds to root_url, but you want
  # all requests here to be redirected to UsersController#index.
  # If an action sets the flash and redirects here, the values
  # would normally be lost when another redirect happens, but you
  # can use 'keep' to make it persist for another request.
  def index
    # Will persist all flash values.
    flash.keep
    # You can also use a key to keep only some kind of value.
    # flash.keep(:notice)
    redirect_to users_url
  end
end

 

By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the create action fails to save a resource and you render the new template directly, that’s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can useflash.now in the same way you use the normal flash:

默认状态下, flash里的值会在下一次请求的时候有效, 但是有的时候,你可能希望在同一个请求里使用这些值,比如新建的时候保存数据失败并跳转到一个新的页面里,flash不会出现在这个新的请求里.如果依然希望通过flash来显示错误信息,可以直接使用flash.now方法.

class ClientsController < ApplicationController
  def create
    @client = Client.new(params[:client])
    if @client.save
      # ...
    else
      flash.now[:error] = "Could not save client"
      render :action => "new"
    end
  end
end

 

form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)

Starts a form tag that points the action to an url configured with url_for_options just like ActionController::Base#url_for. The method for the form defaults to POST.

Options

  • :multipart – If set to true, the enctype is set to “multipart/form-data”.
  • :method – The method to use when submitting the form, usually either “get” or “post”. If “put”, “delete”, or another verb is used, a hidden input with name _method is added to simulate the verb over post.
  • A list of parameters to feed to the URL the form will be posted to.
  • :remote – If set to true, will allow the Unobtrusive JavaScript drivers to control the submit behaviour. By default this behaviour is an ajax submit.

Examples

 form_tag('/posts')
 # => <form action="/posts" method="post">

 form_tag('/posts/1', :method => :put)
 # => <form action="/posts/1" method="put">

 form_tag('/upload', :multipart => true)
 # => <form action="/upload" method="post" enctype="multipart/form-data">

 <%= form_tag('/posts') do -%>
   <div><%= submit_tag 'Save' %></div>
 <% end -%>
 # => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>

<%= form_tag('/posts', :remote => true) %>
 # => <form action="/posts" method="post" data-remote="true">
  # File actionpack/lib/action_view/helpers/form_tag_helper.rb, line 49
def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)
  html_options = html_options_for_form(url_for_options, options, *parameters_for_url)
  if block_given?
    form_tag_in_block(html_options, &block)
  else
    form_tag_html(html_options)
  end
end

——————————————————-

<%= form_tag(“#{Settings.cache_clear_link.host}&req_time=#{@time}&req_from=cms&sign=#{@sign}”) do %>
<%= submit_tag “[立即生效]” ,:class => “button” %>
<% end %>

1.radio_button_tag

new:
<%= radio_button_tag “feedback_type”,”T-1″ %>
<label for=’T-1′ style=’display:inline’ >T-1</label>&nbsp;&nbsp;
<%= radio_button_tag “feedback_type”,”T-2″ %>
<label for=’T-2′ style=’display:inline’>T-2</label>&nbsp;&nbsp;

edit:

<%= radio_button_tag “feedback_type”,”T-1″, @feedback.feedback_type == “T-1″%>
<label for=’T-1′ style=’display:inline’ >T-1</label>&nbsp;&nbsp;
<%= radio_button_tag “feedback_type”,”T-2″, @feedback.feedback_type == “T-2″ %>
<label for=’T-2′ style=’display:inline’>T-2</label>&nbsp;&nbsp;

2.text_field_tag

<%= text_field_tag “title” %>

3.image_tag

<% if @feedback.image != nil %>
<%= image_tag @feedback.image, :width => 200 %><br/>
<% end %>
<%= s.text_field :image %>

4.select

<%= f.select :tag_type, options_for_select([[“ugc”,1],
[“豆单”,2],
[“剧集”,3]],@secondchannel.tag_type) %>

<%= f.select :layout_type, options_for_select([“album”,”item”],@secondchannel.layout_type) %>