mardi 4 août 2015

Failing to pass arbitrary form parameters to Rails controller

I have a form with several fields that aren't in my model but I'm not able to access them inside my controller.

My new form is fairly simple and was generated with some rails scaffolding. It's inside my controller's create method that I'm not able to access these params.

I've added the params with attr_accessor to my controller, which looks something like:

class LittleClassSessionsController < ApplicationController
  before_action :set_event_session, only: [:show, :edit, :update, :destroy]
  attr_accessor :schedule_type

My view has a form field that looks like this. I can see the parameter's value being submitted in the console.

<select name="schedule_type" id="schedule_type">
  <option value="1">Recurring— Create a series of class sessions in advance</option>
  <option value="2">Not recurring— Create just one class session in advance</option>
</select>

I've added :schedule_type to my whitelisted params. When trying to puts the params to my console, it's not in there.

What am I missing?



via Chebli Mohamed

ActionController::RoutingError (No route matches [GET] "/scan"):

Getting an error for a simple index route in rails:

Here are my routes:

Prefix Verb URI Pattern           Controller#Action
scan_index GET  /scan/index(.:format) scan#index
      root GET  /                     scan#index
      scan GET  /scan(.:format)       scan#index

Yet, typing in the following url:

http://ift.tt/1Ulkpus

Produces the following in error_log:

I, [2015-08-04T15:38:42.902191 #24943]  INFO -- : Started GET "/scan" at 2015-08-04 15:38:42 +0000
F, [2015-08-04T15:38:42.902936 #24943] FATAL -- : 
ActionController::RoutingError (No route matches [GET] "/scan"):



via Chebli Mohamed

Rails - Controller that does not check for CSRF token

In my rails application, one of the controllers displays public statistics that I want websites hosted on different domains to pull data from. (http://ift.tt/1ML3M9n)

My controller code is given below:

class StatsController < ApplicationController 
require 'ostruct'
skip_before_action :verify_authenticity_token
respond_to :html, :xml, :json, :csv

def index
    @stats = OpenStruct.new
    @stats.users = User.all.count
    @stats.organizations = Organization.all.count
    @stats.donors = Person.all.count
    respond_to do |format|
        format.json {render json: @stats}
    end
end
end

I thought the line skip_before_action :verify_authenticity_token would be enough, but when I try to make requests to this page from the console, I get the following error:

XMLHttpRequest cannot load http://ift.tt/1ML3M9n. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

How can I fix this?



via Chebli Mohamed

How do I specify the join table on a has_many through association?

I have a users table (admin_users) a join table (UserCompanies) and a companies table (Companies), each has an active record model (AdminUser, TableModule::UserCompany, TableModule::Company). I want to do something like the following:

AdminUser.first.companies

But, my attempts so far are not working, I'm assuming because I need to specify the table names, model names, or key names, but I don't know how that works with a has_many through relationship. Here is my best attempt at defining it so far:

class AdminUser < ActiveRecord::Base
    has_many :companies, through: :user_company, source: "TableModule::UserCompany"
end

How do I properly specify this relationship?



via Chebli Mohamed

Unable to send mail with attachment using Mandrill-api gem (Rails 4.1)

Emails without attachment are delivered fine, but when there is an attachment I get the following error in production:

ArgumentError: An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.

  • Letter opener in dev model shows the email rendering perfectly
  • This error only shows up in production

the call to mailer is:

# template variables
merge_vars = {"AMOUNT" => "100"}

invoice = Invoice.new(customer)

attachments[invoice.name] = {
  data: invoice.invoice_data,
  mime_type: 'application/pdf'
}

mail(to: user.email, cc: cc_emails.compact.uniq, subject: mail_subject, content_type: content_type) do |format|
  # Template renders fine
  format.html{mandrill_template('invoice_template', merge_vars)}
end

InvoiceMailer < ActionMailer::Base has defaults for "from" and "reply_to"

Mandril gem version 1.0.53 and Rails 4.1.10.



via Chebli Mohamed

How can I call a method within active_scaffold?

I can't seem to find any questions similar to this that have been asked. Maybe this means I am way off and this is a dumb question.

I am trying to add a feature to a preexisting app. This feature allows the user to copy a report over to the next period (a fiscal year).

Here is the abridged active_scaffold code:

active_scaffold :report do |config|
  config.list.columns = [:user, :period, :division, :released, :reportable, :comp_plan]
  config.create.columns = [:user, :period, :division, :released, :reportable, :comp_plan]
  config.update.columns = [:user, :period, :division, :released, :reportable, :comp_plan]

  config.actions.exclude :show

  config.columns[:user].form_ui = :select
  config.columns[:period].form_ui = :select

  config.columns[:user].clear_link
  config.columns[:period].clear_link

  config.columns[:user].search_sql = ["users.first_name", "users.last_name"]
  config.search.columns << :user

  config.nested.add_link :access_rights, :label => 'Access'
end

Now I want to add a link to each row which calls a function which copies that report.

Any ideas are greatly appreciated.

Edit: Most of the code probably isn't relevant to the question but I figure it would be helpful to see anyways.



via Chebli Mohamed

Paperclip S3 Bucket and Rails Images will upload but will not display

I am using paperclip gem along with an AWS s3 bucket to upload images to my app. I have it all working properly and the images will upload to the actual bucket and the web pages will load. The only problem is the images themselves will not display and it will only display their names like enter image description here

to display the image I am using the code

<%= image_tag @post.image.url(:medium), class: "edit_recipe_image" %>

has any one experiences this before or possibly know a solution to fix this?

Thanks in advance!



via Chebli Mohamed