Ruby on Rails - Edit not working - ruby-on-rails

Trying to update a post that I've made in my Ruby on Rails project but nothing happens. It is really annoying since I don't get any errors and can't seem to figure out what I am doing wrong.
I think it worked before, but since I first wrote the different actions I've added multiple things to my "feed"-model, such as impressions and tags. Don't know if this has affected my update action...
My controller looks like this:
def edit
#feed = Feed.find(params[:id])
end
def update
#feed = Feed.find(params[:id])
respond_to do |format|
if #feed.update_attributes(params[:feed])
format.html { redirect_to #feed, notice: 'Feed was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #feed.errors, status: :unprocessable_entity }
end
end
end
My feed model looks like this:
attr_accessible :content, :tag_list, :guid, :language, :location, :published_at, :summary, :url, :title, :user_id, :thumbnail_url, :url_to_feed, :type_of_feed
has_many :impressions, :as=>:impressionable
validates_length_of :tag_list, :maximum => 10
acts_as_taggable
My view looks like this:
<h1>Editing feed</h1>
<%= render 'form' %>
<%= link_to 'Show', #feed %> |
<%= link_to 'Back', feeds_path %>
____________________ form
<%= form_for(#feed) do |f| %>
<% if #feed.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#feed.errors.count, "error") %> prohibited this feed from being saved:</h2>
<ul>
<% #feed.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :language %><br />
<%= f.text_field :language %>
</div>
<div class="field">
<%= f.label :tag_list, "Tags (seperated by spaces)" %><br />
<%= f.text_field :tag_list %>
</div>
<div class="actions">
<%= f.submit %>
</div>

The log entry
Started PUT "/feeds/1" for 127.0.0.1 at 2013-02-17 12:27:36 +0100 Processing by FeedsController#show as HTML
says,that the PUT request (sending your form data) is being processed by FeedsController#show, but it has to be processed by FeedsController#update. So your routes seem to be wrong. Check out the Rails Routing Guide.
I would use a feeds ressource, because it creates the correct routes automatically:
resources :feeds

Related

Using validations and pluralize in views to show errors count for forms rails 7

i am trying to display a message for whenever someone does not fill the form completely and press the submit button!
Example: when someone clicks the submit button without filling the Name and Email field in the form, it should display
2 Errors Prohibited from submitting the form!
. Name can't be blank
. Email can't be blank
but it is not being displayed whenever we submit the form with empty fields
can someone explain me the reason why it is not working?
Orders_controller.rb
class OrdersController < ApplicationController
# GET to /orders/new
def new
#order = Order.new
end
# POST to /orders
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save!
format.html{ redirect_to root_url, notice: "Order Succesfully Submitted!" }
else
format.html{ render :new, status: :unprocessable_entity }
end
end
end
private
def order_params
params.require(:order).permit(:first_name, :last_name, :phone_number, :email, :paper_size, :color, :paper_style, :quantity, :description, files: [] )
end
end
_form.html.erb (i already rendered the partial in new.html.erb by <%= render 'form', order: #order%>
<div class="container">
<h1 class="text-center">Order From Home!</h1>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<%= form_with(model: order) do |f| %>
<% if order.errors.any? %>
<div style="color: red">
<h3><%= pluralize(order.errors.count,"errors")%> Prohibited from submitting the form! <br/>
All Fields Are Required!</h3>
<ul>
<% order.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :first_name%>
<%= f.text_field :first_name, class:"form-control" %><br/>
<%= f.label :last_name %>
<%= f.text_field :last_name, class:"form-control" %><br/>
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class:"form-control" %><br/>
<%= f.label :email %>
<%= f.text_field :email, class:"form-control" %><br/>
<%= f.label :files %>
<%= f.file_field :files, multiple: true %><br/>
<%= f.label :paper_size %>
<%= f.select :paper_size, ['A4', 'B4'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :color %>
<%= f.select :color, ['Black & White', 'Color'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :paper_style %>
<%= f.select :paper_style, ['Black to Back', 'Side to Side'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :quantity %>
<%= f.number_field :quantity, class:'form-control' %><br/>
<%= f.label :description %>
<%= f.text_area :description, class:"form-control" %><br/>
<div class="btn-order">
<%= f.submit %>
</div>
<% end %>
</div>
</div>
</div>
#order.save! raises validation errors. You don't need bang method if you want to render such errors, just use #order.save instead
form_with sends XHR request. If you need to render something, you can disable Turbo for this form
<%= form_with(model: order, data: { turbo: false }) do |f| %>

unknown attribute 'development_id' for Lot with cocoon triple nested forms

I'm almost certain I know what the problem is, and if I'm correct, I just can't find where the problem is lying.
When triple-nesting with the cocoon gem, I'm having what seems to be an error with incorrect pluralisation. I have 3 resources, Developments > Lots > Listings, where Developments is the grandparent, Lots is the parent and Listings is the child.
The error I'm getting is unknown attribute 'development_id' for Lot. stemming from ActiveModel::UnknownAttributeError in Developments#new. I've checked my models and partials and have been playing around with them. Here is my code:
developments_controller.rb
class DevelopmentsController < ApplicationController
before_action :set_development, only: %i[ show edit update destroy ]
# GET /developments or /developments.json
def index
#developments = Development.all
end
# GET /developments/1 or /developments/1.json
def show
end
# GET /developments/new
def new
#development = Development.new
end
# GET /developments/1/edit
def edit
end
# POST /developments or /developments.json
def create
#development = Development.new(development_params)
respond_to do |format|
if #development.save
format.html { redirect_to #development, notice: "Development was successfully created." }
format.json { render :show, status: :created, location: #development }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #development.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /developments/1 or /developments/1.json
def update
respond_to do |format|
if #development.update(development_params)
format.html { redirect_to #development, notice: "Development was successfully updated." }
format.json { render :show, status: :ok, location: #development }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #development.errors, status: :unprocessable_entity }
end
end
end
# DELETE /developments/1 or /developments/1.json
def destroy
#development.destroy
respond_to do |format|
format.html { redirect_to developments_url, notice: "Development was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_development
#development = Development.find(params[:id])
end
# Only allow a list of trusted parameters through.
def development_params
params.require(:development).permit(:development_name, :development_type, :development_address, :description, :estimated_completion_date, :body_corp,
lots_attributes: [:id, :status, :stage, :land_size, :price, :eta, :_destroy,
listings_attributes: [:id, :lot_number, :price, :type, :bed, :bath, :car, :house_size, :rent, :_destroy]])
end
end
development.rb (model)
class Development < ApplicationRecord
has_many :lots
accepts_nested_attributes_for :lots, reject_if: :all_blank, allow_destroy: :true
end
Lot.rb (model)
class Lot < ApplicationRecord
has_many :listings
accepts_nested_attributes_for :listings, reject_if: :all_blank, allow_destroy: :true
belongs_to :development
end
Listing.rb (model)
class Listing < ApplicationRecord
belongs_to :lot
end
_form.html.erb (partial)
<%= form_for #development do |f| %>
<div class="field">
<%= f.label :development_name%>
<%= f.text_field :development_name%>
</div>
<div class="field">
<%= f.label :development_type %>
<%= f.text_field :development_type %>
</div>
<div class="field">
<%= f.label :development_address %>
<%= f.text_field :development_address %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :estimated_completion_date %>
<%= f.date_select :estimated_completion_date %>
</div>
<div class="field">
<%= f.label :body_corp %>
<%= f.number_field :body_corp %>
</div>
<div id="listings">
<%= f.fields_for :lots do |lot| %>
<%= render 'lot_fields', f: lot %>
<% end %>
<div class="links">
<%= link_to_add_association 'add lot', f, :lots %>
</div>
</div>
<%= f.submit %>
<% end %>
_lot_fields.html.erb (partial)
<div class="nested-fields">
<h3> New Lots </h3>
<div class="field">
<%= f.label :status %>
<br/>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.label :stage %>
<br/>
<%= f.text_field :stage %>
</div>
<div class="field">
<%= f.label :land_size %>
<br/>
<%= f.text_field :land_size %>
</div>
<div class="field">
<%= f.label :price %>
<br/>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :eta %>
<br/>
<%= f.text_field :eta %>
</div>
<%= link_to_remove_association "remove lot", f %>
</div>
_listing_fields.html.erb (partial)
<div class="nested-fields">
<h3> New Listing </h3>
<div class="field">
<%= f.label :status %>
<br/>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.label :stage %>
<br/>
<%= f.text_field :stage %>
</div>
<div class="field">
<%= f.label :land_size %>
<br/>
<%= f.text_field :land_size %>
</div>
<div class="field">
<%= f.label :price %>
<br/>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :eta %>
<br/>
<%= f.text_field :eta %>
</div>
<%= link_to_remove_association "remove listing", f %>
</div>
new.html.erb
<h1>New Development</h1>
<%= render 'form', development: #development %>
<%= link_to 'Back', developments_path %>
I have a clone project of this and I noticed in the schema.rb that there is a key difference:
t.index ["developments_id"], name: "index_lots_on_developments_id"
I believe it should be development_id instead of being pluralised, but I'm not sure where/how this needs to be changed. I'm under the impression you should never alter a schema file.
Any help is greatly appreciated!
Exactly, it should be :
t.index ["development_id"], name: "index_lots_on_development_id"
You have to rewrite a migration with
def change
remove_index :lots, name: "index_lots_on_developments_id"
add_reference :lots, :development, index: true
end

Undefined method when saving an instance of my "Stop" model

My Rails App consists of touristic Routes. Each route has many stops and waypoints and stops have categories.
Waypoints are just the intermediate model between routes and stops because stops can belong to many routes.
So, when i try to save a Stop, i get the error:
undefined method `category' for #<Stop:0x007fa067acea90>
And the " if #stop.save" line is highlighted in red:
respond_to do |format|
if #stop.save
Waypoint.create(route_id: params[:route_id] , stop_id: #stop.id)
#print out category id or name here
category = params[:category]
In my stops controller i have this on create:
def create
#stop = Stop.new(stop_params)
respond_to do |format|
if #stop.save
Waypoint.create(route_id: params[:route_id] , stop_id: #stop.id)
#print out category id or name here
category = params[:category]
#once printed save it (stop category)
StopCategory.create(category_id: category, stop_id: #stop.id)
format.html { redirect_to #stop, notice: 'Stop was successfully created.' }
format.json { render :show, status: :created, location:#stop }
else
format.html { render :new }
format.json { render json: #stop.errors, status: :unprocessable_entity }
end
end
And on my waypoints controller i have this on create:
def create
#waypoint = Waypoint.create(waypoint_params)
redirect_to #waypoint.route
end
This is the stop model:
class Stop < ActiveRecord::Base
validates :description, length: { maximum: 140 }
validates :category, presence: true
#Image Uploader
mount_uploader :stop_image, StopImageUploader
#Relationship with stops and waypoints
has_many :waypoints
has_many :routes, through: :waypoints
# Relationship with categories
has_many :stop_categories
has_many :categories, through: :stop_categories
end
And this is the view where the form is :
<h2>Create a new stop:</h2><br>
<%= form_for(#stop) do |f| %>
<% if #stop.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#stop.errors.count, "error") %> prohibited this stop from being saved:</h2>
<ul>
<% #stop.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<select name="category" id=" " >
<option value="" disabled selected> Select a Category </option>
<% #categories.each do |category| %>
<option value="<%= category.id %>"> <%= category.name %> </option>
<% end %>
</select>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :stop_image %><br>
<%= f.file_field :stop_image %>
</div>
<div class="field">
<%= f.label :stop_lat %><br>
<%= f.number_field :stop_lat, :class => 'text_field', :step => 'any' %>
</div>
<div class="field">
<%= f.label :stop_long %><br>
<%= f.number_field :stop_long, :class => 'text_field', :step => 'any' %>
</div>
<%= hidden_field_tag :route_id, params[:id] %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h2>Add existing stop</h2>
<br>
<%= form_for(#waypoint) do |f| %>
<div class="field">
<%= f.label :stop_id %><br>
<%= f.number_field :stop_id %>
</div>
<%= f.hidden_field :route_id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit', edit_route_path(#route) %> |
<%= link_to 'Back', routes_path %>
<br>
<br>
<br>
</div>
Any ideas ?
Thanks!
Do you have a 'category' attribute in your Stop model? If not, then the following doesn't make sense:
validates :category, presence: true
That is where it's probably failing.
If you want to validate the presence of categories, you need to replace that check with something like this:
validate :has_categories
def has_categories # make this private
if categories.blank?
# add errors here
end
end
The following might also work:
validates :categories, presence: true
In the Stop model, you should replace the line
validates :category, presence: true
with
validates :categories, presence: true
because the validator takes the attribute/association name as argument, not the model name of the association.
Your association is called categories, so that's what you need to perform the validation on.

has_many record insertion

I want to insert tags in new Place. giving that tags is in different model.
####### models ##########
class Tag < ActiveRecord::Base
belongs_to :place
attr_accessible :tag
end
class Place < ActiveRecord::Base
has_many :tags
end
how can I handle this in the new Place create form? and the create action in the places_controller? so that I can insert new place and mant tags then assign the product id to each tag.
####### place controller ##########
def create
#place = Place.new(params[:place])
#tag = Tag.new(params[:?????]) #this should be more than once
respond_to do |format|
if #place.save
format.html { redirect_to #place, notice: 'Place was successfully created.' }
format.json { render json: #place, status: :created, location: #place }
else
format.html { render action: "new" }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
####### place new form ##########
<%= form_for #place do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :rank %><br />
<%= f.text_field :rank %>
</div>
<div class="field">
<%= f.label :lat %><br />
<%= f.text_field :lat %>
</div><div class="field">
<%= f.label :lng %><br />
<%= f.text_field :lng %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :website %><br />
<%= f.text_field :website %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= label_tag "tags" %>
<%= f.text_field :tag %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If you want to do it on your own, you should look into accepts_nested_attributes_for, where there's a great 2 part tutorial by Ryan Bates at railscasts.com
If you don't want to do it on your own, there are several tag gems available, for instance:
ActsAsTaggableOn.
Add this in your form:
<%= f.fields_for :tags do |t| %>
<%= t.label :name %>
<%= t.text_field :name %>
<% end %>
And in your Place model:
accepts_nested_attributes_for :tags
And in your controller, you wouldn't even have to worry about creating tags.
You should still try to read about fields_for and accepts_nested_attributes_for, they're often useful, since your problem is very common.

Ruby On Rails Form with integers titles instead of numbers in a drop-select-list

Hi
I'm pretty new with Ruby On Rails, and came across this problem.
I have 4 tables, and 1 that has the three others connected to it.
Sportcategories - name of each category
Sports - Name of each sport
Clubs - Name of each club
Results,
t.integer "sportcategory_id"
t.integer "sport_id"
t.integer "club_id"
I have managed to make a simple edit form with text_field for each field in results. But how can I get the names for the integers instead of the numbers?
<%= form_for(#result) do |f| %>
#if...
#..
#end
<div class="field">
<%= f.label :sportcategory_id%><br />
<%= f.text_field :sportcategory_id%>
</div>
<div class="field">
<%= f.label :sport_id %><br />
<%= f.text_field :sport_id %>
</div>
<div class="field">
<%= f.label :club_id %><br />
<%= f.text_field :club_id %>
</div>
<div class="field">
<%= f.label :result %><br />
<%= f.text_field :result %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I have made it so that SportCat, Sports and clubs has many results and that results belongs to all of them.
This is my controller file for results with edit & update
def edit
#result = Price.find(params[:id])
end
def update
#price = Price.find(params[:id])
respond_to do |format|
if #price.update_attributes(params[:price])
format.html { redirect_to(#price, :notice => 'Price was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #price.errors, :status => :unprocessable_entity }
end
end
end
And question two might be answered in question one, but I want to be able to choose from a drop-down list from all the available categories, sports and clubs with their actual name and then pass the right ID when I update it.
Check the Rails Form select helper
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
<%= form_for(#result) do |f| %>
<div class="field">
<%= f.label :sportcategory_id%><br />
<%= f.select :sportcategory_id, #sportcategories.map {|s| [s.name, s.id]} %>
</div>
<div class="field">
<%= f.label :sport_id %><br />
<%= f.select :sport_id, #sports.map {|s| [s.name, s.id]} %>
</div>
<div class="field">
<%= f.label :club_id %><br />
<%= f.select :club_id, #clubs.map {|c| [c.name, c.id]} %>
</div>
<div class="field">
<%= f.label :result %><br />
<%= f.text_field :result %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Get #sportcategories, #sports, #clubs in your controller actions.

Resources