I have the following code in my controller
#current_chat = current_user.sent_messages
.where("created_at > ? and receiver_id = ?", current_user.current_sign_in_at, current_chat[:receiver_id].to_i)
.select("body, created_at").each { |message| message.instance_eval { def type; #type end; #type = 'sent' } }
And I'm passing the #current_chat object to a partial like so:
<%= render partial: 'shared/chat_form', locals: { messages: #current_chat } %>
But I'm getting the following error:
singleton can't be dumped
At the first line in ActiveSupport::MessageVerifier#generate
def generate(value)
data = ::Base64.strict_encode64(#serializer.dump(value))
"#{data}--#{generate_digest(data)}"
end
Any ideas on how to fix this?. Thanks in advance.
You can not use this
#serializer.dump(value)
This is causing the error. Read this link, its all about using singletons in ruby.
link
Related
I'm trying to refactor the Companies_Controller#index method to encompass less logic by moving most of the query into a scope, company_search_params.
What is the best way to pass the param to the model scope? I'm getting an error thrown back, wrong number of arguments (given 0, expected 1). I'm relatively new to writing scopes and couldn't find much on passing arguments/conditions that was applicable in the Rails Guide.
Companies Controller
def index
params[:name_contains] ||= ''
Company.company_search_params(params[:name_contains])
#search = Company.company_search_params
#companies = #search.page(params[:page])
end
Company Model
scope :company_search_params, ->(name_contains){
where(
<<-SQL
"name LIKE :match OR subdomain LIKE :match", { match: "%#{name_contains}%" }
SQL
).where(is_archived: false).order(name: :asc)
}
Thanks for your help.
using named_scope sample and info
scope :named_scope, lambda {
|variable1, variable2|
where...
order...
}
#when you call it from your controller
Model.named_scope("value 1","value 2")
for your problem
in your company.rb
scope :company_search_params, lambda {
|name_contains|
where(
<<-SQL
"name LIKE :match OR subdomain LIKE :match", { match: "%#{name_contains}%" }
SQL
).where(is_archived: false).order(name: :asc)
}
company_controller.rb
def index
#search = Company.company_search_params(params[:name_contains])
#companies = #search.page(params[:page])
end
I am using rails 3 and fragment cache.
Here is my controller code(show method):
if request.format == 'pdf'
render pdf: #design.slug, layout: 'application'
else
**expire_fragment('design_printed_by')**
#design.add_evaluation(:viewed_count, 1, current_user) if current_user && !current_user.evaluated?(#design, :viewed_count)
respond_with #design
end
And here is my view file code(show.html.slim) :
- cache ['design_printed_by', #design], skip_digest: true do
= render partial: 'printed_by', locals: { design: #design }
But when i add new record, this record is not display on page.
I don't know what i am missing.
Is there anything wrong with this code?
Check this link may it help you.
http://guides.rubyonrails.org/caching_with_rails.html#fragment-caching
I am stuck with what I think is a simple problem. I am creating json and need to have the format be:
[{ "source" : "google / organic", "visits" : 20 }]
And here is what I get:
[{"source"=>"google / organic", "visits"=>20}]
Here is the model (campaign_results.rb)
def as_json(options = {})
{ "source" => source,
"visits" => visits,
}
end
In the controller:
def show
#campaign_summary = CampaignResults.all
end
In the view:
<%= raw #campaign_summary.as_json %>
Any suggestions on what I should do to replace the "=>" with ":"?
Try calling #to_json:
<%= raw #campaign_summary.as_json.to_json %>
I know how to pass parameters the dumb way. For example,
<%= link_to "Order", new_order_item_path(:item_id => #item.id) %>
The OrderItemsController receives it as params[:item_id] = id.
Problem:
#order_item = OrderItem.new(params)
raises an exception (Can't mass-assign protected attributes: action, controller). I can get around this with the following code.
#order_item = OrderItem.new
#order_item.item_id = params[:item_id]
I know the controller requires params[:order_item][:item_id] for new to work the first way. My question is, how do I get new_order_item_path to generate url? I know this isn't a major problem, but it just bugs me that I don't know the cleaner/proper way to do this. I have tried searching, but only received unrelated questions/answers/results.
Thanks
You didn't really specify if you didn't want to use it or not, but in your model, you could make the attribute item_id accessible like so:
class OrderItem < ActiveRecord::Base
attr_accessible :item_id
...
In this way,
#order_item = OrderItem.new(params)
would work.
Hope this helps.
How about this:
# Controller
def get_item_edit_method
#order = OrderItem.find("your criteria")
##order = OrderItem.new # if new
#item = Item.new()
end
def post_item_edit_method
#order = OrderItem.new(params) # should work now
#order.save
end
# End controller
<!-- view -->
<% #order.item = #item %>
<%= link_to "Order", new_order_item_path(#order) %>
<!-- end view -->
I have in my controller this:
#itemsok = Search.where("first_item_id = ?", params["3"])
This is sopposed to be a query in the search table of the database asking for all the searches that have a first_item_id = 3 ...
Question 1 .- The syntax is I found it in http://guides.rubyonrails.org/active_record_querying.html but im not sure if im using it right?
Ok the question 2 is, I have this on the controller, is it ok to have querys in the controller?
In the view im printing the variable <%= #itemsok %> and all I get is a
ActiveRecord::Relation:0x007fd3d3e894d8
Any suggestions?
Thanks in advance.
ActiveRecord 3 lets you chain relations together so you can do something like this:
#itemsok = Search.where("first_item_id = ?", params["3"]).where("foo = ?", "bar")
The where() function returns an ActiveRecord::Relation. Generally this isn't a problem, since if you use the object it'll automatically run the query and return the results on the object so you'll get the database objects. AR doesn't run the query until it's actually needed.
Where will return a list of items (Array), so if you're just debugging, change your view to this:
<%= debug #itemsok.to_a %>
You seem to be constructing the query wrong way.
If you want to search for records with first_item_id = 3, you should do:
Search.where("first_item_id = ?", 3)
This will return an array of matching records, something you can't easily print with <%= #itemsok %>. You should iterate over the elements and print each one:
<% #itemsok.each do |item| %>
<%= item.name %>
<% end %>
I'd also suggest defining to_s method for the objects you want to print.
class Search
def to_s
name
end
end
Then you can simply print the object and to_s method will be automatically called for you:
<% #itemsok.each do |item| %>
<%= item %>
<% end %>
The right way to do is to define a namedscope in the model and then use it in the controller.
Something similar to this :
class Search < ActiveRecord::Base
named_scope:item_ok,lambda {|*args|{:conditions=>["item_id >= ?", args.first]}}
end
and then call the namedscope from the controller like this :
#itemsok = Search.item_ok(params[:value])