naming parameters in ruby on rails using form_for - ruby-on-rails

I'm trying to pass through some parameters using form_for in rails, how do I pass them so that they can be accessed in the controller like this: params[:image]? the start of my form_for tag looks like this:
<%= form_for(#image, as: :image, url: create_image_path(current_user), html: { multipart: true }, method: :post) do |f| %>

I can give you an example wtih carrierwave
gem install carrierwave
rails g model Resume attachment:string
rails g uploader attachment
**Add this in your model , controller and view **
class Resume < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
end
class ResumesController < ApplicationController
def index
#resumes = Resume.all
end
def new
#resume = Resume.new
end
def create
#resume = Resume.new(resume_params)
if #resume.save
redirect_to resumes_path, notice: "The resume #{#resume.name} has been uploaded."
else
render "new"
end
end
def destroy
#resume = Resume.find(params[:id])
#resume.destroy
redirect_to resumes_path, notice: "The resume #{#resume.name} has been deleted."
end
private
def resume_params
params.require(:resume).permit( :attachment)
end
end
<div class = "well">
<%= form_for #resume, html: { multipart: true } do |f| %>
<%= f.file_field :attachment %>
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
</div>

Related

How can I create a form without using resources (action :new, :create) in Rails?

This is my controller
class SchoolsController < ApplicationController
def teacher
#teacher = Teacher.new
end
def form_create
#teacher = Teacher.new(teacher_params)
if teacher.save
redirect_to schools_teacher_path
else
flash[:notice] = "error"
end
end
private
def teacher_params
params.require(:teacher).permit(:name)
end
end
This is my views/schools/teacher.html.erb
<%= form_for :teacher do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
I am new to Ruby on Rails, and not sure how to proceed.
You should move this to a TeachersController let me show you how:
First you need to create the controller, you can get this done by typing this on the terminal at the project root directory:
$ rails g controller teachers new
Then into your route file (config/routes.rb):
resources :teachers, only: [:new, :create]
After that go to the teachers_controller.rb file and add the following:
class TeachersController < ApplicationController
def new
#teacher = Teacher.new
end
def reate
#teacher = Teacher.new(teacher_params)
if #teacher.save
redirect_to schools_teacher_path
else
redirect_to schools_teacher_path, notice: "error"
end
end
private
def teacher_params
params.require(:teacher).permit(:name)
end
end
Then you can have the form at views/teachers/new.html.erb:
<%= form_for :teacher do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
Please let me know how it goes!

undefined method `images_path' for #<#

I am using CarrierWave to handle image uploading for my rails app (rails 4.1).
The issue is I am getting this
undefined method `images_path' for #<#
error every time try to go to /image/new.
I have checked my routes, my views, my controller, my model, and my uploader and everything seems fine. any advice or ideas?
Code:
image_uploader.rb:
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
/models/image.rb:
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
Relevant Routes:
Rails.application.routes.draw do
resources :image
image_controller.rb
class ImageController < ApplicationController
def new
#image = Image.new(:image => params[:image_params])
end
def create
#image = Image.create( image_params )
if #image.save
redirect_to #image
else
render 'new'
end
end
def show
end
private
def image_params
params.require(:image).permit(:title, :description, :image
end
def find_image
#image = Image.find(params[:id])
end
end
new.html.erb
<div class="col-md-12 col-md-offset-2">
<%= form_for #image, html: { multipart: true } do |f| %>
<%= f.file_field #image %>
<p id="uploadClick">Click to Upload</p>
<br>
<%= submit_tag 'Upload Image', id: 'submitPhoto' %>
<% end %>
</div>
Any help with this would be greatly appreciated, thanks!
SOLUTION:
in new.html.erb I had to change:
<%= form_for #asset, html: { multipart: true } do |f| %>
to
<%= form_for :asset, html: { multipart: true } do |f| %>
my new action is now working correctly after those changes, hope this helps people in the future.
Try changing resources :image to resources :images in routes.rb and change new and create methods in the controller like below
def new
#image = Image.new
end
def create
#image = Image.new(image_params)
if #image.save
redirect_to #image
else
render 'new'
end
end

Ruby on Rails: First argument in form cannot contain nil or be empty

I have a model named 'Image.rb' to store uploaded images, a controller 'images_controller.rb' and 'static_pages_controller.rb' with action 'tournaments'. I get the following error on navigating to '/tournaments':
ArgumentError in StaticPages#tournament
Showing ../static_pages/tournament.html.erb where line #9 raised:
First argument in form cannot contain nil or be empty
Extracted source (around line #9):
7 This is the information bulletin for the upcoming tournament
8 </h2>
9 <%= form_for(#image, html: {multipart: true}) do |f| %>
10 <%= render 'shared/error_messages', object: f.object %>
11 <span class="picture">
routes.rb
.
.
get 'tournaments' => 'static_pages#tournament'
resources :images
tournament.html.erb
<%= form_for(#image, html: {multipart: true}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<span class="picture">
<h4> </h4>
<h3>Upload a picture here!</h3>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Give it a caption.." %>
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
<%= f.submit "Post picture!" %>
</span>
<% end %>
static_pages_controller.rb
class StaticPagesController < ApplicationController
def tournaments
#image = Image.new
#images = Image.paginate(page: params[:page])
end
end
images_controller.rb
class ImagesController < ApplicationController
before_action :logged_in_user
def create
#image = Image.new(image_params)
if #image.save
flash.now[:success] = "Uploaded successfully"
redirect_to tournaments_path
else
flash.now[:alert] = "Something went wrong"
redirect_to tournaments_path
end
end
private
def image_params
params.require(:image).permit(:name, :picture)
end
end
image.rb
class Image < ActiveRecord::Base
mount_uploader :picture, PictureUploader
validates :name, presence: true, length: {maximum: 50}
validate :picture_size
private
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "Should be less than 5MB.")
end
end
end
I cannot understand where I'm going wrong. Any helpful pointers to resolving this error would be much appreciated.
You should change your def tournaments method to def tournament
def tournament
#image = Image.new
#images = Image.paginate(page: params[:page])
end
changed action tournaments to tournament.

View not recognising model attributes? Rails

I am trying to make it so that this form here will display certain fields based on the type of the website. In this case, I want it to display the form for when project.type == Website.
However I keep getting
undefined method `type' for #<Project::ActiveRecord_Relation:0x007ffe1cb543a8>
I am sure i can call .type normally because it works in the console.
Here are my files:
#views/assets/_new_asset.html.erb
<%= simple_form_for #asset do |f| %>
<% if #project.type == 'Website' %>
<%= f.input :name %>
<%= f.input :url %>
<%= f.button :submit %>
<% end %>
<% end %>
Here is my assets/controller
#controller/assets_controller.rb
class AssetsController < ApplicationController
def new
#asset = Asset.new
project = Asset.where(:project_id)
#project = Project.where(:id == project)
end
def create
#asset = current_user.assets.build(asset_params)
if #asset.save
flash[:notice] = "Asset successfully added."
redirect_to(#project, :action => 'show')
else
render(:action => 'new')
end
end
private
def asset_params
params.require(:asset).permit(:id, :type,:url, :page_rank, :rev_company ,:social_pages)
end
end
Well, you are getting back an object of ActiveRecord::Relation, not your model instance, thus the error since there is no method called type in ActiveRecord::Relation.
This should work
#project = Project.where(:id == project).first
OR
You can do like this too
<% if #project.first.type == 'Website' %>
Doing #project.first.type works because #project.first is returning the first instance of the model that was found by the where
#views/assets/_new_asset.html.erb
<%= simple_form_for #asset do |f| %>
<% if (#project.type == 'Website') %>
<%= f.input :name %>
<%= f.input :url %>
<%= f.button :submit %>
<% else %>
You Should not see this line.
<% end %>
In Controller
#controller/assets_controller.rb
class AssetsController < ApplicationController
def new
#asset = Asset.new
# As if i have no idea from where youre getting :project_id
# in your code so i changed that. add that to asset_params
# if required. Thanks!!!
#project = Project.where(id: params[:project_id]).take
end
def create
#asset = current_user.assets.build(asset_params)
if #asset.save
flash[:notice] = "Asset successfully added."
redirect_to(#project, :action => 'show')
else
render(:action => 'new')
end
end
private
def asset_params
params.require(:asset).permit(:id, :type,:url, :page_rank, :rev_company ,:social_pages)
end
end

Carrierwave, Rails 4, and Multiple Uploads

I have been banging my head against the wall trying to get Carrierwave, Rails 4, and Multiple Uploads all working together. I can get a single file upload working just fine as in this and many other projects.
This is not a nested situation - just simply uploading to a single model called Transcription and wanting to create a record for each document uploaded.
I cannot seem to find the correct way to declare the "document" field used for the carrierwave mount
mount_uploader :document, DocumentUploader
as an array for the strong parameters to recognize.
I have tried whitelisting: whitelisted[:document] = params[:transcription]['document'],
declaring the "document" as an array:
params.require(:transcription).permit(..... ,:document => [])
params.require(:transcription).permit(..... , { document: [] })
This all seems more like I am declaring the array for a nested model, but I really want Rails 4's strong parameters to simply see the "document" array created by the file_field, :multiple => true
ie. from the log: form-data; name=\"transcription[document][]
Has anybody successfully accomplished multiple uploads in Rails 4 with strong parameters? If so would you please share?
Thanks...
Cheers,
Bill
This is solution to upload multiple images using carrierwave in rails 4 from scratch
To do just follow these steps.
rails new multiple_image_upload_carrierwave
In gem file
gem 'carrierwave'
bundle install
rails generate uploader Avatar
Create post scaffold
rails g scaffold post title:string
Create post_attachment scaffold
rails g scaffold post_attachment post_id:integer avatar:string
rake db:migrate
In post.rb
class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end
In post_attachment.rb
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end
In post_controller.rb
def show
#post_attachments = #post.post_attachments.all
end
def new
#post = Post.new
#post_attachment = #post.post_attachments.build
end
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
params[:post_attachments]['avatar'].each do |a|
#post_attachment = #post.post_attachments.create!(:avatar => a, :post_id => #post.id)
end
format.html { redirect_to #post, notice: 'Post was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
def update
respond_to do |format|
if #post.update(post_params)
params[:post_attachments]['avatar'].each do |a|
#post_attachment = #post.post_attachments.create!(:avatar => a, :post_id => #post.id)
end
end
end
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to #post }
format.json { head :no_content }
end
end
private
def post_params
params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])
end
In views/posts/_form.html.erb
<%= form_for(#post, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<%= f.fields_for :post_attachments do |p| %>
<div class="field">
<%= p.label :avatar %><br>
<%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
</div>
<% end %>
<% if params[:controller] == "post" && params[:action] == "edit" %>
<% #post.post_attachments.each do |p| %>
<%= image_tag p.avatar, :size => "150x150" %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #post.title %>
</p>
<% #post_attachments.each do |p| %>
<%= image_tag p.avatar_url, :size => "150x150" %>
<%= link_to "Destroy", p, method: :delete %>
<% end %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%= link_to 'Back', posts_path %>
In rails 3 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4.
CarrierWave doesn't support multiple uploads. It's designed to associate a single file with a single field.
If you want multiple uploads, you need either multiple fields (each with a CarrierWave uploader), or multiple objects each with a single CarrierWave uploader field.
The multiple attribute is also unsupported, so if you use it, it's entirely up to you to get the parameters assigned properly.
I would create a model called Documents with a field that's mounted
class Documents < ActiveRecord::Base
belongs_to :transcription
mount_uploader :doc, DocumentUploader
end
class Transcriptions < ActiveRecord::Base
has_many :documents
end
And I would still have user the below line in my controller:
params.require(:transcription).permit(..... , { document: [] })
The best method for this that I have come across is using the native approach of CarrierWave. If you already have single file upload done, with the native approach it takes less than 5 minutes to get multiple file upload. https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads

Resources