class ItemController < ApplicationController
def create
item = current_user.items.build(params[:presentstem])
item.created_at = Time.now
item.save!
redirect_to root_path
end
def destroy
end
end
And my form in views/home/index/html.erb to add an item
<div id="add_item">
<p>Add a new item</p>
<% form_for Item.new do |f| %>
<div id="add_item_container">
<%= f.text_field :present %>
<%= f.text_field :stem %>
<%= f.text_field :secondary %>
<%= f.check_box :atype %>
<%= f.text_field :comment %>
</div>
<%= f.submit "Add to List" %>
<% end %>
</div>
How do I define Item?
at localhost:3000 I get
Expected /Users/user/Desktop/test/app/models/item.rb to define Item
Extracted source (around line #3):
You should have Item class definition in this file /Users/user/Desktop/test/app/models/item.rb, probably you don't...
class Item < ActiveRecord::Base
#class definition goes here
end
You are thinking wrong.
You have an model in app/models/item.rb
for this you have an controller in app/controllers/items_controller.rb
and you have views in app/views/items/template.haml
if you want to do a form_for you do the form for a object. rails is looking what type of action you want to participate (new, update) and generates automatically the route (restful).
so you just gave an object to the form_for helper
#in view
=form_for Item.new do |f|
or
#in items_controller.rb
def new
#item = Item.new
end
#in new.haml
=form_for #item
Related
I have no idea why it is showing me this problem NO Method in Author.
Showing /home/muba/muba/app/views/author/new.html.erb where line #3 raised:
<%= form_for #author do |f| %>
<div class="form-group">
<%= f.label :first_name %><br/>
<%= f.text_field :first_name, class:'form-control' %><br/>
</div>
<% end %>
Author.rb Controller
class AuthorController < ApplicationController
def new
#page_title= 'Add new Author'
#author = Author.new
end
def create
end
def update
end
def edit
end
def destroy
end
def index
end
def show
end
def author_params
params.require(:author).permit(:first_name, :last_name)
end
end
But Same Thing I did for my Categories page that is working. The code is:
<%= form_for #category do |f| %>
<div class="form-group">
<%= f.label :name %><br/>
<%= f.text_field :name, class:'form-control' %><br/>
</div>
<%=f.submit "Submit", class:'btn btn-primary' %>
<%= link_to "Cancel", categories_path, class:'btn btn-default' %>
<% end %>
Category.rb controller
class CategoriesController < ApplicationController
def new
#page_title= 'Add new Category'
#category= Category.new
end
def create
end
def update
end
def edit
end
def destroy
end
def index
end
def show
end
def category_params
params.require(:category).permit(:name)
end
end
I noticed there is issue in your first line it should be
class AuthorsController < ApplicationController
instead of
class AuthorController < ApplicationController
also make sure your controller's file name is authors_controller.rb
Ruby on Rails follow linguistic convention. Check Ruby and Rails Naming Conventions
In your view :
<%= form_for #author do |f| %>
<div class="form-group">
<%= f.label :first_name %><br/>
<%= f.text_field :first_name, class:'form-control' %><br/>
</div>
<% end %>
here , make sure the first_name is present as a column name in your Author model in the table in the database . It will check the exact name in the table which will raise an error if the name doesn't match .
I hope this helps.
I'm trying to create a reviews model for company pages. For this I have:
Models
user.rb
has_many :reviews
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :reviews
end
My reviews controller is:
def create
#company = Company.find_by_slug(params[:id])
#review = #company.reviews.create(params[:review])
#review.save
redirect_to company_path(#company)
end
and I have this code in the company show page:
<% #company.reviews.each do |review| %>
<p>
<strong>Title:</strong>
<%= review.title %>
</p>
<p>
<strong>Avantage:</strong>
<%= review.avantage %>
</p>
<p>
<strong>Inconvenient:</strong>
<%= review.inconvenient %>
</p>
<% end %>
</br>
<%= form_for([#company, #company.reviews.build]) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :avantage %><br>
<%= f.text_area :avantage %>
</div>
<div class="field">
<%= f.label :inconvenient %><br>
<%= f.text_area :inconvenient %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
However, when I go to a specific company page and try to create a review for this company I'm getting this error message undefined method reviewsfor nil:NilClass
Instead of #company = Company.find_by_slug(params[:id]) use this code #company = Company.friendly.find(params[:company_id])
There are a couple of things you may find useful:
If you're using Rails 4, you may encounter a further problem. In the third line of your create method, you are using unsecure params directly in a .create call. Check out the Rails Guide page on "strong params".
If you implement strong parameters as mentioned above, you should probably deliberately omit the company_id field from the list of permitted params.
Assuming your users are allowed to write a review for any company in your system, it might be simpler for you to embed the company_id as a hidden field in your form. This would allow you to also simplify the controller method. For example:
# _form.html.erb
<%= form_for(#review) do |f| %>
<%= f.hidden_field :company_id, value: #company.id %>
...bla bla bla
<% end %>
Then, in your reviews_controller...
# reviews_controller.rb
def create
#review = Review.new(approved_params)
if #review.save
flash[:success] = 'Review created!'
else
flash[:error] = "Review wasn't saved"
end
#company = #review.company
redirect_to #company
end
def approved_params
params.require(:review).permit(:title, :avantage, :inconvenient, :company_id)
end
In your companies_controller, you should add this to your show method
# companies_controller.rb
def show
#company = Company.find(params[:id]
# add this line below...
#review = Review.new
end
I hope this helps.
I have albums and pictures where albums hasmany pictures
This is my routes.rb
resources :albums do
resources :photos
end
Instead of photos_path my path is album_photos_path
In my photos/new.html.erb I'm getting this error:
undefined method photos_path' for #<#<Class:0x5232b40>:0x3cd55a0>
How I can do to instead of photos_path simple form write album_photos_path ?
My new.html.erb
<%= simple_form_for([#album, #photo]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
You can specify url in your form. Like this:
<%= simple_form_for #photo, url: album_photos_path do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
But your code should also work. In your new action did you initialize both #album and #photo, something like:
def new
#album = Album.find params[:album_id]
#photo = #album.pictures.build
end
P.S above code is just a sample code and if both variables(#album and #photo) are initialized properly then rails will automatically generate correct url.
You need to set the parent resource.
class PhotosController < ApplicationController
before_action :set_album
# GET /albums/1/photos/new
def new
#photo = #album.photos.new
end
# POST /albums/1/photos
def create
#photo = #album.photos.new(photo_params)
# ...
end
private
# ...
def set_album
#album = Album.find(params[:album_id])
end
end
The reason rails is trying to call photos_path is that the polymorphic route helpers which turn an array of models into a route helper method compact the array. url_for([nil, Photo.new]) will result in the same result as url_for(Photo.new) - photos_path.
I can't get along with saving Students with one POST when i"m saving Project.
My Projects controller looks like:
class ProjectsController < ApplicationController
def index
#projects = Project.all
end
def new
#project = Project.new
3.times do
student = #project.students.build
end
end
def create
#project = Project.new(project_params)
#project.status = "Waiting"
# I'm not sure about these lines
#project.students.each do |student|
student = Student.new(params[:name])
end
#project.save!
redirect_to projects_path
end
private
def project_params
params.require(:project).permit(:name, :lecturer)
end
end
And a new_project view looks like:
<h1>Creating new project...</h1>
<%= form_for #project, url: projects_path do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :lecturer %>
<%= f.text_field :lecturer %>
</p>
<p>
<%= f.fields_for :students do |s| %>
<%= s.label :name %>
<%= s.text_field :name %>
<% end %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
And my question is how to save Project and Students (assigned to it) using one form?
First, your project_params method isn't allowing the students' names to be submitted. Try something like this:
def project_params
params.require(:project).permit(:name, :lecturer, students_attributes: [ :name ] )
end
Next, in your Project model you'll need the line
accepts_nested_attributes_for :students
(You might have put it there already - but if you didn't, you'll need to.)
Now that that's done, you shouldn't need these lines in your Project#create method:
#project.students.each do |student|
student = Student.new(params[:name])
end
Because your project can now accept nested attributes for students, they should be created automatically with the project when you save it.
I am not sure if I'm structuring my application corretly (I've been learning Rails for 2 months now) but I am building a pretty deeply nested application that looks like this:
user has_many accounts > accounts has_many characters > characters has_many items
So it's 4 levels deep (that's the plan at least).
I'm currently at characters and I'm having trouble creating the form which is throwing up this error: undefined method 'characters' for nil:NilClass (screenshot).
Here's the project on github: https://github.com/imjp/d2shed
characters_controller.rb
class CharactersController < ApplicationController
def create
#user = User.find(params[:user_id])
#account = Account.find(params[:account_id])
#character = #account.characters.create(params[:character])
redirect_to root_url
end
end
character.rb
class Character < ActiveRecord::Base
attr_accessible :name, :type
belongs_to :account
end
_form.html.erb
<%= form_for([#account, #account.characters.build]) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.radio_button(:type, "SC") %>
<%= f.label(:type, "SC") %>
<%= f.radio_button(:type, "HC") %>
<%= f.label(:type, "HC") %>
<%= f.radio_button(:type, "SCL") %>
<%= f.label(:type, "SCL") %>
<%= f.radio_button(:type, "HCL") %>
<%= f.label(:type, "HCL") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You get this error because you don't define #account in the users controller in the show action,
class UsersController < ApplicationController
...
def show
#user = User.find(params[:id])
#account = #user.accounts.first # Otherwise #account == nil
...
end
...
end
Also, the route in your form don't look right.
The create action for the Character resource is like this in the routes:
POST /:user_id/accounts/:account_id/characters
So you need to provide, :user_id, :account_id, and character
like this:
<%= form_for [#user, #account, #account.characters.build] %>