I am new to rails development and still trying to understand the building blocks of it. While running this simple code, I am getting following error
undefined method `home_home_path'
it is coming from this line <%= form_for(#homes) do |f| %> in .html.erb file. Here is my complete code, what I am doing wrong ?
I have Homes Controller file
def index
#homes = Home.all
end
def show
#home = Home.find(params[:id])
end
def new
#home = Home.new
end
def create
#home = Home.new(params[:home])
#home.save
end
Homes.rb model file
class Home < ActiveRecord::Base
attr_accessible :email, :message, :name
end
views/homes/index.html.erb
# this will show all the data
<% #homes.each do |home| %>
<%= home.name %><br />
<%= home.email %> <br />
<%= home.message %><br />
<% end %>
<br />
# this is a form where you will new records
<%= form_for(#homes) do |f| %>
<%= f.text_field :name %>
<%= f.text_field :email %>
<%= f.text_area :message %>
<%= f.submit %>
<% end %>
You should use :
<%= form_for(#home) do |f| %>
(singular)
And if you want to add a form in your index.html , remember to instantiate a new home object in your controller index method :
def index
#homes = Home.all #For displaying all the homes
#home = Home.new #For your form
end
#homes is an array object. I'm not really sure how Rails infers the url from this but running
url_for Home.limit(2).all
will also give you the same error.
The solution is to change #homes to Home.new or declare #home = Home.new in your controller and use #home in the form.
form_for Home.new
or
# controller
def index
#homes = Home.all
#home = Home.new
end
# view
form_for #home
Related
at the moment I am attempting to create a form for my website that will allow for users to input information and then the information with be POST'ed to my database for storage. I am a new ruby on rails developer so keep that in mind.
I was able to get to the point where the user could see the form and type in information but once they hit the submit button I recieve an error, and that error is
ArgumentError in StudentsController#create
wrong number of arguments (0 for 1) in app/controllers/students_controller.rb:13:in `create'
The parameters that were sent were the following
{"utf8"=>"✓",
"authenticity_token"=>"bLalQ9Ek5ziaGiGHj03AGPCTIABAgcT+o4eTgN44qv44dxNDlrGA0h2u5BSTQVTMh+YgA/mLPQee05lT7mxCsw==",
"student"=>{"first_name"=>"Andrew",
"last_name"=>"Terra"},
"commit"=>"Submit"}
Below is my students_controller.rb file.
class StudentsController < ApplicationController
def index
#students = Student.all
end
def new
#student = Student.new
end
def create
#student = Student.new(params.require[:student])
if #student.save
redirect_to students_path
end
end
def destroy
#student = Student.find_by_id(params[:id])
if #student.destroy
redirect_to students_path
end
end
end
Below is my views/students/_form.html.erb file
<%= form_for #student do |f| %>
<p>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</p>
<%= f.submit "Submit" %>
<% end %>
Below is my /views/students/index.html.erb file
<%= link_to "Create new information", new_student_path %> <br /> <hr>
<% #students.each do |fo| %>
Firstname: <%= fo.first_name %> <br />
Lastname: <%= fo.last_name %> <br />
<%= link_to "Delete info?", student_path(student), :data=>{:confirm=>"Are you sure ?"}, :method=> :delete %>
<br />
<hr>
<% end %>
Finally, here is my /views/students/new.html.erb file
Enter new info
<hr>
<%= render :partial => "form" %>
And I did remember to put resources :students in my routes file. I searched around and found other people who had previously had this problem but none of the solutions worked on the code that I have written. So I was wondering if anyone could point me in the direction of where the bug is and how exactly I can fix it. Thank you.
You need to add a new private method:
private
def student_params
params.require(:student).permit(:first_name, :last_name)
end
And then as said before change your create method to:
def create
#student = Student.new(student_params)
if #student.save
redirect_to students_path
end
end
I recommend reading the documentation on Strong Parameters - to better understand how they work. https://github.com/rails/strong_parameters
You have to change
params.require[:students] to params.require(:students)
But this is still not good way to handle your params for create action, you should add private method student_params to your controller where you would whitelist your params. Like this:
def student_params
params.require(:student).permit(:first_name, :last_name)
end
Here you can find more about it,
http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html
iam new to rails, i have model(welcome) and i want to update the model with single form where the form has duplicate fields .
only second field updates the model ..
Update multiple records in single model with single form_submit
routes
get 'welcome/index'
get 'welcome/new'
post 'welcome/create'
root 'welcome#index'
welcomecontroller
class WelcomeController < ApplicationController
def index
end
def new
#article = Welcome.new
end
def create
#article = Welcome.new(user_params)
if #article.save
redirect_to welcome_new_path
else
render 'new'
end
end
private
def user_params
params.require(:welcome).permit(:name , :descrip )
end
end
welcomeindex view
<%= form_for# article, url: welcome_create_path do |f | %>
<%= f.text_field: name %>
<%= f.text_field: descrip %>
<%= f.text_field: name %>
<%= f.text_field: descrip %>
<%= f.submit "Create" %>
<% end %>
thanks
Only the second set of fields are being updated because you have duplicate names for the fields. Give each field a unique name and it should work.
<%= form_for# article, url: welcome_create_path do |f | %>
<%= f.text_field: name1 %>
<%= f.text_field: descrip1 %>
<%= f.text_field: name2 %>
<%= f.text_field: descrip2 %>
<%= f.submit "Create" %>
<% end %>
You will need to update the controller method user_params accordingly
def user_params
params.require(:welcome).permit(:name1 , :descrip1, :name2 , :descrip2 )
end
I don't know if this is possible, but if a user creates a tag called mission-statement in a valuations form:
<%= f.text_field :name %>
<%= f.text_field :tag_list, valuation: #valuation.tag_list.to_s.titleize %>
how can we make the :name of that valuation appear in the home page :jumbotron:
<% content_for :jumbotron do %>
<h1>Mission</h1>
<p>
# Mission Statement Goes Here
<% #valuation_mission.each do |valuation_mission| %>
<%= valuation_mission.name %>
<% end %>
</p>
<% end %>
I assume we'd have to write a method in the pages_controller like I attempted:
class PagesController < ApplicationController
def home
#user = current_user
if logged_in?
#habits = current_user.habits
#valuations = current_user.valuations
#accomplished_goals = current_user.goals.accomplished
#unaccomplished_goals = current_user.goals.unaccomplished
#averaged_quantifieds = current_user.quantifieds.averaged
#instance_quantifieds = current_user.quantifieds.instance
#valuation_mission = current_user.valuations #We'd need to add .something to make this work?
#feed_items = current_user.feed.paginate(page: params[:page])
end
end
end
I'm using the acts-as-taggable-on gem, which I learned how to implement from here: http://railscasts.com/episodes/382-tagging
Thank you for your time!
You can use this:
current_user.valuations.tagged_with('mission-statement')
Found in the documentation: https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects
I am new to rails and I am just learning the basics.
This is my code on saving data:
app/controllers/employee_controller.rb
class EmployeesController < ApplicationController
def index
render json: #employees = Employee.all
end
def show
render json: #employee = Employee.find(params[:id])
end
def new
#employee = Employee.new
end
def create
#employee = Employee.new(employee_params)
#employee.save
redirect_to #employee
end
private
def employee_params
params.require(:employee).permit(:fname, :mname, :lname, :contactno, :address, :username, :password)
end
end
app/views/employees/new.html.erb
<%= form_for #employee do |f| %>
<p>
<label>First Name</label><br>
<%= f.text_field :fname %>
</p>
<p>
<label>Middle Name</label><br>
<%= f.text_field :mname %>
</p>
<p>
<label>Last Name</label><br>
<%= f.text_field :lname %>
</p>
<p>
<label>Contact No.</label><br>
<%= f.text_field :contactno %>
</p>
<p>
<label>Address</label><br>
<%= f.text_area :address %>
</p>
<br>
<p>
<label>Username</label><br>
<%= f.text_field :username %>
</p>
<p>
<label>Password</label><br>
<%= f.text_field :password %>
</p>
<br>
<p>
<%= f.submit %>
</p>
But, my goal is to save right away without the html form. (NO INPUT) Like when I visit a certain URL and the values are automatically saved in the database.
For a start, I would like to assign a constant value in every field just to see how it works.
Example,
fname='sample name'
mname='sampleMidName'
lname='sampleLastName'
and etc...
How can I assign those values right away after a certain URL/site is visited.
You start by adding a method to your controller
def update_fname
# get the parameters
fname = params[:fname]
# get the employee ID
id = params[:id]
# find the employee
#employee = Employee.find(id)
# update the employee
employee.update_attributes(fname: fname)
redirect_to #employee
end
Then, in your route, you add:
resources :employees do
get 'update_fname'
end
And you call the route, who should be http://localhost:3000/employees/{:id}/update_fname?fname={your_fname}
In your controller try something like:
class EmployeesController < ApplicationController
def custom
#employee = Employee.create(fname: "sample name")
end
end
and define proper route in config/routes.rb:
get "/custom" => "employees#custom"
When you enter proper url in your browser, like:
localhost:3000/custom
The Employee should be saved.
Good luck!
I'm using rails 4.0.8. I added a comment section to a model called 'Things', but I keep getting the same error "param is missing or the value is empty: thing" when I press the submit comment button. It says the error is in the Things#Controller. What am I doing wrong?
UPDATE: I removed the url path from the form, but a new error returns "Couldn't find Thing without an ID". The error is in Comments#Controller.
VIEW FOR THING/SHOW
<div id= "thing">
<h1>
<%= #thing.name %>
</h1>
<br>
<div id= "commentsection">
Comments
<div id= "comments">
<br>
<% #thing.comments.each do |c| %>
<%= c.username %>
<br>
<%= c.text %>
<% end %>
<%= form_for #comment, :url => thing_path do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :comment %>
<%= f.text_field :text %>
<%= f.submit "Enter", class: "btn btn-small btn-primary" %>
<% end %>
</div>
</div>
</div>
THINGS CONTROLLER
class ThingsController < ApplicationController
def show
#thing = Thing.find(params[:id])
#thing.comments.build
#comment = Comment.new
end
def index
end
def new
#thing = Thing.new
#things = Thing.all
end
def create
#thing = Thing.new(thing_params)
if #thing.save
redirect_to #thing
else
render 'new'
end
end
private
def thing_params
params.require(:thing).permit(:name, :avatar)
end
end
COMMENTS CONTROLLER (I put asterisks around the line where the error is)
class CommentsController < ApplicationController
def show
#comment = Comment.find(params[:id])
end
def new
#comment = Comment.new
#comments = Comment.all
end
def create
****#thing = Thing.find(params[:thing_id])****
#comment = #thing.comments.create(comment_params)
redirect_to thing_path(#thing)
end
end
private
def comment_params
params.require(:comment).permit(:user, :text, :upvotes, :downvotes, :thing_id)
end
end
ROUTES
Website::Application.routes.draw do
get "comments/new"
get "comments/show"
get "things/new"
root 'home_page#home'
get "all/things/new" => 'things#new'
get "all/allthings"
resources :things
resources :good_comments
get "things/show"
get "things/results"
end
You are posting the #comment form to post '/things' path.
<%= form_for #comment, :url => thing_path do |f| %>
It should just be <%= form_for #comment do %> (Rails is smart enough to plug in the comments_path) or if you feel like being more explicit (even though it's not necessary)
<%= form_for #comment, url: :comments_path do %>
Another note though, if you want that Comment to be tied to that specific Thing then in your models it should be
Class Thing
has_many :comments
end
Class Comment
belongs_to :thing
end
Then make sure in your database comment has a thing_id foreign_key field and then your form for comment should actually look like
<%= form_for #thing, #comment do %>
<% end %>