Trouble using Simple Form on a rails starter project - ruby-on-rails

I'm getting the following error while trying to create a form, undefined method 'model_name' for NilClass:Class
This project of mine is to capture my ideas. I could use a google doc but figured this should be easy enough to try and code an app for myself. Below is the code as I think it should be from what I read on the simple form site in my newopp.html.erb (in my opps view folder.
I created a controller and a model and I am certain part of my problem is the fact that I can't figure out what I need to code or what to add for code to properly complete this step. All the tutorials I have looked at gave me a couple ideas to play with and try to make work to no avail.
So basically I am sitting on a rails generated controller named opps_controller.rb and a model called opp.rb. Both of these are nothing more than what the generator provided since I had to go back to square one
Simple form code that I have started with
<%= simple_form_for #opp do |f| %>
<%= f.input :title %>
<%= f.input :subtitle %>
<%= f.input :description %>
<%= f.input :idea %>
<%= f.input :added %>
<%= f.button :submit %>
<% end %>
opp.rb file
class Opp < ActiveRecord::Base
attr_accessible :added, :description, :idea, :subtitle, :title
end
If it makes a difference, I have migrated the database, I ran the simple form install script with the bootstrap configuration. I'm using rails 3.2.9 and ruby 1.9.3p362 (2012-12-25 revision 38607) [x86_64-darwin12.2.1]
As I mentioned I just have a blank controller that was created using a generator and I need to get the CRUD functionality working. Everything I have tried has failed at this point. I appreciate any assistance you can provide.

It shouldn't be newopp.html.erb, just new.html.erb. So the path would be /views/opps/new.html.erb
If you're still getting an error then make sure #opp is defined in the controller:
class OppsController < ApplicationController
def new
#opp = Opp.new
end
def create
#opp = Opp.new
if #opp.update_attributes(params[:opp])
...
else
render 'new'
end
end
end

Related

Uploading 2 images in rails using two field

I have a form for a blog, and I would like to have two field for images. One image being the cover (in Show) and another image will serve as a preview (in index).
My form looks as follow:
<%= semantic_form_for #blog, :html => { :multipart => true } do |f| %>
<%= t :Choose_File_for_cover %> <%= f.file_field :image_path, id: "avatar-upload2", required: true %>
<img id="img_prev3" width="100%" height=200 src="#" alt="your image" class="img-thumbnail hidden"/>
<%= t :Choose_File_for_homepage %> <%= f.file_field :homepagepic, id: "avatar-upload3", required: true %>
<%= f.hidden_field :image_path_cache %>
<%= f.hidden_field :homepagepic_cache %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
My model looks like:
class Blog < ApplicationRecord
belongs_to :user
acts_as_taggable
mount_uploader :image_path, BlogUploader
mount_uploader :homepagepic, BlogcoverUploader
end
It works well when I only have the image_path (the cover), but when I add a new field for homepagepic, i get a ROLLBACK at validation.
Can someone help me on how to select files through two separate fields on the same form please.
Thank you
The code you've provided is very sparse and it would be helpful to see a little bit more (e.g. the controller and the uploader).
I can, however, hazard a guess: image_path is an existing helper method provided by Rails (see https://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-image_path). I have absolutely no idea what happens when you use this as a name for a form field. It could also be because you declare your submit button to be an input (I've only ever seen and used as: :button for f.action :submit).
So overall, I would pick the following approach:
rename your upload fields to cover_image and the other one to preview_image (that's what you've described in your posts as their respective purpose, so you should name them accordingly)
change the submit to a button and remove all the noise from your template and start with the bare minimum: the two upload fields and nothing else (see sample code below – note that I haven't tested it but it should work or be very close to working)
after that works, start adding back the noise (i.e. the translations, the cache fields etc.)
Test that it still works after every step. If you can write a Capybara test, do that – otherwise test it manually.
If you have questions, feel free to ask.
<%= semantic_form_for #blog, html: { multipart: true } do |f| %>
<%= f.file_field :cover_image %>
<%= f.file_field :preview_image %>
<%= f.actions do %>
<%= f.action :submit, as: :button %>
<% end %>
<% end %>
class Blog < ApplicationRecord
belongs_to :user
acts_as_taggable
mount_uploader :preview_image, BlogUploader
mount_uploader :cover_image, BlogcoverUploader
end
As the previous poster said it's hard to debug your code without all the pieces to the puzzle. A ROLLBACK is happening because one or more validations failed.
Any time you have a ROLLBACK you can add a ! to the create or update method being called on the object being rolled back and ActiveRecord will throw an error telling you why the ROLLBACK happened instead of failing gracefully.
Once you know why your object isn't persisting you can check the params of the controller action that form is submitting to. Perhaps you forgot to whitelist a param via strong params?

How should I approach making a custom form involving different model attributes?

I'm rather new to Ruby, so I need guidance as how to approach this. I know how to make very simple forms, but I'm thinking of implementing a custom form that can generate a payroll for a business during a specific start and end date. I already have business and payroll models, but I'm not sure how to implement this type of custom form and properly route it to get it be fully functional. Are there any resources or pieces of guidance I can get to help me with this?
please checkout this link :create an application on RoR. Please go to the link above and get started by creating your very own application. I have started with this and have learned a lot from this. This will get you kick started on RoR. But I believe you need more guidance in Ruby also. So I am attaching some more useful links. Please check them out also.
tutorails point - ruby
ruby-lang documentation
ruby guides
Thank you.
I don't know if I really get your issue... If your models Business and Payroll are linked (with :belongs_to and :has_many) you should check the fields_for method. You can nest it in a form_for/form_with to create fields associated to an other model. For instance you can do something like that:
#in your view
<%= form_with model: #business |f| %>
<%= f.label "Something:" %>
<%= f.text_field :something %>
<%= f.fields_for #business.payroll do |ff| %>
<%= ff.label "Something else:" %>
<%= ff.text_field :something_else %>
<% end %>
<%= f.submit %>
<% end %>
#in your controller
if #business.create(business_params)
#...
end
if #business.payroll.create(payroll_params)
#...
end
Hope it will help you !

File uploading Mailboxer Gem

I'm trying to send a message with an attachment in Mailboxer Gem.
My stack is : Rails 4 and Ruby 2.1.1
However, I can see that attachment using CarrierWave is already supported as the link in the code below.
https://github.com/ging/mailboxer/blob/4b2681c1790b823f7b493fb00b41e9899bb90ebe/app/models/message.rb#L13
However, I did my setup exactly like that. Normal message without an attachment is going fine.
This is my code :
Controller :
def create_message
if params[:user].present? & params[:message].present? & params[:subject].present?
current_user.send_message(User.find(params[:user]), params[:message], params[:subject])
redirect_to inbox_path
end
end
This is my view code :
<%= form_tag do %>
<%= select_tag 'user', options_from_collection_for_select(User.all, :id, :fullname) %><br/>
<%= text_field_tag 'subject' %><br/>
<%= text_area_tag 'message' %><br/>
<%= submit_tag 'Send' %>
<% end %>
The above code is working fine and the messages are getting sent, however, when I try to add the file field to it like so and try changing the controller code, the attachment is not getting uploaded :
def create_message
if params[:user].present? & params[:message].present? & params[:subject].present?
current_user.send_message(User.find(params[:user]), params[:message], params[:subject], true , params[:attachment])
redirect_to inbox_path
end
end
View :
<%= form_tag do %>
<%= select_tag 'user', options_from_collection_for_select(User.all, :id, :fullname) %><br/>
<%= text_field_tag 'subject' %><br/>
<%= text_area_tag 'message' %><br/>
<%= file_field_tag 'attachment' %>
<%= submit_tag 'Send' %>
<% end %>
I think this is a problem with the strong params. In Rails 3 I could have used attr_accessible. However how do I ensure that the attachment field is not being blocked and allowed?
P.S - I have the carrierwave gem installed and I have even restarted my server multiple times.
Thanks.
I am changing my previous answer, because in fact it wasn't correct.
Basically, if your problem is like mine, it could easily be solved by adding :multipart => true to your form. At least in my case, that's why carrierwave was not picking up the attachments.
I tried to solve the problem by extending the mailboxer Message class and setting up and mounting a completely new carrierwave object with a different attribute name. Basically, this allowed me to avoid working with the attachment attribute defined in mailboxer and to customize the attachments.
But this got very messy with the extension of the mailboxer message.rb Class. So I finally abandoned that course. Still, having your own uploader instead of relying on the mailboxer attachment is very convenient, especially if you want or need to upload your files to a different directory or to the cloud.
So finally, I have created a new model for my attachments and mounted a new carrierwave uploader on it. In that way, I can customize it as I want without having to tweak mailboxer, which has very little in terms of documentation or support.
Probably this is not useful to you anymore, but might help others!

Uploading multiple images to S3 in nested Rails form

I'm trying to get file uploading to work with a nested fields_for tag in a Rails 4 app. I've followed several Railscasts, namely: 253, 381, 383, but still can't quite get it fully functioning. Also using Carrierwave & jquery file upload.
Basic app structure is as follows:
blogpost.rb
class Blogpost < ActiveRecord::Base
has_many :blogpics
end
blogpic.rb
class Blogpic < ActiveRecord::Base
belongs_to :blogpost
end
blogposts_controller.rb
def new
#blogpost = Blogpost.new
blogpic = #blogpost.blogpics.build
end
blogpost_form.html.erb
<div>
<%= form_for #blogpost do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :post_id %>
<%= f.text_field :title %>
<%= f.text_field :location %>
<%= f.text_area :content %>
<%= f.fields_for :blogpics do |builder| %>
<%= builder.file_field :image %>
<%= builder.hidden_field :blogpost_id %>
<% end %>
<p><%= f.submit %></p>
<% end %>
Uploading a single file works. But, adding ":multiple => true, :name => 'blogpic[image]'" to the file field breaks functionality and no files upload.
When I edit blogposts_controller.rb as such:
def new
#blogpost = Blogpost.new
3.times do
blogpic = #blogpost.blogpics.build
end
end
I am able to input three files individually, then upload successfully. Is there any way I can achieve this functionality while being able to drag & drop multiple files into one input?
I really appreciate any help and direction, thanks.
Your blogpost model is missing an accepts_nested_attributes association.
class Blogpost < ActiveRecord::Base
has_many :blogpics
accepts_nested_attributes_for :blogpics
end
I'm not quite sure how handling multiple files in one dialog box. I'd imagine you'd be using some javascript to detect that multiple files were selected, and creating field forms for each of them.
You can pass :multiple => true as a param on builder.file_field :image. See http://apidock.com/rails/ActionView/Helpers/FormTagHelper/file_field_tag for details
With the multiple attribute on the file input, you can drag and drop ONTO the input element
See for details http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-selecting-files
I've run into the same issue (where multiple: true breaks the nested form) and my understanding is that you have to manually transform the params before the controller receives it. If you inspect (using pry or debugger) the params hash, you need to compare between submitting Parent Model with several input files (on individual inputs) VERSUS Parent Model with multiple input files (in one input). The former creates an array of child objects (each with their own file), while the latter creates only one child object that has all the images in one array.

What all models/controllers are needed for building a counter

I want to create a counter example where in the variable increments when ever the user presses a button on the webpage(view)
I have created the ruby program for the counter like this:
def counter(x = 0)
x+=1
puts "The current value is #{x}"
end
I have saved this as counter.rb. Then for the view I created another file called counter.erb But I dont know where to call this rb file and should I use <%= %> embedded ruby tags? Totally confused.
Another thing is the method counter's parameter should be linked to the button clicks.
Please guide me
Thanks,
Ok this is basic and you should be knowing this but still, I will tell you. The question is also vague so I will assume that you have a counter which is a number and a name for the counter. This is how you update any column from scratch.So first create
rails generate scaffold counter count:integer name:string
After this step.
def update
#counter = Counter.find(params[:id])
if #counter.update_attributes(params[:counter])
redirect_to #counter, :flash => { :success => "Profile updated." }
end
end
After this create a view in edit.html.erb under views:
<%= form_for(#counter) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :count %><br />
<%= f.text_field :count %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
Go to counter.rb model and do the following:
class Counter < ActiveRecord::Base
attr_accessible :count, :name
end
You dont have to add anything in the routes file because it already has the update resource.
Now open the browser and if you say
localhost:3000/counter/1/update
after creating the first counter value then you can update stuff from there.
This is how you updating can be done in ruby on rails. If you need more explanation regarding your code you should check the api documentation for rails it is a good start. I am a beginner too.
Looks like you are lacking basic understanding of Rails and MVC. I suggest you start with a simple tutorial to get a grasp on how things are working.
http://guides.rubyonrails.org/getting_started.html will get you started with a simple app and also explains everything you need to know at the beginning.
Basically said, you need a CounterController and - if you want to persist your counter - a Counter model. or you could use a cookie.
However, I advise you to read the guide I posted above.

Resources