ãã«ãã¦ã³ãªã¹ãã§ãã¼ã¯ã¼ãã®çµãè¾¼ã¿
ç¾ç¶ã®ãã¼ã¯ã¼ããçµãè¾¼ãæä½ã¯ãçµãè¾¼ã¿ãããã¼ã¯ã¼ãã®ãªã³ã¯ãã¯ãªãã¯ãããããä¸åº¦ã¯ãªãã¯ããã¨ãçµãè¾¼ã¿è§£é¤ããããããã ã¨ãç¾å¨ã®ãã¼ã¸ã«è¡¨ç¤ºããã¦ããªããã¼ã¯ã¼ãã§çµãè¾¼ã¿ããæã«å°ã£ã¦ãã¾ããæä½ç°å¢ã¨ãã¦ã¯ããã«ãã¦ã³ãªã¹ãã表示ããã¦ãã¦ãããããé¸æãããã¼ã¯ã¼ãã«çµãè¾¼ã¾ããæ¹ãããã
ãã«ãã¦ã³ãªã¹ãã®åãæ±ã
ãã«ãã¦ã³ãªã¹ãã¯ãç·¨éãæ°è¦ä½æã®ãã¼ã¸ã§æ¢ã«å©ç¨(app/views/models/softwares/_edit2_keyword.rhtml)ãã¦ããã®ã§ãåãä»çµã¿ã§ç°¡åã«å®ç¾ã§ããã¨ãã£ã¦ã¿ã...
collection_select
åãã¡ã½ããcollection_selectãå©ç¨ããã¨ãé¸ææ¸ã¿ã®ãã¼ã¯ã¼ãããSoftwareã¢ãã«ã®keyword_idã¨ãã¦ãã¤ã³ã¹ã¿ã³ã¹ãã¨ä¿åããªãã¨ãã¾ããããªã...ã
# ã³ã³ããã¼ã©ã¼ @software = Software.new(:keyword_id => 1)
<%# ãã¥ã¼ %> <%= collection_select :software, :keyword_id, Keyword.find_all, :id, :name, :include_blank => true %>
- åãªãIDã¨ããæ°å¤ãæ±ãããã ããªã®ã«ãããã ã¨ç¡é§ãå¤ãã
- ããã«ãã¼ã¯ã¼ãã®ãªã³ã¯ãã¯ãªãã¯ãã¦çµãè¾¼ãæä½ãæ®ãããã®ã§ã工夫ããªãã¨ãã¾ãé£æºã§ããªãã
select
selectã¡ã½ããã«ã¯ã:selectedã¨ãããªãã·ã§ã³ãããã以ä¸ã®ããã«ããã°ãSoftwareã¢ãã«ã¨ã¯ç¡é¢ä¿ã«ãæ°å¤ã ããæ±ããã¨ãã§ãããã ã
# ã³ã³ããã¼ã©ã¼ @keyword_id = 1
<%# ãã¥ã¼ %> <%= select :software, :keyword_id, Keyword.find_all.collect {|p| [ p.name, p.id ] }, :selected => @keyword_id, :include_blank => true %>
ãã¼ã¯ã¼ãã®çµãè¾¼ã¿ã«ã¯ãselectã¡ã½ãããå©ç¨ãããã¨ã«æ±ºå®ã
-
-
- collection_selectã§ãã:selectedã:selected_valueã§ãªãã·ã§ã³è¨å®ãã¦ã¿ãããæå®ããå¤ãé¸æãã¦ãããªã...ã
-
å¤ãç£è¦ãã¦ãå¤åãããçµãè¾¼ã
ãã¼ã¯ã¼ãã®ãã«ãã¦ã³ãªã¹ãã¯è¡¨ç¤ºãããããã«ãªã£ããããã¼ã¯ã¼ããé¸æãã¦ããä½ãèµ·ãããªã...ãããã¯ãã©ã¼ã ã®éä¿¡ããã¦ããªãããã ããã©ã¼ã ã®éä¿¡ã®ããã«ã¯ãçµãè¾¼ã¿ããã¿ã³ãä½ãã°ããã®ã ãããã®é½åº¦ãã¿ã³ãæ¼ãã®ã¯é¢åã ãé¸æãããã¼ã¯ã¼ãã§èªåçã«çµãè¾¼ãããã«ããããRailsãç¨æãã¦ãããã¡ã½ãããobserve_fieldã使ã£ã¦ã¿ãã
<%= select :software, :keyword_id, Keyword.find_all.collect {|p| [ p.name, p.id ] }, :selected => @keyword_id.to_i, :include_blank => true %> <%= observe_field :software_keyword_id, :update => "list_update", :submit => "store", :url => {:action => 'list_update'} %>
ä¸è¨ã®ããã«ãã¦ããã¨ã:software_keyword_idã¨ããå ´æãç£è¦ãã¦ãå¤ãå¤åããã...
- :url=>ã§æå®ããã¡ã½ãããå¼ã³åºãã
- :submit=>ã§æå®ãããã©ã¼ã ãéä¿¡ããã
- :update=>ã§æå®ããå ´æãæ´æ°ããã
-
- ãã¹ã¦htmlã®idå±æ§ã§æå®ãããï¼ä¾ï¼ï¼div id="list_update"ï¼<ï¼/span>
- <%= select :software, :keyword_id, ...%>ã®ããã«æå®ããã¨ãidå±æ§ã¯software_keyword_idã«ãªãã
-
å®éã®ã³ã¼ã
app/views/models/softwares/_list_form.rhtml
- ãã¼ã¯ã¼ãã®ãã«ãã¦ã³ãªã¹ãã®ä½ç½®ãããã¼ã¯ã¼ãåã¨åããããããããã¼ãã«è¡¨ç¤ºã«ããã
- ãã«ãã¦ã³ãªã¹ããå¤ãä¿æããã®ã§ããã¼ã¯ã¼ãã®hidden_fieldã¯åé¤ããã
- æ¡ä»¶ããªã»ãããããªã³ã¯ã¯ããã¼ã¸ã®ä¸ã«ãæ°è¦ä½æ | ãªã»ãããã®ããã«è¡¨ç¤ºããããã«ããã
<%# ----ãã¡ã¤ã«å app/views/models/softwares/_list_form.rhtml------ %> <div id="store"> <%= form_remote_tag :update => "list_update", :submit => "store", :url => {:action => 'list_update'} %> <table width=100%><tr> <td width=70%> <span style="font-weight:bold;"> <%= human_attribute_name(Software, "title") %>/<%= human_attribute_name(Software, "description") %> </span>ãæ¤ç´¢ : <%= text_field_tag("store_search_word", @search_word) %> <%= submit_to_remote "store", "æ¤ç´¢", :update => "list_update", :submit => "store", :url => {:action => 'list_update'} %> <small> <%= link_to_remote "ã¯ãªã¢", :update => "list_update", :submit => "store", :url => {:action => 'list_update', :store_search_word => ""} %> </small> </td> <td width=16%> <%= select :software, :keyword_id, Keyword.find_all.collect {|p| [ p.name, p.id ] }, :selected => @keyword_id.to_i, :include_blank => true %> <%= observe_field :software_keyword_id, :update => "list_update", :submit => "store", :url => {:action => 'list_update'} %> <%= hidden_field_tag("store_order_field", @order_field) %> <%= hidden_field_tag("store_order_direction", @order_direction) %> <%#= hidden_field_tag("store_keyword", @keyword_id) %><%# <---åé¤ãselectã«ç½®ãæãã %> </td> <td> </td> </tr></table> <%= end_form_tag %> </div>
app/controllers/softwares_controller.rb
- ãã«ãã¦ã³ãªã¹ãããã®ãã©ã¡ã¼ã¿ã¼ãåãåãããã«ãparams[:software] && params[:software][:keyword_id]ã®ããã«åå¾ãããparams[:software][:keyword_id]ã ãã§ã¯ãlistã¢ã¯ã·ã§ã³ã§ãnil[:keyword_id]ã®ããã·ã¥å¤ãæ±ãã¦ã¨ã©ã¼ã«ãªã£ã¦ãã¾ã£ãã
- paramsã§åå¾ããå¤ã¯ãæååã«ãªã£ã¦ãã¾ãããã ã@keyword_idã¯selectã¡ã½ããã§å©ç¨ããã¨ããselected=>@keyword_id.to_iã®ããã«å¤æããå¿ è¦ãããã
# ----ãã¡ã¤ã«å:app/controllers/softwares_controller.rb------ ...ï¼éä¸çç¥ï¼... def set_pagination #ãã©ã¡ã¼ã¿ã¼ãåå¾ ###以ä¸å¤æ´ #@keyword_id = params[:keyword] || params[:store_keyword] || "%" @keyword_id = params[:keyword] || (params[:software] && params[:software][:keyword_id]) || "%" @order_field = params[:order_field] || params[:store_order_field] || "id" @order_direction = params[:order_direction] || params[:store_order_direction] || "ASC" @search_word = params[:store_search_word] #æ¡ä»¶ã«ãã£ã¦ããã©ã¡ã¼ã¿ã¼ãå¤æ´ ###以ä¸å¤æ´ #@keyword_id = "%" if params[:keyword] == params[:store_keyword] @keyword_id = "%" if params[:keyword] == (params[:software] && params[:software][:keyword_id]) ###1è¡è¿½å @keyword_id = "%" if @keyword_id.to_i < 1 @order_direction = 'ASC' if params[:order_field] && !(params[:order_field] == params[:store_order_field]) @direction_mark = @order_direction == 'DESC' ? 'â¼' : 'â²' @order_next_direction = @order_direction == 'DESC' ? 'ASC' : 'DESC' #paginateå¦ç conditions = ["keyword_id LIKE ? AND (title LIKE ? OR description LIKE ?)", @keyword_id ,"%#{@search_word}%" ,"%#{@search_word}%" ] order = "#{@order_field} #{@order_direction}" @software_pages, @softwares = paginate :softwares, :per_page => 2, :conditions => conditions, :order => order end def list_update Software.with_scope(login_user) do @ajax_update = true set_pagination render :action => "list", :layout => false end end ...ï¼éä¸çç¥ï¼...
ããã§ããã«ãã¦ã³ãªã¹ãã§ãã¼ã¯ã¼ããé¸æããã ãã§ãçµãè¾¼ãã§ããããä»ã¾ã§ã®ãã¼ã¯ã¼ãã®ãªã³ã¯ãã¯ãªãã¯ããæä½ããã®ã¾ã¾ä½¿ãããç¾å¨ã®è¦ãç®ã¯ãããªæã...ã