Reails remote form format exists but not recognized - ruby-on-rails

The following form intends to generate an XHR response
<% articlediscounts_for_article = #articlediscounts.where(article_id: article.id).first %>
<%= form_with(scope: articlediscounts_for_article, url: user_discount_users_path, local: false, method: :post) do |form| %>
the action processes
Processing by UsersController#user_discount as JS
and generates expected results, but fails in the rendering process, complaining about (with initial stack)
ActionController::UnknownFormat (UsersController#user_discount is missing a template for this request format and variant.
request.formats: ["text/javascript", "*/*"]
request.variant: []):
actionpack (6.1.3.2) lib/action_controller/metal/implicit_render.rb:42:in `default_render'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
Yet that template clearly exists in the proper path (its corresponding partial is also present in the directory) as shown here:
This is perplexing as the warning message is contrary to the reality. The only assumptions that come to mind are that of some handling of form_with cmobined with the scope or the question of variant
What is mistaken here?

Related

Attachments in ActionMailbox/InboundEmails - unpermitted parametres

I'm trying to test how ActionMailbox works on localhost using:
http://localhost:5000/rails/conductor/action_mailbox/inbound_emails
It works fine until I try to add an attachment. Then I receive an error:
ActionController::UnpermittedParameters (found unpermitted parameter: :attachments):
actionpack (6.1.3.1) lib/action_controller/metal/strong_parameters.rb:973:in `unpermitted_parameters!'
actionpack (6.1.3.1) lib/action_controller/metal/strong_parameters.rb:604:in `permit'
actionmailbox (6.1.3.1) app/controllers/rails/conductor/action_mailbox/inbound_emails_controller.rb:23:in `new_mail'
actionmailbox (6.1.3.1) app/controllers/rails/conductor/action_mailbox/inbound_emails_controller.rb:17:in `create'
I was trying to add :attachments to permitted parameters in line 23 of inbound_emails_controller.rb but with no effect :/
I have no idea why it doesn't work and nobody else found this bug :(

Flexirest to handle string array

I'm using Flexirest to make a request to an endpoint that returns an array of strings with Content-Type: application\json.
This is the model:
class Neighborhood < BaseModel
get :by_location, "/neighborhoods/by_location"
end
And this is how I make the request:
def get_neighborhood_list
city_to_use = ApplicationHelper.user_location[:city] || Rails.configuration.default_location[:city]
#hoods = Neighborhood.by_location(city: city_to_use)
end
And this is called but it only reaches the super line since it breaks inside there:
def handle_response(response, cached = nil)
result = super(response, cached)
setup_paging_variables result, response
setup_paging_variables result.data, response if result.respond_to? 'data'
result
end
The endpoint returns an array of strings:
[
"Ahwatukee Foothills Village",
"Alhambra",
"Camelback East Village",
"Central City",
]
With this headers
My problem is that Flexirest is treating the strings as json objects instead due to the value on the Content-Type header.
I get this error
TypeError: no implicit conversion of Symbol into Integer
on this line on the if attributes[:_links].
It's getting there because hal_response? is true
Do I need to do something in particular in order for Flexirest to stop behaving like this?
Update
I guess I didn't explained it properly. The error is occurring on the gem side.
Here is the full error:
TypeError - no implicit conversion of Symbol into Integer:
flexirest (1.5.5) lib/flexirest/request.rb:612:in `handle_hal_links_embedded'
flexirest (1.5.5) lib/flexirest/request.rb:553:in `new_object'
flexirest (1.5.5) lib/flexirest/request.rb:704:in `block in generate_new_object'
flexirest (1.5.5) lib/flexirest/request.rb:703:in `generate_new_object'
flexirest (1.5.5) lib/flexirest/request.rb:501:in `handle_response'
config/initializers/01_flexirest.rb:7:in `handle_response'
flexirest (1.5.5) lib/flexirest/request.rb:219:in `block (2 levels) in call'
faraday (0.9.2) lib/faraday/response.rb:57:in `on_complete'
flexirest (1.5.5) lib/flexirest/request.rb:200:in `block in call'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
flexirest (1.5.5) lib/flexirest/request.rb:156:in `call'
flexirest (1.5.5) lib/flexirest/mapping.rb:46:in `_call'
flexirest (1.5.5) lib/flexirest/mapping.rb:28:in `block in _map_call'
app/controllers/application_controller.rb:93:in `get_neighborhood_list
The if attributes[:_links] line is part of the gem code, and it's here on this line
When it reaches this line the value of attributes is one of the strings inside the response, for instance: attributes = "Ahwatukee Foothills Village". That's why it gives the error.
What I'm trying to figure out if there is a way (maybe something in the configuration) that I can use Flexirest to handle this kind of responses.
This is a old question but thought I'd answer it. you need to add
plain: true
in your model like below.
class Neighborhood < BaseModel
get :by_location, "/neighborhoods/by_location", plain: true
end
It will then return the string array

