show child checkboxes under parent checkboxes in view page rails - ruby-on-rails

I am showing check boxes in view page and data will come from the database. Here is my code and the problem is that while looping if the same parent name came it shows same parent checkboxes two times.
<% #permission.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<br/>
<% #permission.each do |f| %>
<% if #controller_code == f.controller_code %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
<br/>
<% end %>
<% end %>
<% end %>
I have also tried this way but its not working properly..
<% #permission.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<br/>
<% end %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
<br/>
<% end %>

I dont know that i m giving exact answer for your question, i think this can help you,
<% #permission.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<% else %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %>
<%= f.action_name %>
<% end %>
<% end %>

I found the solution of my question here it is in controller do following
#permission=Permission.all.where(active: 1)
if #permission.blank? == false
#permissions = Permission.all.where(active: 1).order_by(:controller_code => "asc")
else
#permissions = ""
end
And in view page do following
<% #permissions.each do |f| %>
<% if #controller_code != f.controller_code %>
<% #controller_code = f.controller_code %>
<%= check_box_tag "cntrl_#{f.controller_code}", f.controller_code, false, :class => "Par_#{f.controller_code}", :id => "Par_#{f.controller_code}" %> <%= f.controller_name %>
<br/>
<% #permissions.each do |f| %>
<% if #controller_code == f.controller_code %>
<%= check_box_tag "action_#{f.controller_code}_#{f.action_code}",f.action_code, false, :class => "Child_#{f.controller_code}", :id => "Child_#{f.controller_code}_#{f.action_code}" %> <%= f.action_name %>
<br/>
<% end %>
<% end %>
<% end %>
<% end %>

Related

Rails search form with multiple params

I am working on a search form which should accept multiple params. But it currently accepts only one.
My Controller action:
def index
#halls = Hall.order(:name).page(params[:page]).per(9)
#city = City.all
#venue_type = VenueType.all
#event_type = EventType.all
if !params[:city].blank?
session[:city] = params[:city]
city = City.find_by(name: params[:city])
#halls = #halls.where(:city => city)
end
if !params[:venue_types].blank?
session[:venue_types] = params[:venue_types]
venue_types = VenueType.find_by(name: params[:venue_types])
#halls = #halls.where(:venue_types => venue_types)
end
if !params[:event_types].blank?
session[:event_types] = params[:event_types]
event_types = VenueType.find_by(name: params[:event_types])
#halls = #halls.where(:event_types => event_types)
end
end
My view:
<div class="search">
<%= form_tag(halls_path, :method => "get", class: "navbar-form", id: "search-form") do %>
<%= select_tag "city", options_from_collection_for_select(City.all, "name", "name") %>
<ul>
<% #venue_type.each do |venue| %>
<li>
<%= check_box_tag 'venue_type[]', venue.name -%>
<%= venue.name -%>
</li>
<% end %>
</ul>
<ul>
<% #event_type.each do |event| %>
<li>
<%= check_box_tag 'event_type[]', event.name -%>
<%= event.name -%>
</li>
<% end %>
</ul>
<button class="btn" type="submit">Search</button>
<% end %>
</div>
I bet the mistake is in the controller but as a newbie I can't find it.
Would be grateful for the answers.
Your view defined the check_box_tag with:
<%= check_box_tag 'venue_type[]', venue.name -%>
<%= venue.name -%>
<%= check_box_tag 'event_type[]', event.name -%>
<%= event.name -%>
As you see, your controller must get :value_type & :event_type from params instead of :value_types & :event_types. So you can change from either view or controller, it should work, but I recommend change from view like this:
<%= check_box_tag 'venue_types[]', venue.name -%>
<%= venue.name -%>
<%= check_box_tag 'event_types[]', event.name -%>
<%= event.name -%>

SyntaxError at /setups/new: syntax error, unexpected tIDENTIFIER, expecting keyword_end

