rails 4 application view helper possible refactor [closed] - ruby-on-rails

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
In three different layouts I have to display social links utilizing three different configurations. I wanted to setup a few methods to handle this, but I
feel like this code could be optimized further. The section attached can be called by social_links(["show_div","show_email"]) for example which will
trigger various configurations from the methods. Is this overkill? The other thought I had was to break these down even further and only include the
link instead of the html code.
Tips to refactor? Optimize?
In my application_helper.rb file:
# shared social links displayed in multiple layouts
def social_links(view_options = nil)
html=%(<ul class="social-links">)
html+=%(<li>) + blog_link(view_options) + %(</li>)
html+=%(<li>) + podcast_link(view_options) + %(</li>)
html+=%(<li>) + twitter_link(view_options) + %(</li>)
html+=%(<li>) + instagram_link(view_options) + %(</li>)
html+=%(<li>) + facebook_link(view_options) + %(</li>)
html+=%(<li>) + pinterest_link(view_options) + %(</li>)
if view_options && view_options.include?("show_email")
html+=%(<li>) + contact_us_link(view_options) + %(</li>)
end
html+=%(</ul>)
raw(html.strip)
end
def twitter_link(view_options = nil)
html=%(<a href="#{ENV['SOCIAL_TWITTER']}" target="_blank">)
if view_options && view_options.include?("show_div")
html+=%(<div>)
end
html+=%(<i class="fa fa-twitter-square"></i>)
if view_options && view_options.include?("show_desc")
html+=%(Twitter)
end
if view_options && view_options.include?("show_div")
html+=%(</div>)
end
html+=%(</a>)
raw(html.strip)
end
def instagram_link(view_options = nil)
html=%(
<a href="#{ENV['SOCIAL_INSTAGRAM']}" target="_blank">)
if view_options && view_options.include?("show_div")
html+=%(<div>)
end
html+=%(<i class="fa fa-instagram"></i>)
if view_options && view_options.include?("show_desc")
html+=%(Instagram)
end
if view_options && view_options.include?("show_div")
html+=%(</div>)
end
html+=%(</a>)
raw(html.strip)
end
def facebook_link(view_options = nil)
html=%(
<a href="#{ENV['SOCIAL_FACEBOOK']}" target="_blank">)
if view_options && view_options.include?("show_div")
html+=%(<div>)
end
html+=%(<i class="fa fa-facebook-square"></i>)
if view_options && view_options.include?("show_desc")
html+=%(Facebook)
end
if view_options && view_options.include?("show_div")
html+=%(</div>)
end
html+=%(</a>)
raw(html.strip)
end
def pinterest_link(view_options = nil)
html=%(
<a href="#{ENV['SOCIAL_PINTEREST']}" target="_blank">)
if view_options && view_options.include?("show_div")
html+=%(<div>)
end
html+=%(<i class="fa fa-pinterest-square"></i>)
if view_options && view_options.include?("show_desc")
html+=%(Pinterest)
end
if view_options && view_options.include?("show_div")
html+=%(</div>)
end
html+=%(</a>)
raw(html.strip)
end
def blog_link(view_options = nil)
html=%(
<a href="#{ENV['SOCIAL_BLOG']}" target="_blank">)
if view_options && view_options.include?("show_div")
html+=%(<div>)
end
html+=%(<i class="fa fa-rss-square"></i>)
if view_options && view_options.include?("show_desc")
html+=%(Blog)
end
if view_options && view_options.include?("show_div")
html+=%(</div>)
end
html+=%(</a>)
raw(html.strip)
end
def podcast_link(view_options = nil)
html=%(
<a href="#{ENV['SOCIAL_PODCAST']}" target="_blank">)
if view_options && view_options.include?("show_div")
html+=%(<div>)
end
html+=%(<i class="fa fa-caret-square-o-right"></i>)
if view_options && view_options.include?("show_desc")
html+=%(Podcast)
end
if view_options && view_options.include?("show_div")
html+=%(</div>)
end
html+=%(</a>)
raw(html.strip)
end
def contact_us_link(view_options = nil)
html=%(
<a href="/contact_us" target="_blank">)
if view_options && view_options.include?("show_div")
html+=%(<div>)
end
html+=%(<i class="fa fa-envelope"></i>)
if view_options && view_options.include?("show_desc")
html+=%(Contact)
end
if view_options && view_options.include?("show_div")
html+=%(</div>)
end
html+=%(</a>)
raw(html.strip)
end
UPDATE ONE: Partial Method
After further conversation and thought, I changed the helpers to a partial _social_links.html.slim
- social_twitter = "<i class='fa fa-twitter-square'></i>".html_safe
- social_instagram = "<i class='fa fa-instagram'></i>".html_safe
- social_facebook = "<i class='fa fa-facebook-square'></i>".html_safe
- social_pinterest = "<i class='fa fa-pinterest-square'></i>".html_safe
- social_rss = "<i class='fa fa-rss-square'></i>".html_safe
- social_podcast = "<i class='fa fa-caret-square-o-right'></i>".html_safe
- social_email = "<i class='fa fa-envelope'></i>".html_safe
ul[class="social-links"]
li
a[href="#{ENV['SOCIAL_TWITTER']}" target="_blank"]
- if collection && collection.include?("show_div")
div
= social_twitter
- else
= social_twitter
- if collection && collection.include?("show_desc")
| Twitter
li
a[href="#{ENV['SOCIAL_INSTAGRAM']}" target="_blank"]
- if collection && collection.include?("show_div")
div
= social_instagram
- else
= social_instagram
- if collection && collection.include?("show_desc")
| Instagram
li
a[href="#{ENV['SOCIAL_FACEBOOK']}" target="_blank"]
- if collection && collection.include?("show_div")
div
= social_facebook
- else
= social_facebook
- if collection && collection.include?("show_desc")
| Facebook
li
a[href="#{ENV['SOCIAL_PINTEREST']}" target="_blank"]
- if collection && collection.include?("show_div")
div
= social_pinterest
- else
= social_pinterest
- if collection && collection.include?("show_desc")
| Pinterest
li
a[href="#{ENV['SOCIAL_BLOG']}" target="_blank"]
- if collection && collection.include?("show_div")
div
= social_rss
- else
= social_rss
- if collection && collection.include?("show_desc")
| Blog
li
a[href="#{ENV['SOCIAL_PODCAST']}" target="_blank"]
- if collection && collection.include?("show_div")
div
= social_podcast
- else
= social_podcast
- if collection && collection.include?("show_desc")
| Podcast
- if collection && collection.include?("show_email")
li
a[href="/contact_us" target="_blank"]
- if collection && collection.include?("show_div")
div
= social_email
- else
= social_email
- if collection && collection.include?("show_desc")
| Contact
The partial can be invoked by:
render partial: "layouts/social_links", locals: { collection: ["show_div","show_email"]} or
render partial: "layouts/social_links", locals: { collection: nil}
UPDATE TWO: OO Method suggest below
This can be called like:
social_links()
social_links(show_desc: true, show_list: %w{contact})
social_links(show_div: true, show_list: %w{contact})
In the folder /helpers/social_link I have my general helper:
module SocialLink
module SocialLinkHelper
# ****************************************************************************************************
# ****************************************************************************************************
DEFAULT_SOCIAL_PROVIDERS = %w{twitter facebook instagram pinterest blog podcast}
def social_links(options={})
content_tag :ul, class: "social-links" do
provider_link(options)
end
end
def provider_link(options={})
social_providers = DEFAULT_SOCIAL_PROVIDERS
social_providers = social_providers + options[:show_list] if options[:show_list]
social_providers.each do |provider|
klass = "SocialLink::SocialLinkClass::#{provider.titleize}Link".constantize
link = klass.new(options)
concat link.generate
end
end
# ****************************************************************************************************
# ****************************************************************************************************
end
end
In the folder /helpers/social_link I have my control class:
module SocialLink
module SocialLinkClass
class SocialBase < ActionView::Base
include ActionView::Helpers::TagHelper
def initialize(options)
#show_div = options[:show_div] || nil
#show_desc = options[:show_desc] || nil
end
def generate
content_tag :li do
content = content_tag(:i, nil, class: self.class.icon)
content = content + self.class.description.titleize if #show_desc
content = "<div>#{content}</div>" if #show_div
link_to(content.html_safe, self.class.link)
end
end
end
# ****************************************************************************************************
class ContactLink < SocialBase
def self.link
"/contact_us"
end
def self.description
"contact"
end
def self.icon
"fa fa-envelope"
end
end
# ****************************************************************************************************
class BlogLink < SocialBase
def self.link
ENV['SOCIAL_BLOG']
end
def self.description
"blog"
end
def self.icon
"fa fa-rss-square"
end
end
# ****************************************************************************************************
class PodcastLink < SocialBase
def self.link
ENV['SOCIAL_PODCAST']
end
def self.description
"podcast"
end
def self.icon
"fa fa-caret-square-o-right"
end
end
# ****************************************************************************************************
class PinterestLink < SocialBase
def self.link
ENV['SOCIAL_PINTEREST']
end
def self.description
"pinterest"
end
def self.icon
"fa fa-pinterest-square"
end
end
# ****************************************************************************************************
class InstagramLink < SocialBase
def self.link
ENV['SOCIAL_INSTAGRAM']
end
def self.description
"instagram"
end
def self.icon
"fa fa-instagram"
end
end
# ****************************************************************************************************
class TwitterLink < SocialBase
def self.link
ENV['SOCIAL_TWITTER']
end
def self.description
"twitter"
end
def self.icon
"fa fa-twitter-square"
end
end
# ****************************************************************************************************
class FacebookLink < SocialBase
def self.link
ENV['SOCIAL_FACEBOOK']
end
def self.description
"facebook"
end
def self.icon
"fa fa-facebook-square"
end
end
# ****************************************************************************************************
end
end
UPDATE THREE: My buddy #drewtempelmeyer approach
File: _social_link.html.erb
<%= social_icon service, collection %>
<% if show_desc?(collection) %>
| <%= service.titleize %>
<% end %>
File: profile.html.erb
<%= render_social_link 'facebook', %w(show_div show_desc) %>
File: social_helper.rb
module SocialHelper
def render_social_link(service = 'facebook', collection = nil)
render partial: 'shared/social_link', locals: {
service: service, collection: collection
}
end
def show_div?(collection)
collection && collection.include?('show_div')
end
def show_desc?(collection)
collection && collection.include?('show_desc')
end
def social_icon(service, collection)
content = content_tag(:li, nil, class: "fa fa-#{service.downcase}")
content = "<div>#{content}</div>" if show_div?(collection)
content
end
end