NameError - uninitialized constant error in rails 4

I have the following code in my "ProgramsController.rb " file where, Im using a class called "DataTableDelegate" which is in a separate file called: "datatable_delegate.rb"
# GET /programs
# GET /programs.json
def index
puts "Running Program/index"
puts "Model name = #{controller_name.classify}"
respond_to do |format|
format.html
#datatable_options = generate_datatable_hash(view_context, controller_name.classify, Program.data_table_attribute_array )
log_with_blue("============================================")
log_with_yellow("#{#datatable_options.inspect}")
log_with_blue("============================================")
>>>>>> format.json { render json: DataTableDelegate.new( #datatable_options) }
end
end
The file "datatable_delegate.rb" is located at
app/datatables/datatable_delegate.rb
When I load the Programs url in the browser I get the following in my log:
Completed 500 in 237ms
NameError - uninitialized constant ProgramsController::DataTableDelegate:
activesupport (4.0.0) lib/active_support/dependencies.rb:500:in `load_missing_constant'
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
app/controllers/programs_controller.rb:22:in `block (2 levels) in index'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:191:in `respond_to'
app/controllers/programs_controller.rb:13:in `index'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
......
I tried to put a require statement in my programs controller file but I still am getting the error.....
What should I do?
Thanks
You do not need the require statement in your ProgramsController as all files in the app/ directory are autoloaded by Rails.
The problem is the way you're accessing the DataTableDelegate. It is namespaced with Datatable, hence the placement of this file is in app/datatables/ directory.
Try with the following:
::Datatable::DatatableDelegate.new( #datatable_options)
Please note the case of characters in the module and class names above.
Rename your file to 'data_table_delegate.rb'. Also check whether the path app/datatables is in your autoload_paths.

production-only error ActionView::Template::Error (can't convert Symbol into Integer)

I am attempting to deploy a site onto engineyard and I keep getting an error that seems to be a data formatting error. This only happens when deployed as production onto the server cluster. using the same DB and codebase locally or on a VPS we have no issues.
The errors seem to me to show that suddenly there is an array / hash type mismatch with some of the user data. It always seems to trace back to the model where it is tasked to get 'options'. The errors are triggered by the templates calling for access of checks on user attributes.
production log:
Completed 500 Internal Server Error in 32ms
** [Airbrake] Failure: Net::HTTPClientError
ActionView::Template::Error (can't convert Symbol into Integer):
18: = admin_user.username
19: = username_helper(admin_user)
20: %td= country_image admin_user
21: %td= admin_user.account_name
22: %td
23: - if admin_user.is_a?(Creative)
24: - if admin_user.is_juror?
app/models/user.rb:79:in `[]'
app/models/user.rb:79:in `has_option?'
app/models/contest_holder.rb:77:in `is_client_type?'
app/models/contest_holder.rb:81:in `is_agent?'
app/models/contest_holder.rb:70:in `ao_client_name'
app/models/contest_holder.rb:115:in `account_name'
app/views/admin/users/_users.html.haml:21:in `block in _app_views_admin_users__users_html_haml__845309245401675393_70184787547360'
trace:
ActionView::Template::Error: can't convert Symbol into Integer
Sample stack trace (show Rails)
/app/models/user.rb: 79:in `[]'
/app/models/user.rb: 79:in `has_option?'
…P/releases/20130814153250/app/models/contest_holder.rb: 77:in `is_client_type?'
…P/releases/20130814153250/app/models/contest_holder.rb: 81:in `is_agent?'
…P/releases/20130814153250/app/models/contest_holder.rb: 70:in `ao_client_name'
…P/releases/20130814153250/app/models/contest_holder.rb: 115:in `account_name'
…1/gems/haml-4.0.3/lib/haml/helpers/action_view_mods.rb: 10:in `block in render_with_haml'
…ed_gems/ruby/1.9.1/gems/haml-4.0.3/lib/haml/helpers.rb: 89:in `non_haml'
…1/gems/haml-4.0.3/lib/haml/helpers/action_view_mods.rb: 10:in `render_with_haml'
…ms/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb: 68:in `block in call'
…ms/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb: 56:in `each'
…ms/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb: 56:in `call'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 184:in `call!'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 164:in `call'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 184:in `call!'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 164:in `call'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 184:in `call!'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 164:in `call'
…ruby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/builder.rb: 49:in `call'
…VP/releases/20130814153250/lib/middleware/force_ssl.rb: 8:in `call'
…ems/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb: 35:in `block in call'
…ems/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb: 34:in `catch'
…ems/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb: 34:in `call'
…ndled_gems/ruby/1.9.1/gems/rack-1.4.5/lib/rack/etag.rb: 23:in `call'
…/ruby/1.9.1/gems/rack-1.4.5/lib/rack/conditionalget.rb: 25:in `call'
…/1.9.1/gems/rack-1.4.5/lib/rack/session/abstract/id.rb: 210:in `context'
…/1.9.1/gems/rack-1.4.5/lib/rack/session/abstract/id.rb: 205:in `call'
…es/20130814153250/config/initializers/session_store.rb: 10:in `call'
code from user.rb which seems to be the point of contact for the error:
def has_option?(option)
self.options[option] && self.options[option].to_s == "1"
end
an example of an options value:
--- !map:ActiveSupport::HashWithIndifferentAccess is_agent: "1"
Came to find out that the issue was an additional line break within the database file. Specifically the application uses serialized yaml data a fair amount. An additional line break had been added between the yaml hash key and value causing an error within the object. We initially tried creating a script to cleanse the db and came to find it seemed to be simply corrupt from the original db file.
This was what wrong with my case
Error : ActionView::Template::Error (can't convert Symbol into Integer)
Reason:
In my haml.
I was trying to access a property from a json object.
object[:key]
But actually it was a json array(Array of entities from active record where clause). I just filtered the first element.
Hope someone will find useful.

NameError in UsersController#show

I am currently working on the Ruby on Rails tutorial by Michael Hartl. I am trying to add a page for each user in my database by creating an HTML with embedded ruby page in the views directory. The code for show.html.erb is below:
<%= #user.name %>, <%= #user.email %>
When I add the user to the user_controller.rb file, it looks like this:
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
end
end
When I run the rails server and click on open up the users/1 URL, I get a NameError complaining about an uninitialized constant. The error and trace is below:
NameError in UsersController#show
uninitialized constant UsersController::User
Rails.root: /usr/sample_app
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:3:in `show'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:414:in
.
.
.
.
Please let me know how to go about this because I cannot pass my spec tests with this error. If anyone has any suggestions or insight I would greatly appreciate them.Thank you.
Controller file name should be users_controller.rb
As you mentioned in comment you don't have any User model defined. To create model run rails g model user name:string email:string. This will create User model with attributes name and email.
Try Rails scaffolding and see what files it creates(in controller, model and views ignore other files for now) and see the contents of those files. To use scaffold for creating User is as below:
rails g scaffold user name:string email:string
rake db:migrate

Resources