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})
Related
I'm creating a prayer website with the ability to comment on a public prayer. When I try to create a comment on a prayer, it returns four errors:
Prayer must exist
User must exist
User can't be blank
Prayer can't be blank
And then every comment creation form on the page is automatically filled in with the text I put in the first comment box, and all of them have the same 4 errors on them. I tried to use the hidden_field_tag to put in the comment form for the right user id and prayer id but they aren't put into the hash for the new comment object, they are separate.
This is the debug stuff at the bottom of the page:
#<ActionController::Parameters {"authenticity_token"=>"abcdefg", "comment"=>#<ActionController::Parameters {"content"=>"Comment comment 1 2 3"} permitted: false>, "user_id"=>"1", "prayer_id"=>"301", "commit"=>"Comment", "controller"=>"comments", "action"=>"create"} permitted: false>
controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
if logged_in?
#prayer = current_user.prayers.build
#comment = current_user.comments.build
#feed_items = current_user.feed.paginate(page: params[:page])
end
end
end
controllers/comments_controller.rb
class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#comment = Comment.create(comment_params)
if #comment.save
flash[:success] = "Comment created!"
redirect_to root_url
else
#feed_items = current_user.feed.paginate(page: params[:page])
render 'static_pages/home', status: :unprocessable_entity
end
end
def destroy
#comment.destroy
flash[:success] = "Comment deleted"
redirect_back_or_to( root_url, status: :see_other )
end
private
def comment_params
params.require(:comment).permit(:content, :prayer_id, :user_id)
end
def correct_user
#comment = current_user.comments.find_by(id: params[:id])
redirect_to root_url, status: :see_other if #comment.nil?
end
end
models/comment.rb
class Comment < ApplicationRecord
belongs_to :prayer
belongs_to :user
default_scope -> { order( created_at: :desc) }
validates :user_id, presence: true
validates :prayer_id, presence: true
validates :content, presence: true, length: { maximum: 140 }
end
views/comments/_comment.html.erb
<li id="comment-<%= comment.id %>">
<%= link_to gravatar_for(comment.user, size: 30), comment.user %>
<span class="user"><%= link_to comment.user.name, comment.user %></span>
<span class="comment-content"><%= comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago.
<% if current_user?(comment.user) %>
<%= link_to "delete comment", comment, data: { "turbo-method": :delete,
"turbo-confirm": "Are you sure?"} %>
<% end %>
</span>
</li>
views/prayers/_prayer.html.erb
<li id="prayer-<%= prayer.id %>">
<%= link_to gravatar_for(prayer.user, size: 50), prayer.user %>
<span class="user"><%= link_to prayer.user.name, prayer.user %></span>
<span class="content">
<%= prayer.content %>
<% if prayer.image.attached? %>
<%= image_tag prayer.image.variant(:display) %>
<% end %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(prayer.created_at) %> ago.
<% if current_user?(prayer.user) %>
<%= link_to "delete", prayer, data: { "turbo-method": :delete,
"turbo-confirm": "Are you sure?"} %>
<% end %>
</span>
<span>
<%= render 'shared/comment_form', prayer_id: prayer.id %>
</span>
<span>
<% if prayer.comments.any? %>
<ol class="comments">
<% prayer.comments.each do |comment| %>
<%= render comment %>
<% end %>
</ol>
<% end %>
</span>
</li>
** views/shared/_comment_form.html.erb * **
<%= form_with(model: #comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<center>
<div class="field">
<%= f.text_area(:content, placeholder: "Comment on this prayer...") %>
</div>
<div><%= hidden_field_tag :user_id, #user.id %></div>
<div><%= hidden_field_tag :prayer_id, prayer_id %></div>
<%= f.submit "Comment", class: "btn btn-primary" %>
</center>
<% end %>
views/shared/_error_messages.html.erb
<% if object != nil && object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
views/shared/_feed.html.erb
<% if #feed_items.any? %>
<ol class="prayers">
<%= render #feed_items %>
</ol>
<%= will_paginate #feed_items,
params: { controller: :static_pages, action: :home } %>
<% end %>
With help from Maxence, I discovered I need to use f.hidden_field versus hidden_field_tag
so I changed the views/shared/_comment_form.html.erb to the following:
<%= form_with(model: #comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<center>
<div class="field">
<%= f.text_area(:content, placeholder: "Comment on this prayer...") %>
</div>
<div><%= f.hidden_field :user_id, value: #user.id %></div>
<div><%= f.hidden_field :prayer_id, value: prayer_id %></div>
<%= f.submit "Comment", class: "btn btn-primary" %>
</center>
<% end %>
I have this on my view:
<div class='container'>
<div class='row upper_container'>
<div class='search_container'>
<%= form_tag deals_path, :method => :get, :class => 'navbar-form navbar-left' do %>
<div class='form-group'>
<%= text_field_tag :search, params[:search], class: 'form-control' %>
</div>
<%= submit_tag 'Search', :name => nil %>
<% end %>
</div>
</div>
<% #deals.each_with_index do |d, i| %>
<% if i % 3 == 0 %>
<div class='row middle_container'>
<% end %>
<div class='col-md-4'>
<div class='deal_container'>
<%= d.title %>
<img src='<%= d.photo %>', class='deal_img'>
</div>
</div>
<% if (i % 3 == 2) || (i == (#deals.length - 1)) %>
</div>
<% end %>
<% end %>
</div>
this in my controller:
class DealsController < ApplicationController
def index
# #deals = Deal.paginate(:page => params[:page])
#search = Deal.search do
fulltext params[:search]
end
#deals = #search.result
end
private
def deal_params
params.require(:deal).permit(:title)
end
end
and this in my model:
class Deal < ActiveRecord::Base
searchable do
text :title
end
end
when I want to do a seach by some word, like 'Treatment', the #deals variable, in the controller is null, but the param is being sent: Parameters: {"utf8"=>"✓", "search"=>"Treatment"}
any idea?
Try this:
query = params[:search]
#search = Deal.search do
fulltext query
end
#deals = #search.result
Please check this answer for details.
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 %>
1.I would like to set up the form of GroupMessage model on the view page, group/show/:id.
2.I tried the code below.
(show.html.erb-group)
<%= form_for([#group, #group_messages]) do |f| %>
<% if #group_message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#group_message.errors.count, "error") %> prohibited this group_message from being saved:</h2>
<ul>
<% #group_message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :member_id %><br />
<%= f.number_field :member_id %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
(groups_controller)
def show
#group = Group.find(params[:id])
#group_message = #group.group_messages.build
if !checklogin? then return end
groupfriends = GroupInMember.where(:member_id => session[:login].id)
gflist = '('
groupfriends.each do |gf|
gflist += gf.member_id.to_s + ","
end
gflist += session[:login].id.to_s + ')'
#query = "select * from group_messages where member_id in " + gflist
#group_messages = GroupMessage.find_by_sql(#query)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #group_messages }
end
end
3.And then I got an error.
undefined method `group_messages' for #
4.How can I solve this??
My guess is that #group = Group.find(params[:id]) is not returning a Group.
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 %>