This would be a good use case of applying OOP on view logic. Partials can also do the job but with too much options it will looks messy.
Starting with a general helper social_links is nothing wrong. Helper is just to help the view.
But later, you need classes to do the detailed job.
Your view which calls the helper
<%= social_links show_div: 'foo' %>
The general helper
SOCIAL_PROVIDERS = ['Twitter', 'Facebook', 'GooglePlus']
def social_links(options)
content_tag :ul do
provider_link(options)
end
end
def provider_links(options)
SOCIAL_PROVIDERS.each do |provider|
klass = "#{provider}Link".constantize
link = klass.new(options)
concat link.generate
end
end
Then the classes
class SocialLink
def self.link
"#"
end
def initialize(options)
#show_div = options.try(:show_div)
#show_desc = options.try(:show_div)
end
def generate
content_tag :li do
concat main_link
concat show_div_link if #show_div
concat show_desc_link if #show_desc
end
end
private
def main_link
# Your html
# call self.class.link here
end
def show_div_link
# You html
end
def show_desc_link
# You html
end
end
class TwitterLink < SocialLink
def self.link
ENV['SOCIAL_TWITTER']
end
end
These are demo code but you got the idea

Related

custom simple_form input date_flag

Trying to use f.input(:activated_at, as: :date_flag) to render as a checkbox, which sends down Time.now for true and nil for false.
https://github.com/postageapp/date_flag/blob/master/lib/date_flag.rb
https://raw.githubusercontent.com/heartcombo/simple_form/main/lib/simple_form/inputs/boolean_input.rb
# frozen_string_literal: true
class DateFlagInput < SimpleForm::Inputs::Base
def input(wrapper_options = {})
template.content_tag(:div, class: "form-check form-switch") do
template.concat(#builder.check_box(attribute_name, {class: 'form-check-input'}, Time.now, nil))
template.concat(template.label(attribute_name, label_text, {class: "form-check-label"}))
end
end
end
This code above gets me a bootstrap5 checkbox, which sends down the time, but cannot render a "checked".
I figured this out!
# frozen_string_literal: true
class DateFlagInput < SimpleForm::Inputs::BooleanInput
def input(wrapper_options = {})
flag_value = object.send(attribute_name)
# date_flag magic
input_html_options[:checked] = flag_value ? (flag_value <= Time.now) : false
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
merged_input_options[:class] = [input_html_classes]
template.content_tag(:div, class: container_class_name) do
build_hidden_field_for_checkbox +
build_check_box_without_hidden_field(merged_input_options) +
#builder.label(label_target, label_text, {class: boolean_label_class})
end
end
def label_translation
if SimpleForm.translate_labels && (translated_label = translate_from_namespace(:labels))
translated_label
elsif object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(reflection_or_attribute_name.to_s.gsub(/_at$/, ''))
else
attribute_name.to_s.gsub(/_at$/, '').humanize.titleize
end
end
def container_class_name
"form-check"
end
def label(wrapper_options)
template.label_tag(nil, ' '.html_safe)
end
def input_html_classes
'form-check-input'
end
def boolean_label_class
"form-check-label"
end
def checked_value
Time.now
end
def unchecked_value
0
end
end
adding the checked: option to show how the checkbox should render.
the unchecked_value cannot be nil because nil does gets excluded from the params. using nil or 0 both worked for the unchecked_value!

Sorting multiple models in one controller

I am using Ruby on Rails 5.2.3 and Mongoid 7.0
I need to be able to sort multiple models (Item and Text) in one cotroller.
Now only Item or Text is sorted, it is necessary that Position was set in relation to each other.
class UsersController < ApplicationController
def sort
params[:item].each_with_index do |id, index|
Item.where(id: id).update_all(position: index + 1)
end
head :ok
end
def admin
#user_items = #user.user_feed
end
end
admin.html.erb
<div id="items" data-url="<%= sort_users_path %>">
<%= render partial: 'users/user_item', collection: #user_items %>
</div>
_user_item.html.erb
<% if user_item[:title].present? %>
<div id="item_<%= user_item[:id] %>">
<%= user_item[:position] %>
</div>
<% end %>
<% if user_item[:text].present? %>
<div id="item_<%= user_item[:id] %>">
<%= raw user_item[:position] %>
</div>
<% end %>
User.rb
def user_activity
activity_items = []
items.each do |item|
activity_item = {}
activity_item[:id] = item.id
activity_item[:url] = item
activity_item[:title] = item.title
activity_item[:position] = item.position
activity_item[:item_link] = item.url
activity_items << activity_item
end
texts.each do |text|
activity_item = {}
activity_item[:id] = text.id
activity_item[:url] = text
activity_item[:text] = text.text
activity_item[:position] = text.position
activity_items << activity_item
end
activity_items.sort_by! { |activity_item| activity_item[:position] }
activity_items
end
def user_feed
activity_items = user_activity
activity_items.sort_by! { |activity_item| activity_item[:position] }
activity_items
end
Just needed to add Text.where(id: id).update_all(position: index + 1)
def sort
params[:item].each_with_index do |id, index|
Item.where(id: id).update_all(position: index + 1)
Text.where(id: id).update_all(position: index + 1)
end
head :ok
end

custom will_paginate renderer

Documentation is lacking for will_paginate custom renderers:
There is no documentation how to write your own link renderer, but the source code is pretty self-explanatory. Dive into it, and selectively override methods of the LinkRenderer to adjust them to your needs.
Is there any unofficial documentation?
Found a decent blog post about custom will_paginate renderer
module ApplicationHelper
# change the default link renderer for will_paginate
def will_paginate(collection_or_options = nil, options = {})
if collection_or_options.is_a? Hash
options, collection_or_options = collection_or_options, nil
end
unless options[:renderer]
options = options.merge :renderer => MyCustomLinkRenderer
end
super *[collection_or_options, options].compact
end
end
and then in an initializer
class MyCustomLinkRenderer < WillPaginate::ActionView::LinkRenderer do
def container_attributes
{class: "tc cf mv2"}
end
def page_number(page)
if page == current_page
tag(:span, page, class: 'b bg-dark-blue near-white ba b--near-black pa2')
else
link(page, page, class: 'link ba b--near-black near-black pa2', rel: rel_value(page))
end
end
def gap
text = #template.will_paginate_translate(:page_gap) { '…' }
%(<span class="mr2">#{text}</span>)
end
def previous_page
num = #collection.current_page > 1 && #collection.current_page - 1
previous_or_next_page(num, #options[:previous_label], 'link ba near-black b--near-black pa2')
end
def next_page
num = #collection.current_page < total_pages && #collection.current_page + 1
previous_or_next_page(num, #options[:next_label], 'link ba near-black b--near-black pa2')
end
def previous_or_next_page(page, text, classname)
if page
link(text, page, :class => classname)
else
tag(:span, text, :class => classname + ' bg-dark-blue near-white')
end
end
end
Thanks to previous answer, i wrote this code to use will_paginate with materialize
application_controller.rb
def custom_paginate_renderer
# Return nice pagination for materialize
Class.new(WillPaginate::ActionView::LinkRenderer) do
def container_attributes
{class: "pagination"}
end
def page_number(page)
if page == current_page
"<li class=\"cyan active\">"+link(page, page, rel: rel_value(page))+"</li>"
else
"<li class=\"waves-effect\">"+link(page, page, rel: rel_value(page))+"</li>"
end
end
def previous_page
num = #collection.current_page > 1 && #collection.current_page - 1
previous_or_next_page(num, "<i class=\"material-icons\">chevron_left</i>")
end
def next_page
num = #collection.current_page < total_pages && #collection.current_page + 1
previous_or_next_page(num, "<i class=\"material-icons\">chevron_right</i>")
end
def previous_or_next_page(page, text)
if page
"<li class=\"waves-effect\">"+link(text, page)+"</li>"
else
"<li class=\"waves-effect\">"+text+"</li>"
end
end
end
end
your_controller.rb
# GET /articles/1
def articles
#articles = #articles.paginate(:page => params[:page], :per_page => 20).order(id: :desc)
#custom_paginate_renderer = custom_paginate_renderer
end
your_view.html.erb
<%= will_paginate #articles, renderer: #custom_paginate_renderer %>
Not the most beautiful rails code, but it works
Thanks to the answers which have guided me to write this renderer for Bootstrap 5.
//config/initializers/bootstrap_paginate_renderer.rb
class BootstrapPaginateRenderer < WillPaginate::ActionView::LinkRenderer
def container_attributes
{ class: 'pagination' }
end
def html_container(html)
child = tag(:ul, html, container_attributes)
tag(:nav, child)
end
def page_number(page)
if page == current_page
'<li class="page-item active">' + link(page, page, rel: rel_value(page),class: 'page-link') + '</li>'
else
'<li class="page-item">' + link(page, page, rel: rel_value(page),class: 'page-link') + '</li>'
end
end
def previous_page
num = #collection.current_page > 1 && #collection.current_page - 1
previous_or_next_page(num, '<span aria-hidden="true">«</span>')
end
def next_page
num = #collection.current_page < total_pages && #collection.current_page + 1
previous_or_next_page(num, '<span aria-hidden="true">»</span>')
end
def previous_or_next_page(page, text)
if page
'<li class="page-item">' + link(text, page, class: 'page-link') + '</li>'
else
'<li class="page-item disabled">' + link(text, page, class: 'page-link') + '</li>'
end
end
end
//app/helpers/application_helper.rb
def will_paginate(coll_or_options = nil, options = {})
if coll_or_options.is_a? Hash
options = coll_or_options
coll_or_options = nil
end
unless options[:renderer]
options = options.merge renderer: BootstrapPaginateRenderer
end
super *[coll_or_options, options].compact
end
For anyone looking for a TailwindUI solution, I'm working on one right now using templates as the renderer.
Gist is here.

rails4 using helper/service and where to put it

I created this class for assembling the text based on the length of the product attributes for sharing on twitter.
My questions:
Is this the good approach to tackle the problem? If not this then what? (where should I put the class and the methods, how to invoke it, etc.)
If this is the good approach then what should be changed? For instance I feel the def twitter_share_text shouldn't be in the product.rb.
show.html.erb
<a class="twitter-share" data-behavior="twitter-share"
data-twittertext="<%= #product.twitter_share_text %>"
data-twitterurl="<%= product_url(#product) %>"
data-twitteranchor>
<i class="fa fa-lg fa-twitter"></i>
</a>
product.rb
def twitter_share_text
TwitterProductShare.new(self).return_text
end
app/services/twitter_product_share.rb
class TwitterProductShare
URL_LENGTH = 23 #defined by twitter API
SPACE_LENGTH = 1
TWITTER_MAX = 140
attr_reader :name, :oneliner
def initialize(product)
#name = product.name
#oneliner = product.oneliner
end
def return_text
if full_length <= TWITTER_MAX
return basic_text
else
return basic_text[0...-(difference + text_end.length)] + text_end
end
end
private
def basic_text
"#{name}: #{oneliner}"
end
def difference
full_length - TWITTER_MAX
end
def full_length
basic_text.length + SPACE_LENGTH + URL_LENGTH
end
def text_end
"..."
end
end
I think code like that belongs into a view helper:
# in app/helpers/product_helper.rb
def twitter_share_link(product)
data = {
behavior: 'twitter-share',
twittertext: TwitterProductShare.new(product).return_text,
twitterurl: product_url(product),
twitteranchor: 'twitteranchor'
}
link_to(class: 'twitter-share', data: data) do
tag(:i, class: 'fa fa-lg fa-twitter')
end
end
In your view use this helper like this:
<%= twitter_share_link(#product) %>
Or you could even return the whole data hash from the TwitterProductShare.

Parameter missing error(rails)

I am facing an error that when ever I try to call the function finalized_request it throws me an error saying "param is missing or the value is empty: finalizedeal". Since I am new to this I can't figure out what am I doing wrong(I am new to ROR).
Request_controller.rb
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
#categories_list = Category.getAll()
end
def active
user = session[:user]
#requests = Array.new
#tag = Array.new
#requests = Request.getRequestByUser(user)
#requests.each.with_index do |request, index|
if request != nil
#tag[index] = Array.new
request[:tag_id].each do |t|
#tag[index] << Tag.getTag(t)
end
end
end
end
# GET /requests/1
# GET /requests/1.json
def show
#user = User.getUser(#request[:user_id])
#tag = Array.new
#request[:tag_id].each do |cate|
#tag << Tag.getTag(cate)
end
end
# GET /requests/1/edit
def edit
#tag = Array.new
#request[:tag_id].each do |cate|
#tag << Tag.getTag(cate)
end
end
# POST /requests
def post_request
tags_arr = params[:tags] ;
#=begin
#categories = Array.new ;
#if tags != nil
# tags.each do |tag|
# category = Category.createCategoryIfNotExist(tag)
# if(category != nil)
# categories << category[:_id]
# end
# end
#end
#=end
tags = Array.new ;
if tags_arr != nil
tags_arr.each do |t|
tag = Tag.createTagIfNotExist(t)
if(tag != nil)
tags << tag[:_id]
end
end
end
request_data = request_params
user_id = session[:user]
request_data[:tag_id] = tags
request_data[:user_id] = user_id
#request_ = Request.createRequest(request_data)
if #request_
flash[:notice] = "Request Post successfully."
redirect_to :action => "active"
end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
#tags = params[:tags] ;
#categories = Array.new ;
#if tags != nil
# tags.each do |tag|
# category = Category.createCategoryIfNotExist(tag)
# if(category != nil)
# categories << category[:_id]
# end
# end
#end
tags_arr = params[:tags] ;
tags = Array.new ;
if tags_arr != nil
tags_arr.each do |t|
tag = Tag.createTagIfNotExist(t)
if(tag != nil)
tags << tag[:_id]
end
end
end
Rails.logger.info("RequestsParams: #{request_params.inspect}")
request_data = request_params
if request_data[:is_service] != "on"
request_data[:is_service] = "off"
end
user_id = session[:user]
request_data[:tag_id] = tags
request_data[:user_id] = user_id
if Request.updateRequest(#request,request_data)
flash[:notice] = "Request has been Edited successfully."
redirect_to :action => "active"
end
end
def delete_request ()
if Request.delete_request(params[:id])
flash[:notice] = "Request has been Deleted successfully."
render :json => "great"
end
end
# GET /requests
def finalize_request()
finalizedrequest = finalizedRequest_params
request = Request.getRequest(finalizedrequest[:title])
finalizedrequest[:title] = request[:title]
Request.delete_request(request[:_id])
FinalizedDeal.createFinalizedRequest(finalizedrequest)
redirect_to :action => "bookmark"
end
# GET /requests
def bookmark
user = session[:user]
#requests = Array.new
#tag = Array.new
#requests = Request.getRequestByUser(user)
#requests.each.with_index do |request, index|
if request != nil
#tag[index] = Array.new
request[:tag_id].each do |t|
#tag[index] << Tag.getTag(t)
end
end
end
end
# GET /requests
def bookmark_request
data = params[:d]
bookmarked_against_Request = Request.getRequest(1)
request_bookmarked = Request.getRequest(data)
request_bookmarked_2 = request_bookmarked
bookmarked_against_Request_2 = bookmarked_against_Request
Rails.logger.info("Bookmark 2: #{bookmarked_against_Request_2.inspect}")
#bookmarked_against_Request_2[:favourites] << request_bookmarked[:id]
#request_bookmarked_2[:favourites_of] << bookmarked_against_Request[:id]
#hello
#Request.updateRequest(request_bookmarked , request_bookmarked_2)
#Request.updateRequest(bookmarked_against_Request , bookmarked_against_Request_2)
redirect_to :action => "bookmark"
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
#request = Request.getRequest(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:title, :description, :type , :is_service , :required_user_location , :required_product_location ,:Upper_price_range , :lower_price_range , :negotiable , :type , :tags , :category_id)
end
def finalizedRequest_params
params.require(:finalizedeal).permit(:title , :description)
end
end
finalized_deal.rb
class FinalizeDeal
include Mongoid::Document
field :deal_details, type: String
field :categories, type: Array
field :owner_user, type: MongoId
field :corsponing_user, type: MongoId
field :title, type: String
field :corresponding_product, type: String
field :meeting_date, type: String
field :date_finalized, type: String
field :description, type: String
class << self
def getRequestByUser(user_id)
requests = where(user_id: user_id).to_a
if requests
requests
end
end
def getFinzlizedRequest(req)
request = find(req)
if request
request
end
end
def createFinalizedRequest(req_data)
request = self.new(req_data)
if request.save
request
end
end
def updateFinalizedRequest(request,req_data)
if request.update(req_data)
request
end
end
def delete_FinalizedRequest(req_id)
if find(req_id).delete
true
end
end
end
end
request.html.erb
div id="form-details-modal-lbms" class="reveal-modal" data-reveal>
<h3>Enter Contract Details:</h3>
<!--<form>-->
<%= form_tag({controller: "requests", action: "finalize_request"}, method: "GET",id:"post-form-lbms" ,data: {abide: ''}) %>
<input type="hidden" id="currect_opened_req_id" value="" name="FinalizeDeal[title]"/>
<select name="meeting-id">
<option value="1">Meeting 1</option>
<option value="2">Meeting 2</option>
</select>
<label for="details-lbms">Details</label>
<textarea id="details-lbms" name="FinalizeDeal[description]"></textarea>
<button class="button tiny">
Submit
</button>
</form>
<a class="close-reveal-modal">×</a>
</div>
Please tell me what am I doing wrong. I am also posting a link to the screenshot of the error
http://tinypic.com/r/n6t8w2/8
http://tinypic.com/r/33kdq1k/8
The code is complaining because you are requiring the :finalizedeal parameter (but apparently you are not passing it along) by adding this .require(:finalizedeal) to this code:
def finalizedRequest_params
params.require(:finalizedeal).permit(:title , :description)
end
So one solution would be to simply remove the require part. Like so:
params.permit(:title , :description)
#require source
Ensures that a parameter is present.

Resources