my controller:
class SetupsController < ApplicationController
def new
#setup = Setup.new
#todays_rate = #setup.todays_rates.build
end
def create
#setup = Setup.new(params[:setup])
if #setup.save
redirect_to setup_path(#setup)
else
render 'new'
end
end
end
My view code: setup/new.html.erb
<%= form_for #setup, :html => {:multipart => true} do |f| %>
<% if #setup.errors.any? %>
<ul>
<% #setup.errors.full_messages.each do |error| %>
<li><strong><%= error %></strong></li>
<% end %>
</ul>
<% end %>
<h4><%= f.label :effective_from, class:'required' %>
<%= f.text_field :effective_from %></h4>
<h4><%= f.label :effective_to, class:'required' %>
<%= f.text_field :effective_to %></h4>
<%= f.fields_for(:todays_rate) do |i| %> ##**combining two model with one view**
<h1>Interest Rate</h1>
<table cellpadding = "5px" cellspacing = "5px" width = "100%" class="table condensed-table"><tr>
<h4><th>Days From</th>
<th>Days To</th>
<th>Rate</th>
<th>Senior increment</th>
<th>Super Senior increment</th>
<th>Widow increment</th></h4>
</tr>
<h4><td><%= i.text_field :days_from, :class => 'input-mini' %></td></h4>
<h4> <td><%= i.text_field :days_to, :class => 'input-mini' %></td></h4>
<h4><td><%= i.text_field :rate, :class => 'input-mini' %></td></h4>
<h4> <td><%= i.text_field :senior_increment, :class => 'input-mini' %></td></h4>
<h4> <td><%= i.text_field :super_senior_increment,class:"input-mini" %></td></h4>
<h4><td><%= i.text_field :widow_incrtement,class: "input-mini" %></td></h4>
</table>
<% end %>
<fieldset class="form-actions"> <%= f.submit "Create Customer", class: "btn btn-primary" %></field>
setup.rb mmodel:
class Setup < ActiveRecord::Base
has_many :todays_rates
accepts_nested_attributes_for :todays_rates
attr_accessible :effective_from, :effective_to, :todays_rate
end
i'm combining two model in one view but i'm getting the above error. i don't know where i missed the keyword_end.can any one help me
I think your problem is that you haven't closed the form, ie you need a <% end %> at the end of your template.
The error sort of tells you that, though the tIDENTIFIER stuff can throw one off the scent a bit.

Rails Nested Forms Not Saving or Validating On Update

I have the following controller:
class Admin::ArticleChangeRequestsController < Admin::ApplicationController
before_action :set_order, only: :index
before_action :set_article_change_request, only: [:edit, :delete]
helper SortClassHelper
protected
def set_order
if params[:column].present?
#column = params[:column]
else
#column = 'created_at'
end
if params[:sort].present?
#sort = params[:sort]
else
#sort = 'desc'
end
#order = #column.to_s + ' ' + #sort.to_s
end
def set_article_change_request
#article_change_request = ArticleChangeRequest.find(params[:id])
end
def article_change_request_params
params[:article_change_request].permit(:title, :change, :article)
end
public
def index
#article_change_requests = ArticleChangeRequest.order(#order).page params[:page]
end
def edit
if request.patch?
if #article_change_request.update(article_change_request_params)
redirect_to :admin_article_change_requests, :flash => { success: t(:article_change_request_updated) }
end
end
end
def delete
if request.post?
if #article_change_request.delete
flash[:success] = t(:article_change_request_deleted)
render :json => { :success => true }
else
render :json => { :success => false }
end
end
end
end
and have added:
accepts_nested_attributes_for :article
in my ArticleChangeRequest model. However whenever I hit Update it does not validate or save the article. Here is my Form:
<%= form_for #article_change_request, { :url => :admin_article_change_request_edit } do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<div>
<%= f.submit t(:update), class: 'submit' %>
</div>
<% end %>
<% if #article_change_request.errors.any? %>
<div class="message errormsg">
<p>
<%= t :there_is %> <%= pluralize #article_change_request.errors.count, t(:error) %>
</p>
</div>
<% end %>
<div class="container">
<%= f.label :title, t(:change_request_title) + ':' %>
<%= f.text_field :title, size: 40, class: 'text' %>
<% unless #article_change_request.errors[:title].blank? then %>
<span class="note error">
<% #article_change_request.errors[:title].each do |e| %>
<% if e == #article_change_request.errors[:title].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= f.label :change, t(:change_request_change) + ':' %>
<%= f.text_area :change, size: '100x5' %>
<% unless #article_change_request.errors[:change].blank? then %>
<span class="note error">
<% #article_change_request.errors[:change].each do |e| %>
<% if e == #article_change_request.errors[:change].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<hr>
<%= f.fields_for :article do |a| %>
<div class="container">
<%= a.label :category_id, t(:article_category) + ':' %>
<%= a.select :category_id, [''].concat(Category.select_options) %>
<% unless #article_change_request.article.errors[:category_id].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:category_id].each do |e| %>
<% if e == #article_change_request.article.errors[:category_id].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :title, t(:article_title) + ':' %>
<%= a.text_field :title, size: 40, class: 'text' %>
<% unless #article_change_request.article.errors[:title].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:title].each do |e| %>
<% if e == #article_change_request.article.errors[:title].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :slug, t(:article_slug) + ':' %>
<%= a.text_field :slug, size: 40, class: 'text slug' %>
<% unless #article_change_request.article.errors[:slug].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:slug].each do |e| %>
<% if e == #article_change_request.article.errors[:slug].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :article_type, t(:article_type) + ':' %>
<%= a.select :article_type, [''].concat(ArticleType.select_options) %>
<% unless #article_change_request.article.errors[:article_type].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:article_type].each do |e| %>
<% if e == #article_change_request.article.errors[:article_type].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div id="article_content_container" class="container" style="display: none;">
<%= a.label :content, t(:article_content) + ':' %>
<%= a.text_area :content, size: '100x40', class: 'wysiwyg' %>
<% unless #article_change_request.article.errors[:content].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:content].each do |e| %>
<% if e == #article_change_request.article.errors[:content].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div id="article_link_container" class="container" style="display: none;">
<%= a.label :link, t(:article_link) + ':' %>
<%= a.text_field :link, size: 40, class: 'text' %>
<% unless #article_change_request.article.errors[:link].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:link].each do |e| %>
<% if e == #article_change_request.article.errors[:link].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<div class="container">
<%= a.label :summary, t(:article_summary) + ':' %>
<%= a.text_area :summary, size: '100x5' %>
<% unless #article_change_request.article.errors[:summary].blank? then %>
<span class="note error">
<% #article_change_request.article.errors[:summary].each do |e| %>
<% if e == #article_change_request.article.errors[:summary].last %>
<%= e %>
<% else %>
<%= e %>,
<% end %>
<% end %>
</span>
<% end %>
</div>
<% end %>
Can anyone tell me why its not validating or updating?
params[:article_change_request].permit(:title, :change,
:article_attributes => {:category_id, :title, :slug, :article_type})

Showing edit link beside a comment on a group_message page only when the comment was made by session user

I am trying to show edit link for group_message_comment on a group_message page only when it was posted by the session user.
Talking about group_messages, I managed to show edit links only when it is the session user.
<% if #isme %>
<%= link_to 'Edit', edit_group_message_path(#group_message) %>
<% end %>
On the other hand,
talking about group_messages_comments, I failed to show edit links. I have no idea about this. Could you help me?
☆show.html.erb(group_messages_controller)
<p><b>Post:</b></p>
<div class="each_message">
<%= image_tag #group_message.group.imageurl,:width => '20', :height => '25' %><%= "(" + #group_message.group.name + ")" %>
<p>
<%= 'Page:' + #group_message.page.to_s + '&' %><%= 'Line:' + #group_message.line.to_s %>
<%= #group_message.member.name %>
(<%= #group_message.created_at.strftime'%Y-%m-%d %H:%M' %>)
</p>
<div class="group_message">
<p class="message_content"><%= #group_message.content %></p>
</div><!--group_message-->
<br/>
<% if #isme %>
<%= link_to 'Edit', edit_group_message_path(#group_message) %>
<% end %>
</div><!--each_message-->
<hr/>
<b>Comments:</b>
<% if #group_message.group_message_comments.present? %>
<% #group_message.group_message_comments.each do |gmsc|%>
<div class="group_message_comment">
<p><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= gmsc.content %>
<p>
<% if #isme_comment %>
<%= link_to 'Edit', edit_group_message_comment_path(#group_message.group_message_comments) %>
<% end %>
</p>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<ul>
<li>no comment yet.</li>
</ul>
<% end %> <!--if -->
☆group_messages_controller
def show
if !checklogin? then return end
#group_message = GroupMessage.find(params[:id])
#isme = me? #group_message
#group_message_comment = GroupMessageComment.new
#group_message_comment = GroupMessage.find(params[:id]).group_message_comments.build
#isme_comment = me? #group_message_comment<!#########--maybe this is wrong-->
respond_to do |format|
format.html # show.html.erb
format.json { render json: #group_message }
end
end
☆application_controller
def me? obj = nil
id_num = obj !=nil ? obj.member_id : params[:id].to_i
if session[:user_id] == id_num then
return true
else
return false
end
end
I just added this and made it. Thank you for your attentions.
<% if #gmsc_member_id == gmsc.member.id %>
<%= link_to 'Edit', edit_group_message_comment_path(gmsc) %>
<% end %>

An Error when showing group_path(#group)

Now, I tried to set up the route with group_path(#group) but got an error message like,
ActiveRecord::RecordNotFound at /groups/%23%3CActiveRecord::Relation:0x007fd6cf362538%3E
Couldn't find Group with id=#<ActiveRecord::Relation:0x00fds6cf362538>
Here is my code. I would like to set up a link to groups/show/:id. Why did this error occur? Could you give me how to solve this?
☆index.html.erb
<% if #items.present? %>
<% #items.each do |i| %>
<% i_attr = i.get_element('ItemAttributes') %>
<tr>
<td> <%= link_to image_tag(i.get('SmallImage/URL'), {:style => 'border: none;'}), i_attr.get('DetailPageURL') %></td>
<td> <%= link_to i_attr.get('Title'), i_attr.get('DetailPageURL') %></td>
<td> <%= i_attr.get('Author') %></td>
<td> <%= i_attr.get('PublicationDate')%></td>
<td> <%= i_attr.get('Publisher') %></td>
<td> <%= i_attr.get('NumberOfPages')%></td>
<td>
<% if #existing_groups_isbns.include? i_attr.get('ISBN') %>
<% #existing_groups_isbns.each do |isbn| %>
<% if isbn == i_attr.get('ISBN') %>
<% #group = Group.where(:isbn =>isbn) %>
<%= link_to '既存ページへ' , group_path(#group) %>
<% end %>
<% end %>
<% else %><!-- if includes?==-->
<%= link_to '新規作成', {:controller => 'groups', :action => 'new', :name => i.get('ItemAttributes/Title'),:author => i.get('ItemAttributes/Author'), :publish => i.get('ItemAttributes/Publisher'), :published => i.get('ItemAttributes/PublicationDate'), :isbn => i.get('ItemAttributes/ISBN'), :page => i.get('ItemAttributes/NumberOfPages'), :imageurl=>i.get('MediumImage/URL')} ,class: "btn btn-midium btn-primary"%>
<% end %><!--if includes?-->
</td>
</tr>
<% end %><!-- #items.each do-->
<% else %><!--if #items.present?-->
見つかりませんでした。
<% end %><!-- if #items.present?-->
☆index_controller
def index
#keyword = params[:keyword]
if #keyword.present?
Amazon::Ecs.debug = true
res = Amazon::Ecs.item_search(params[:keyword],
:search_index => 'All', :response_group => 'Medium')
#items = res.items
search_isbns = #items.map{ |isbns| isbns.get('ItemAttributes/ISBN')}
search_asins = #items.map{ |asins| asins.get('ItemAttributes/ASIN')}
#existing_groups_isbns = Group.select(:isbn).where(:isbn => search_isbns).map(&:isbn)
#existing_groups_ids = Group.where(:isbn => search_isbns).map{|g| g.id}
end
Replace
<% #group = Group.where(:isbn =>isbn) %>
with
<% #group = Group.where(:isbn =>isbn).first %>
Because you need an object where you had an ActiveRecord relation

Resources