NoMethodError: undefined method `marshal_dump' for #<ActiveModel::Errors:0x0000000006fcbba0> - ruby

We use below ruby and rails version -
Ruby 2.7.3 Rails 6.1.3.2
While creating a new user it throws the below error -
NoMethodError (undefined method `marshal_dump' for #ActiveModel::Errors:0x0000000006fcbba0 Did you mean? marshal_load):
app/controllers/user_controller.rb:33:in `create'
Below Create method defined in the controller -
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end

Related

NoMethodError at /todo_lists undefined method `save' for #<ActiveRecord::Associations::CollectionProxy []>

I am making an app with a todo list and I recently tried to add the ability to build out the todo lists from the user.
I ran into a problem when I changed the following lines.
def new
#todo_list = current_user.todo_lists.build
end
def create
#todo_list = current_user.todo_lists(todo_list_params)
respond_to do |format|
if #todo_list.save
format.html { redirect_to #todo_list, notice: 'Todo list was successfully created.' }
format.json { render :show, status: :created, location: #todo_list }
else
format.html { render :new }
format.json { render json: #todo_list.errors, status: :unprocessable_entity }
end
end
end
I was using the following code.
def new
#todo_list = TodoList.new
end
def create
#todo_list = TodoList.new(todo_list_params)
respond_to do |format|
if #todo_list.save
format.html { redirect_to #todo_list, notice: 'Todo list was successfully created.' }
format.json { render :show, status: :created, location: #todo_list }
else
format.html { render :new }
format.json { render json: #todo_list.errors, status: :unprocessable_entity }
end
end
end
I have added a user id to the todo items by creating a new migration and migrated the data base and created an association to the user for the posts. Not really sure what's going wrong but any help would be greatly appreciated.
Thanks.
Change this line:
#todo_list = current_user.todo_lists(todo_list_params)
into this:
#todo_list = current_user.todo_lists.build(todo_list_params)

Error messages not showing

for some reason my validation error messages are not showing on the window. does it have any to do with twitter-bootstrap? I remember seeing some video tutorial, but cannot find it now unfortunately.
_form partial of record
<%= form_for [#estate,#record] do |f| %>
<% if #record.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#record.errors.count, "error") %> prohibited this record from being saved:</h2>
<ul>
<% #record.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
.......
......
...
<% end %>
my record model
validates :E1, presence: true
validates :E2, presence: true
validates :E3, presence: true
validates :E4, presence: true
validates :R1, presence: true
validates :R2, presence: true
validates :R3, presence: true
validates :R4, presence: true
validates :Year, presence: true
validate :validation
the create and update method of the controller
def create
#record = #estate.records.build(params[:record])
respond_to do |format|
if #record.save
format.html { redirect_to [#estate,#record], notice: 'Record was successfully created.' }
format.json { render json: #record, status: :created, location: #record }
else
format.html { render action: "new" }
format.json { render json: #record.errors, status: :unprocessable_entity }
end
end
end
# PUT /records/1
# PUT /records/1.json
def update
#record = Record.find(params[:id])
respond_to do |format|
if #record.update_attributes(params[:record])
format.html { redirect_to [#estate,#record], notice: 'Record was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #record.errors, status: :unprocessable_entity }
end
end
end
does it have anything to do that record belongs to estate and estate has many records. like while creating a record i do " #record = #estate.records.build(params[:record])"
I tried debugging it using debugger, I found out that when creating a record it is not going into the else part, it is directly creating a record without checking for the validations from the record model.
Take a look at this gem http://rubygems.org/gems/dynamic_form
It will display validation errors for you in your view. You could then delete your code looping on your instance errors.
Also, nothing to do with your issue here, but I suggest refactoring your code with the following:
in your controller:
respond_to :html, :json
def create
#record = #estate.records.build(params[:record])
if #record.save
# flash[:success]... or whatever
else
# flash[:alert]... or whatever
end
respond_with #record
end
# same for your update method
For your information you can replace render action: 'new' by render :new

Should I be using Feedzirra in the model?

I'm playing about with the Ruby Feedzirra gem and have managed to acheive what I set out to do by using it in the controller.
Though everything I've seen has mentioned using it in the model. I was just wondering if this is ok to do in the controller, if not, how might I go about achieving the same in the model?
I want to submit a Feed URL and use that to update my model with the rest of the information about the feed.
Feeds_controller.rb
def create
#feed = Feed.new(params[:feed])
feed = Feedzirra::Feed.fetch_and_parse(#feed.feed_url)
#feed.title = feed.title
#feed.url = feed.url
#feed.last_modified = feed.last_modified
respond_to do |format|
if #feed.save
format.html { redirect_to #feed, notice: 'Feed was successfully created.' }
format.json { render json: #feed, status: :created, location: #feed }
else
format.html { render action: "new" }
format.json { render json: #feed.errors, status: :unprocessable_entity }
end
end
end
feed.rb
class Feed < ActiveRecord::Base
attr_accessible :feed_url, :last_modified, :title, :url
end
I'd make an instance method on the Model and use that in the controller so
class Feed < ActiveRecord::Base
def fetch!
feed = Feedzirra::Feed.fetch_and_parse(feed_url) # probably want some eror handling here
title = feed.title
url = feed.url
last_modified = feed.last_modified
self #or nil if you like
end
end
then your controller thins down to
def create
#feed = Feed.new(params[:feed])
#feed.fetch!
respond_to do |format|
if #feed.save
format.html { redirect_to #feed, notice: 'Feed was successfully created.' }
format.json { render json: #feed, status: :created, location: #feed }
else
format.html { render action: "new" }
format.json { render json: #feed.errors, status: :unprocessable_entity }
end
end
end
The cool thing here is that if this gets too slow you can use something like Resque to run this in the background and just return a "your request is being processed" message to the user, then alert them asynchronously when the the request is done (might not work so well for the json request)
You really don't want to use something like a RSS parser in your controller. That will slow the responsiveness of that URL and your server.
Instead, run the RSS parser in a separate application, or at least a separate thread, and store the retrieved feeds in a database table for rapid access.

Devise touch session (prevent timeout)

I'm using Devise timeoutable to time out user sessions, and am trying to use client-side AJAX calls to occasionally touch the user session and keep the session alive. However, the server-side call in my subclass of Devise::SessionsController is not actually "touching" the session and preventing timeout:
def touch
respond_to do |format|
format.html {head :bad_request}
format.js {
render :json => { :session_expires => User.timeout_in.to_i}, :status => :ok
}
format.json {
render :json => { :session_expires => User.timeout_in.to_i}, :status => :ok
}
end
end
What do I need to do to actually cause some session activity and prevent the timeout from triggering?

Newbie Ruby on Rails - Pass string to method from view?

I am trying to implement the feedzirra (good railcast: http://railscasts.com/episodes/168-feed-parsing) in my view add_feed but I am having some trouble with it. I want a user to be able to add a feed while he is on the website. I think it should be pretty simple, I can add the feed from the console but I haven't figured out how to pass information to a method from a form yet.
My model looks like this (almost the same as the one from railscast):
def self.update_from_feed(feed_url)
feed = Feedzirra::Feed.fetch_and_parse(feed_url)
add_entries(feed.entries)
end
def self.update_from_feed_continuously(feed_url, delay_interval = 15.minutes)
feed = Feedzirra::Feed.fetch_and_parse(feed_url)
add_entries(feed.entries)
loop do
sleep delay_interval
feed = Feedzirra::Feed.update(feed)
add_entries(feed.new_entries) if feed.updated?
end
end
private
def self.add_entries(entries)
entries.each do |entry|
unless exists? :guid => entry.id
create!(
:name => entry.title,
:summary => entry.summary,
:url => entry.url,
:published_at => entry.published,
:guid => entry.id
)
end
end
end
I am not really sure how pass a string to my self_update_from_feed(String) method with my controller and view. My controller currently looks like this:
def add_feed
#feed = String
end
def new
#feed = Feed.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #feed }
end
end
def edit
#feed = Feed.find(params[:id])
end
def create
#feed = Feed.new(params[:feed])
#feed.user_id = current_user.id
respond_to do |format|
if #feed.save
if #feed.url != nil
#feed.update_from_feed(:url)
end
format.html { redirect_to #feed, notice: 'Feed was successfully created.' }
format.json { render json: #feed, status: :created, location: #feed }
else
format.html { render action: "new" }
format.json { render json: #feed.errors, status: :unprocessable_entity }
end
end
end
And my view... well..
<%= form_for #feed do |f| %>
<%= ???%>
<% end %>
Thanks in advance for any response. I usually get really good help here at stackoverflow :)
model
class Feed < ActiveRecord::Base
attr_accessible :feed_url
after_create { |feed| FeedEntry.update_from_feed(feed.feed_url) }
end
controller
class FeedsController < ApplicationController
def create
#feed = Feed.new(params[:feed])
respond_to do |format|
if #feed.save
format.html { redirect_to #feed, notice: 'Feed was successfully created.' }
else
format.html { render action: "new" }
end
end
end
end
view
<%= form_for #feed do |f| %>
<div class="field">
<%= f.label "Feed URL" %><br />
<%= f.text_field :feed_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Resources