Ruby on Rails 4
I have a form to edit a record. The form is identical to the create new record. (Except for the wording). When I click submit it gives, nilLNilClass. My code works for my updating my other records that don't involve a Join Table so I am not sure what is wrong.
The Form:
<%= form_for(#test) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :type %><br>
<%= f.text_field :type %>
</div>
<div class="field">
<%= f.label :category %><br>
<%= f.select :category, [ ["IP Voice Telephony", "ip_voice"], ["IP Video Surveillance", "ip_video_surveillance"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"] ], {prompt: "Select Category"}, class: "input-lg", :id => "category" %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div id="container" style="width:1140px; margin-left: auto; margin-right: auto;">
<button type="button" class="reset">Reset Search</button>
<table width="100%" class="tablesorter">
<thead>
<tr>
<th width="5%" class="filter-false"><input type="checkbox" onClick="toggle(this)"></th>
<th width="37%" data-placeholder="Search">Content</th>
<th width="10%" data-placeholder="Search">Type</th>
<th width="10%" data-placeholder="Search">Category</th>
<th width="10%" data-placeholder="Search">Product</th>
<th width="10%" data-placeholder="Search">User</th>
<th width="8%" data-placeholder="Search">Active</th>
</tr>
</thead>
<tbody>
<% #questions.each do |q| %>
<tr>
<td><%= check_box_tag "test[question_ids][]", q.id, #test.question_ids.include?(q.id) %></td>
<td><%= q.content %></td>
<td><%= q.question_type %></td>
<td><%= q.category %></td>
<td>GXP</td>
<td><%= q.user_id %></td>
<td><%= q.active %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
</div>
<div class="actions">
<%= f.submit "Update Test", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
Controller:
before_action :set_test, only: [:show, :edit, :update, :destroy]
before_action :signed_in_user, only: [:index, :edit, :show, :update, :destroy]
def edit
#test = Test.find(params[:id])
#questions = Question.all
end
def update
respond_to do |format|
if #test.update(test_params)
format.html { redirect_to #test, notice: 'Test was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #test.errors, status: :unprocessable_entity }
end
end
end
def test_params
params.require(:test).permit(:name, :user_id, :type, :category, :description, :question_ids => [], questions_attributes: [ :id ] ).
merge user_id: current_user.id
end
Error:
Started PATCH "/tests/98" for 127.0.0.1 at 2014-05-20 10:12:58 -0400
Processing by TestsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x=", "test"=>{"name"=>"one two three four, spelling YEah!!", "type"=>"", "category"=>"ip_voice", "description"=>"not so smart, wooops", "question_ids"=>["1"]}, "commit"=>"Update Test", "id"=>"98"}
Test Load (0.3ms) SELECT "tests".* FROM "tests" WHERE "tests"."id" = ? LIMIT 1 [["id", "98"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'x' LIMIT 1
Completed 500 Internal Server Error in 6ms
NoMethodError (undefined method `update' for nil:NilClass):
app/controllers/tests_controller.rb:56:in `block in update'
app/controllers/tests_controller.rb:55:in `update'
This is line 56: if #test.update(test_params)
I did think it was wierd that I had to define #test in edit(). I also tried #test.save with same error. I am misunderstanding something.
You need to define #test in update action. Currently its not defined so its value is nil and you receive the error.
Add this line in update action, before calling #test.update:
#test = Test.find(params[:id])
I see that you have a before_action callback for setting test i.e., set_test. In that case, you don't need
#test = Test.find(params[:id])
in update action. All you need to do is add update action in before_action callback as:
before_action :set_test, only: [:show, :edit, :update, :destroy]
Also, I would suggest you to remove the #test setting from edit action as you have already added edit action in before_action callback
Finally, set_test (ideally private method) method should look like:
def set_test
#test = Test.find(params[:id])
end
You need to do like this in your controller
before_action :set_test, only: [:show,:edit,:update,:destroy] and at the end of your controller
private
def set_test
#test = Test.find(params[:id])
end
And also,update your edit method like this
def edit
##test = Test.find(params[:id]) remove this line you wont need it.
#questions = Question.all
end
OR
You can just follow #kirti Thorat's answer.
Related
What I Want:
I need in a view a button or a link (it doesn't matter) to the create action of Reservation controller and to give it a parameter too.
And resolve the ForbiddenAttributesError that now gives me.
Here are my model and controller:
Reservation model
class Reservation < ActiveRecord::Base
belongs_to :user
belongs_to :dinner
end
Reservation controller
class ReservationsController < ApplicationController
load_and_authorize_resource
def show
#reservations = Reservation.joins(:user).where('dinner_id' => params[:dinner_id]).select("users.*,reservations.*")
#dinnerid = params[:dinner_id]
respond_to do |format|
format.html
format.json { render :json => #reservations }
end
end
def create
#reservation = Reservation.new(params[:reservation])
#reservation.user_id = current_user.id
respond_to do |format|
if #reservation.save
format.html { redirect_to #reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: #reservation }
else
format.html { render :show }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:dinner_id)
end
end
EDIT: After the suggestion of #Rahul Singh this is my actual code with relative error:
<p id="notice"><%= notice %></p>
<table>
<thead>
<tr>
<th>Id</th>
<th>User id</th>
<th>Dinner id</th>
<th>User email</th>
<th>User name</th>
</tr>
</thead>
<tbody>
<% #reservations.each do |reservation| %>
<tr>
<td><%= reservation.id %></td>
<td><%= reservation.user_id %></td>
<td><%= reservation.dinner_id %></td>
<td><%= reservation.user.email %></td>
<td><%= reservation.user.name %></td>
</tr>
<% end %>
</tbody>
</table>
<br/>TRY 00a <br/>
<%= form_for(Reservation.new) do |f| %>
<%= f.hidden_field( :dinner_id, :value => #dinnerid.to_s) %>
<%= f.submit "Join1" %>
<% end %>
<br/> !!!!!!!!!!ERROR : ActiveModel::ForbiddenAttributesError
<br/>TRY 00b <br/>
<%= link_to "Join1", reservations_path(dinner_id:#dinnerid.to_s), method: :post %>
<br/> !!!!!!!!!!ERROR : param is missing or the value is empty: reservation
I provide a sreenshot for the error :
Error of the form : https://www.dropbox.com/s/i2x1m520ptqdj56/createReservationForm.jpg
Error of the link_to : https://www.dropbox.com/s/8xjwee5oo7q6uhk/createReservationLink_to.jpg
This should work
<%= form_for(Reservation.new) do |f| %>
<%= f.hidden_field( :dinner_id, :value => #dinnerid.to_s) %>
<%= f.submit "Join1" %>
<% end %>
clicking on Join1 button will submit form to ReservationsController create action.
and with link try this
<%= link_to "Join1", reservations_path(dinner_id:#dinnerid.to_s), method: :post %>
for above to work,add following in your routes.rb
resources :reservations
Change this line -> #reservation = Reservation.new(params[:reservation])
To this -> #reservation = Reservation.new reservation_params
and try again ;).
I'm wondering if it wouldn't be better to have your reservation routes as a nested resource of dinners.
It seems reservations can't exist without a dinner, so I'd make that explicit like this:
# config/routes.rb
resources :dinners do
resources :reservations
end
Run rake routes to see how this would change the routes.
You'd now have the dinner id passed along:
# app/views/dinners/show.html.erb
<%= button_to 'Reserve this dinner', dinner_reservations_path(#dinner) %>
The button would route to the create action because a button's default HTTP method is POST.
# app/controllers/reservations_controller.rb
class ReservationsController < ApplicationController
before_action :set_dinner
def create
#dinner.reservations.create! user: current_user
# would render reservations/create.html.erb
end
private
def set_dinner
#dinner = Dinner.find(params[:id])
end
end
This doesn't fix your immediate problem of just getting that link to work. But I think you'd be a lot better served structuring your app more like the above going forward.
Full disclosure: the person who asked this question contacted me on twitter personally, so I took some liberties in answering this question with a more general design suggestion.
I'm not sure this is the "best" approach but I think its the easiest one.
You could do something with string interpolation:
a href="/reservations?dinner=#{dinner.id}" Join
then you could get the paramter with
params[:dinner]
I am making a simple app where a user can create a Series and then an episode to the series, and then multiple links to each episode. I tried using the gem Cocoon but I wasn't able to get it to show up on the view.
I thought I had set everything right, but I hope someone can help me find what I am doing wrong or missing, thanks!
I am getting this error:
param is missing or the value is empty: series
And in the console:
Processing by SeriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KAF06O/2C3EBRwos7UnJGSzWF2SGVVB7YdrNnuWt0M=", "commit"=>"Update Series", "id"=>"2"}
Series Load (0.2ms) SELECT "series".* FROM "series" WHERE "series"."id" = ? LIMIT 1 [["id", "2"]]
Completed 400 Bad Request in 39ms
ActionController::ParameterMissing (param is missing or the value is empty: series):
app/controllers/series_controller.rb:64:in `series_params'
app/controllers/series_controller.rb:35:in `block in update'
app/controllers/series_controller.rb:34:in `update'
These are what my models look like:
# app/models/series.rb
class Series < ActiveRecord::Base
has_many :episodes
accepts_nested_attributes_for :episodes
end
# app/models/episode.rb
class Episode < ActiveRecord::Base
belongs_to :series
has_many :links
accepts_nested_attributes_for :links
end
# app/models/link.rb
class Link < ActiveRecord::Base
belongs_to :episode
end
My controller:
class SeriesController < ApplicationController
before_action :set_series, only: [:show, :edit, :update, :destroy, :links]
def new
#series = Series.new
#series.episodes.build
end
def update
respond_to do |format|
if #series.update(series_params)
format.html { redirect_to #series, notice: 'Series was successfully updated.' }
format.json { render :show, status: :ok, location: #series }
else
format.html { render :edit }
format.json { render json: #series.errors, status: :unprocessable_entity }
end
end
end
# ... ignoring content that hasn't changed from scaffold
def links
#episodes = #series.episodes
end
private
def series_params
params.require(:series).permit(:name,
episodes_attributes: [:id, :title,
links_attributes: [:id, :url]
])
end
end
The views files:
<!-- app/views/series/links.html.erb -->
<h1><%= #series.name %></h1>
<%= form_for(#series) do |f| %>
<table>
<thead>
<tr>
<td>Title</td>
<td>Season</td>
<td>Episode</td>
</tr>
</thead>
<tbody>
<% #episodes.each do |episode| %>
<tr>
<td><%= episode.title %></td>
<td><%= episode.season %></td>
<td><%= episode.episode %></td>
<td>
<%= f.fields_for :episodes, episode.build do |e| %>
<%= e.fields_for :links, episode.link.build do |a| %>
<%= a.text_area :url %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And the routes file:
MyApp::Application.routes.draw do
resources :series do
member do
get 'links'
end
end
end
I think series parameter is not passing from your view
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KAF06O/2C3EBRwos7UnJGSzWF2SGVVB7YdrNnuWt0M=", "commit"=>"Update Series", "id"=>"2"}
in your action you are using series_params method in that you are using like params.require(:series).permit .
so it's throwing param is missing or the value is empty: series
please check in you view file you passing the view or not
You need to build your links too:
#app/controllers/series_controller.rb
def new
#series = Series.new
#series.episodes.build.links.build #-> creates one link object you can populate
end
Then you need to change your form to use the built link objects:
<%= f.fields_for :episodes do |e| %>
<%= e.fields_for :links do |a| %>
<%= a.text_area :url %>
<% end %>
<% end %>
This should get the new & create actions working. However, you posted the log for the update action -- are you having issues with create or update?
So I have this database where a user can borrow a book. We have books, users and people who borrow.
The code can be found here: https://gist.github.com/Veske/8490542
I am wondering, how can I make it so that I display all the books in borrows view, and then also be able to select thoes books for borrowing for my self or another user?
If I try to make a book instance variable in borrow controller for the view, hell gets loose. So I really have no idea now..
Edit: the #book in the view is not needed anymore as it did not work, i had a action in controller for it before.
This is my controller:
class BurrowsController < ApplicationController
before_action :signed_in_user, only: [:index,:edit,:update, :destroy]
before_action :admin_user, only: :destroy
def index
#burrow = current_user.burrows.build
#burrows = Burrow.all
end
def show
#burrow = Burrow.find(params[:id])
end
def new
#burrow = current_user.burrows.build
end
def create
#burrow = current_user.burrows.build(burrow_params)
if #burrow.save
flash[:success] = "Burrowing a book was successful!"
redirect_to #burrow
else
render current_user
end
end
# Private section, makes the page unable to be seen for non logged in users
private
def burrow_params
params.require(:burrow).permit(:user_id, :book_id)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
# Redirecting not logged in user etc.
def signed_in_user
unless signed_in?
store_location
redirect_to '/sessions/new', notice: "Please sign in!"
end
end
end
And this is my view for creating a new borrow entry:
<% provide(:title, "Burrow a book") %>
<b align="center">Choose the name of a book you want to burrow and enter 'Submit!'</b>
<%= form_for(#burrow) do |f| %>
<div class="forms">
<%= f.text_field :book_id, placeholder: "Type in the name of the book...", autofocus: true %>
<%= f.submit 'Submit!' %>
</div>
<% end %>
The view is bad currently, i am experimenting with absolutly everything all the time right now and I just don't understand what needs to be done.
Borrows index:
<% provide(:title, 'All burrowers') %>
<h2 align="center">All borrowers</h2><
<table align="center">
<tr>
<td align="left"><b>Who borrowed</b></td>
<td align="left"><b>Borrowed what</b></td>
<% if current_user.admin? && !current_user?(#user) %>
<td align="left"><b>Admin functions</b></td>
<% end %>
</tr>
<% #burrows.each do |burrow| %>
<tr>
<td align="left"><%= link_to burrow.user.name, burrow.user %></td>
<td align="left"><%= link_to burrow.book.name, burrow.book %></td>
<% if current_user.admin? && !current_user?(#user) %>
<td>
<%= link_to "Delete this user", burrow, method: :delete, data: { confirm: "You sure?" } %>
</td>
<% end %>
</tr>
<% end %>
</table>
One possible solution for this would be to use a select_tag list like this:
<%= form_for(#burrow) do |f| %>
<div class="forms">
<%= f.select("book_id", Book.all.collect {|b| [ b.name, b.id ] }, { include_blank: true }) %>
<%= f.submit 'Submit!' %>
</div>
<% end %>
Is that what you were looking for?
BTW - I think you mean 'borrow' rather than 'burrow'
I am a rails beginner and I have created 3 models/controllers/views using rails generate scaffold:
Subjects, which have many topics
Topics, which have many notes
Notes
When I go to http://localhost:3000/subjects/1/topics, Rails lists the empty list of topics and when the 'New Topic' link is clicked, you are taken to http://localhost:3000/topics/new.
Should and how do I get the link for 'New Topic' to take the user to http://localhost:3000/subjects/:id/topics/new instead of http://localhost:3000/topics/new and should the new topic form submit to http://localhost:3000/subjects/:id/topics/new instead of http://localhost:3000/topics?
views/topics/index:
<h1>Listing topics</h1>
<table>
<tr>
<th>Name</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #topics.each do |topic| %>
<tr>
<td><%= topic.name %></td>
<td><%= link_to 'Show', topic %></td>
<td><%= link_to 'Edit', edit_topic_path(topic) %></td>
<td><%= link_to 'Destroy', topic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Topic', new_topic_path %>
controllers/topics:
def new
#topic = Topic.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #topic }
end
end
def edit
#topic = Topic.find(params[:id])
end
def create
#topic = Topic.new(params[:topic])
#topic.subject_id = params[:project_id]
respond_to do |format|
if #topic.save
format.html { redirect_to subject_path(#topic.subject_id), notice: 'Topic was successfully created.' }
format.json { render json: #topic, status: :created, location: #topic }
else
format.html { render action: "new" }
format.json { render json: #topic.errors, status: :unprocessable_entity }
end
end
end
new topics form:
<%= form_for(#topic) do |f| %>
<% if #topic.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#topic.errors.count, "error") %> prohibited this topic from being saved:</h2>
<ul>
<% #topic.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes:
resources :subjects do
resources :topics do
resources :notes
end
end
resources :notes
resources :topics
resources :subjects
root :to => 'subjects#index'
when you go to http://localhost:3000/subjects/1/topics the action index of controller TopicsController is called with the params[:subject_id] set to 1.
So in your controller action, you have to check for this parameter and filter the topics if it is given
def index
if params[:subject_id].present?
#subject=Subject.find params[:subject_id]
#topics=#subject.topics
else
#topics=Topic.all
end
end
and in your index view, you have to use this url, if #subject ist present:
<%= link_to 'New Topic', #subject.present? ? new_subject_topic_path(#subject) : new_topic_path %>
in your topics new action again you get the :subject_id as a parameter:
def new
#subject=Subject.find params[:subject_id]
#topic = #subject.topics.new
end
then in your topics form, you can forward the subject_id in a hidden field:
<%= form_for(#topic) do |f| %>
...
<%= f.hidden_field :subject_id %>
...
<% end %>
The rest of your TobicsController can stay the same. The Topic is assiciated to the Subject by the subject_id
Once you have declared the nested resources, don't declare those resources again.
Deep nesting of resources is not recommended. You should think about doing shallow nesting, and you can read about this in the Rails Routing Guide.
One way to avoid deep nesting (as recommended above) is to generate
the collection actions scoped under the parent, so as to get a sense
of the hierarchy, but to not nest the member actions. In other words,
to only build routes with the minimal amount of information to
uniquely identify the resource, like this:
resources :posts do
resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
This idea strikes a balance between descriptive routes and deep
nesting.
I am very new to Rails and Web-programming and hope you can help me out with my very first project.
I am developing a website for a Real Estate Agency.
I have 3 tables in my database (Homes: Home_ID, Home_Name, Admin_ID; Admins: Admin_ID, Admin_Name, Admin_Email; Images: Image_ID, Image_Path, Image_Name, Home_ID, Admin_ID).
All 3 tables have been created using scaffold. The image information (name, path, image_id, home_id etc) have been entered in SQLite.
I get all text information of the different houses displayed correctly on the website except the pictures.
My attempt to link it in the view/home/index.html.erb created the following error:
undefined method `image_path' for #<Home:0xb63d85e0>
I used below code:
<% #homes.each do |home| %>
<tr>
<td><%= home.name %></td>
<td><%= home.details %></td>
<td><%= home.region %></td>
<td><%= home.address %></td>
<td><%= home.price %></td>
<td><%= home.admin_id %></td>
<td><%= home.image_path %></td>
</tr>
<% end %>
</table>
It looks like that the data entered in SQLite do not sync with rails.
Do you have any idea what I have done wrong and how I can fix it?
Thank you.
I'm not positive what the relationship would be between your images and home models would be so correct me if I'm wrong, but I'm guessing that homes will have many images. Each image would belong to one home. Is this correct? If so, you will need to declare that in your models like so:
models/home.rb
has_many :images
models/image.rb
belongs_to :home
You will then need to add this to the image database:
t.integer "home_id"
You can add it by going to the command line and typing:
rails g migration AddHomeToImages home_id:integer
You should look in db/migrate/ and then the most recent migration file and make sure it looks like this:
add_column :images, :home_id, :integer
Then run:
rake db:migrate
At this point you'll only need to update your controller and views to show this association. Let me know if this helps and if so I'll help you with your controller and views.
What's happening is that you are looping through a selection of records from the Home table. Because of this, when you call
<td><%= home.image_path %></td>
it's not recognizing the attribute image_path because you don't have image_path as a column of Home table. You only have Home_ID, Home_Name, Admin_ID for your columns. You will have to look into associations between models in order to figure out how to grab the image_path for each home record. You can start here.
If you update the code later on I'll be glad to comment on it.
I think that the best solution here will be to use paperclip gem.
You can take a look at this very old railscast eoisode just to understand how it works:
http://railscasts.com/episodes/134-paperclip
And here is github repository:
https://github.com/thoughtbot/paperclip
Paperclip will help you with paths, styles and preview for your images.
Thanks Jason,
I agree that it is a bit big for newbies, but the requirements were that we do a website where we need to read and write from/to a database. We had a quick introduction to rails with partnerwork and are now on our own and a "bit" lost and running out of time.
Here is the error message I am getting:
NameError in Homes#index
undefined local variable or method `image' for
<#:0xb62f7aa4>
Extracted source (around line #20):
17: <% #homes.each do |home| %>
18: <tr>
19: <td><%= home.name %></td>
20: <td><%= image.home_id %></td>
21: <td><%= home.details %></td>
22: <td><%= home.region %></td>
23: <td><%= home.address %></td>
When I create the database tables I had an Image_ID in the homes table, but I was told that I don`t need it and that it is enough to have only Home_ID in the images table.
I understand that the error is caused because of image.home_id.
What is your opinion? Should I add the Image_ID back to the homes table in order to display all images for the respective home_id or is there another way? I would like to be able to decide which picture will be displayed as the main picture and which ones as the smaller pictures.
Here is the code I use:
models/home.rb
class Home < ActiveRecord::Base
attr_accessible :address, :admin_id, :details, :name, :price, :region
has_many :images
end
models/image.rb
class Image < ActiveRecord::Base
attr_accessible :image_description, :image_name, :image_path
belongs_to :home
end
views/homes/index.html.erb
<h1>Listing homes</h1>
<table>
<tr>
<th>Name</th>
<th>Image</th>
<th>Details</th>
<th>Region</th>
<th>Address</th>
<th>Price</th>
<th>Admin</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #homes.each do |home| %>
<tr>
<td><%= home.name %></td>
<td><%= image.home_id %></td>
<td><%= home.details %></td>
<td><%= home.region %></td>
<td><%= home.address %></td>
<td><%= home.price %></td>
<td><%= home.admin_id %></td>
<td><%= link_to 'Show', home %></td>
<td><%= link_to 'Edit', edit_home_path(home) %></td>
<td><%= link_to 'Destroy', home, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Home', new_home_path %>
views/homes/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #home.name %>
</p>
<p>
<b>Image:</b>
<%= #image.home_id %>
</p>
<p>
<b>Details:</b>
<%= #home.details %>
</p>
<p>
<b>Region:</b>
<%= #home.region %>
</p>
<p>
<b>Address:</b>
<%= #home.address %>
</p>
<p>
<b>Price:</b>
<%= #home.price %>
</p>
<p>
<b>Admin:</b>
<%= #home.admin_id %>
</p>
<%= link_to 'Edit', edit_home_path(#home) %> |
<%= link_to 'Back', homes_path %>
views/images/_form.html.erb
<%= form_for(#image) do |f| %>
<% if #image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#image.errors.count, "error") %> prohibited this image from being saved:</h2>
<ul>
<% #image.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :image_name %><br />
<%= f.text_field :image_name %>
</div>
<div class="field">
<%= f.label :image_path %><br />
<%= f.text_field :image_path %>
</div>
<div class="field">
<%= f.label :image_description %><br />
<%= f.text_area :image_description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
controllers/images_controller.rb
class ImagesController < ApplicationController
# GET /images
# GET /images.json
def index
#images = Image.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #images }
end
end
# GET /images/1
# GET /images/1.json
def show
#image = Image.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #image }
end
end
# GET /images/new
# GET /images/new.json
def new
#image = Image.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #image }
end
end
# GET /images/1/edit
def edit
#image = Image.find(params[:id])
end
# POST /images
# POST /images.json
def create
#image = Image.new(params[:image])
respond_to do |format|
if #image.save
format.html { redirect_to #image, notice: 'Image was successfully created.' }
format.json { render json: #image, status: :created, location: #image }
else
format.html { render action: "new" }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
# PUT /images/1
# PUT /images/1.json
def update
#image = Image.find(params[:id])
respond_to do |format|
if #image.update_attributes(params[:image])
format.html { redirect_to #image, notice: 'Image was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
#image = Image.find(params[:id])
#image.destroy
respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end
end
Thank you so much. I really appreciate your help!!!