Save the file manually using paperclip from the params, without scaffold - hash

I am new to rails, I was working on paperclip gem and wanted to save the simple files, saved from paperclip.
I have the My model as follow :-
class UserAttachment < ActiveRecord::Base
attr_accessible :email, :user_id, :attached_file
has_attached_file :attached_file
validates_attachment_presence :attached_file
validates_attachment_size :attached_file, :less_than => 20.megabytes
end
My controller action where the form is called :-
class HomeController < ApplicationController
def index
#uattachment = UserAttachment.new
end
end
Index view code, where the form is located
<%= form_for #uattachment, :url => attachment_get_link_path, :html => { :method => :post, :id => 'attachment_form', :multipart => true }, :remote => true do |f| %>
<%= f.email_field :email, :value=>nil, :placeholder => "Enter your email here", :required => true %><br />
<%= f.file_field :attached_file %>
<%= f.submit "Submit" %>
<% end %>
I want to use some following kind of code to save the data :-
(Code below is not the correct code, it's an excitation to tell what I want to do in my application.)
#uattachment = UserAttachment.new
#uattachment = params[:user_attachment]
#uattachment.save
Params received are as follows :-
(rdb:6) pp params
{"utf8"=>"✓",
"authenticity_token"=>"dfjaskldjadslgjsoidruts48589034lsker=",
"user_attachment"=>
{"email"=>"testing#email.com",
"attached_file"=>
#<ActionDispatch::Http::UploadedFile:0x007fcb58682ba0
#content_type="image/jpeg",
#headers=
"Content-Disposition: form-data; name=\"user_attachment[attached_file]\"; filename=\"someimage.jpg\"\r\nContent-Type: image/jpeg\r\n",
#original_filename="800px-Kinnaur_Kailash.jpg",
#tempfile=#<File:/tmp/RackMultipart20121205-8432-1fc1kpi>>},
"commit"=>"Submit",
"controller"=>"attachment",
"action"=>"get_link"}

Got the catch, it was pretty simple, I checked it from the scaffolded assignment,
def index
#uattachment = UserAttachment.new(params[:user_attachment])
#uattachment.save
end
And you get the golden words in response "true".

Related

Rails 4: undefined method `permit' for "xxxx xxxx":String

I have a search model that I hope to use in conjuction with sunspot to handle all searches across different models. I'm stuck before I even have gotne started.
Here is the form...that appears in the header of all the web pages on my site.
<%= simple_form_for #search, :url => searches_path, :method => :post do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", class: "btn btn-primary" %>
<% end %>
In my searches_controller
def search_params
params.require(:search).permit(:search)
end
In my Search.rb
attr_accessor :search
When I plug in my name into it I get this error
undefined method `permit' for "simon walsh":String
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"1Tez0pnMILEciLR6j+li+qSeO4NYBj3XsB6dYG07RymsiNUNSAGI5ztpMiD4JNAtTnqwJYdHTpPBRqGduWHjBw==",
"search"=>"simon walsh",
"commit"=>"Search"}
I am confused. This is clearly a simple error with the way I am posting the params. Any help?
Use this:
def search_params
params.require(:search).permit!
end
Instead of this:
def search_params
params.require(:search).permit(:search)
end
This issue may occur because of string parameter in params["search"], while in controller you are using .permit(:search), or maybe you have an attribute and model name that are the same.

Call method after submitting form Rails

I have the following model:
class Coupon < ActiveRecord::Base
belongs_to :company
validates :description, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
validates :fine_print, presence: true
end
and the following method in the coupon controller:
def redeem
if params[:pin] == #coupon.company.pin
redirect_to root_path
else
flash.now[:notice] = "Incorrect Pin"
render :show
end
end
This form is in the a view:
<%= form_for( #coupon, :url => coupons_redeem_path( #coupon ), :html => { :method => :post } ) do |f| %>
<%= label_tag("pin", "Search for:") %>
<%= text_field_tag("pin") %>
<%= f.submit "Close Message" %>
<% end %>
I want the form to call the redeem method in the coupons controller when hitting submit but am getting this error:
No route matches [POST] "/coupons/redeem.1"
EDIT
These are my routes:
resources :companies do
resources :coupons
end
get 'coupons/redeem'
In your routes, coupons are nested resources of companies. So you should choose one of these alternatives:
1st:
resources :companies do
resources :coupons do
post :redeem, on: :member
end
end
This leads to helpers like this: redeem_company_coupon_path(company, coupon) (and send smth there via POST).
If you don't want to include company to your path, you could choose 2nd:
resources :companies do
resources :coupons
end
post 'coupons/:id/redeem', to: 'coupons#redeem', as: :redeem_coupon
After that you could use redeem_coupon_path(coupon) helper

Rails: how to save few elements from string

I have rails form_tag helper to save data provided by do..each loop. This is my form:
<%= form_tag (customers_path) do |f| %>
< #contacts.each do |c|%>
<%= check_box_tag "accept[]", c %><%= c[:email] %>
<% end %>
<%= submit_tag ("Save") %>
<%end%>
This form saves the contacts whose checkbox is checked. Here is what, c has:
"accept"=>["{:id=>\"2f310f1d9b8f\",
:first_name=>\"San\",
:last_name=>\"Jori\",
:name=>\"Jori,San\",
:email=>\"abc#qwe.com\",
:gender=>nil,
:birthday=>nil,
:profile_picture=>nil,
:relation=>nil}"],
"commit"=>"Save"
I want to save only :name and :email from above hash.
This is my create action of controller:
if params[:accept].present?
params[:accept].each do |customer|
#customer = current_user.customers.new(:name => customer[:name], :email => customer[:email])
#customer.save
end
redirect_to customers_path
end
But it is giving error of :
no implicit conversion of Symbol into Integer
Can anyone tell me how to make it work?
Thank you!
Instead of using
#customer = current_user.customers.new(:name => customer[:name], :email => customer[:email])
Try
#customer = current_user.customers.build(:name => customer[:name], :email => customer[:email])

searchresult usin sphinx

i am using sphinx beta and thinking-sphinx to perform search functionality..the search i
wann should search for the word i entered in the database.which is populated with data already.
so in my application Mydoc,i got articlesmodel and controller.. articles is db is populated with data.
i wann to search for the data in articles..so far i ve done with following things but not getting the search result
=> created new search controller
def index
#articles = Article.search params[:search]
end
=> articles.rb(model)
define_index do
indexes name, :sortable => true
indexes description
indexes title, :sortable => true
end
def self.search(search)
ThinkingSphinx::Search
end
=> searches/index.html.erb
<%= form_tag do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "search", :name => nil %>
</p>
<% end %>
BUT WEN I CLICK ON THE SEARCH BUTTON ITS ASKING FOR CREATEACTION ?????
PLEASE COULD U HELP TO GET SEARCH RESULT
Your form is making a POST request to your SearchController, and that is interpreted as a call to the create action. Try using a GET request instead with your form - something like this:
<%= form_tag '', :method => :get do %>

how do I generate a tag cloud in acts_as_taggable_on?

I can't debug why I am getting an error:
class VendorsController < ApplicationController
def tag_cloud
#tags = Vendor.tag_counts_on(:tags)
end
I set this class as Taggable:
class Vendor < ActiveRecord::Base
acts_as_taggable_on :tags, :competitors
I include the TagsHelper:
module VendorsHelper
include TagsHelper
end
This is in my View:
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>
This is the error that I get:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.empty?
Each instance of Vendor has at least one Tag.
Got it, I needed to add: #tags = Vendor.tag_counts_on(:tags) in the index controller.