I have a admin section and inside the admin i have, Finalist and Images.
Im using, ruby 1.8.7-p249 and Rails 3.1.2
# model image.rb
class Admin::Image < ActiveRecord::Base
belongs_to :finalist
...
end
# model finalist.rb
class Admin::Image < ActiveRecord::Base
has_many :images
...
end
My ImagesController:
def new
#admin_finalist = Admin::Finalist.find(params[:finalist_id])
#admin_image = #admin_finalist.images.build
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #admin_image }
end
end
def edit
#admin_finalist = Admin::Finalist.find(params[:finalist_id])
#admin_image = #admin_finalist.images.find(params[:id])
end
def create
#admin_finalist = Admin::Finalist.find(params[:finalist_id])
#admin_image = #admin_finalist.images.new(params[:admin_image])
respond_to do |format|
if #admin_image.save
format.html { redirect_to admin_finalist_images_path(#admin_finalist) }
format.json { render :json => #admin_image, :status => :created, :location => #admin_image }
else
format.html { render :action => "new" }
format.json { render :json => #admin_image.errors, :status => :unprocessable_entity }
end
end
end
def update
#admin_finalist = Admin::Finalist.find(params[:finalist_id])
#admin_image = #admin_finalist.images.find(params[:id])
respond_to do |format|
if #admin_image.update_attributes(params[:admin_image])
format.html { redirect_to admin_finalist_images_path(#admin_finalist) }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => #admin_image.errors, :status => :unprocessable_entity }
end
end
end
And my Images _form.html.erb:
<%= form_for([#admin_finalist, #admin_image]) do |f| %>
<% if #admin_image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin_image.errors.count, "error") %> prohibited this admin_image from being saved:</h2>
<ul>
<% #admin_image.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :file %>
<%= f.file_field :file %>
</div>
<%- unless #admin_image.new_record? || !#admin_image.file? -%>
<div class="field">
<%= f.label :remove_file, "Remover?" %>
<%= f.check_box :remove_file %>
</div>
<%- end -%>
<div class="field">
<%= f.label :url %>
<%= f.text_field :url, :placeholder => "http://" %>
</div>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="actions">
<%= image_submit_tag("button/save.png") %>
<%= link_to image_tag("button/back.png"), admin_finalist_images_path %>
</div>
<% end %>
Everything seems perfect, but im getting this error when im trying to add an image to finalist at admin/finalists/1/images/new.
Showing .../app/views/admin/images/_form.html.erb where line #5 raised:
undefined method `admin_finalist_admin_images_path' for #<#<Class:0x102da0068>:0x102d9a870>
So, i tryed to tell rails to use a particular url:
<%= form_for([#admin_finalist, #admin_image], :url => admin_finalist_image_path(#admin_finalist.id, #admin_image)) do |f| %>
Saddly, this work only for editing. When i try to edit i get this error message:
No route matches {:controller=>"admin/images", :finalist_id=>1, :id=>nil, :action=>"show"}
And if i wanna create i need to do:
<%= form_for([#admin_finalist, #admin_image], :url => admin_finalist_images_path) do |f| %>
And if i try to edit i get this message:
No route matches [PUT] "/admin/finalists/1/images"
I dont know what to do... I tried everything without success, if anyone can help me would be much appreciated.
EDIT
The problem was that my models was namespaced. So, i rewrite all the app to fix this, and thats running normally.
[]`s
Related
I am new to spree and ruby on rails. while creating a custom controller in my spree app, I can successfully add link to it in spree admin panel using deface. but when I go to that link, it gives me following error
NoMethodError in Spree::Admin::Societies#new
Showing app/views/spree/admin/societies/_form.html.erb where line #1 raised:
undefined method `societies_path' for #<#<Class:0x007f19cb636898>:0x007f19c5ecacf8>
I don't know from where it is looking for 'societies_path' as I already have updated app/views/spree/admin/societies/new.html.erb to look for 'admin_societies_path', here it is
<%= render 'form' %>
<%= link_to 'Back', admin_societies_path %>
and app/views/spree/admin/societies/_form.html.erb contains
<%= form_for(#society) do |f| %>
<% if #society.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#society.errors.count, "error") %> prohibited this society from being saved:</h2>
<ul>
<% #society.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>
<div class="field">
<%= f.label :address %><br>
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :area %><br>
<%= f.text_field :area %>
</div>
<div class="field">
<%= f.label :postcode %><br>
<%= f.number_field :postcode %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I tried removing link to back also, but it's again giving same error.
config/routes.rb is
mount Spree::Core::Engine, :at => '/'
Spree::Core::Engine.add_routes do
namespace :admin do
resource :societies
end
end
and my app/controllers/spree/admin/societies_controller.rb is
module Spree
module Admin
class SocietiesController < Spree::Admin::BaseController
before_action :set_society, only: [:show, :edit, :update, :destroy]
def index
#societies = Society.all
end
def show
end
def new
#society = Society.new
end
def edit
end
def create
#society = Society.new(society_params)
respond_to do |format|
if #society.save
format.html { redirect_to #society, notice: 'Society was successfully created.' }
format.json { render :show, status: :created, location: #society }
else
format.html { render :new }
format.json { render json: #society.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #society.update(society_params)
format.html { redirect_to #society, notice: 'Society was successfully updated.' }
format.json { render :show, status: :ok, location: #society }
else
format.html { render :edit }
format.json { render json: #society.errors, status: :unprocessable_entity }
end
end
end
def destroy
#society.destroy
respond_to do |format|
format.html { redirect_to societies_url, notice: 'Society was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_society
#society = Society.find(params[:id])
end
def society_params
params.require(:society).permit(:name, :url, :building_number, :address, :area, :postcode, :city, :active, :IsDelete)
end
end
end
end
Any help would be greatly appreciated.
I would suspect that it is this line in _form partial
<%= form_for(#society) do |f| %>
You need to reference the namespace here, so maybe somthing like
<%= form_for([:admin, #society]) do |f| %>
or add your own url
<%= form_for(#society, url: admin_societies_path) do |f| %>
i have the following situation:
I have a controller for admins , that i use to manage the users (model generated by Devise).
So, i use the actions of another controller to manage the resources "users".
Here my files:
admins_controller.rb
before_action :set_user, only: [:show, :edit, :update, :destroy]
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to admins_index_path, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
def set_user
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:id)
end
edit.html.erb
<h1>Edit user</h1>
<%= form_for #user, :as => :patch, :url => admins_update_path(id: #user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this line_item from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :id %><br>
<%= f.text_field :id %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :admin %><br>
<%= f.text_field :admin %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The problem is that when i submit the edit , rails gives me an error:
Can you help me? i really don t knok how to fix
thank you
EDIT:
Change your view to drop the :as option, that will fix the params requirement error:
= form_for #user, :url => admins_update_path(id: #user) do |f|
The :as tells form_for to use a different key name than the default class-name based one that you're using in your params.require call.
You should also remove your f.text_field :id - you don't want someone editing that.
Then your permit(...) block will need to include anything else in that form that you want to allow mass-assignment for, most likely you'll want: permit(:email, :admin)
I am using rails 4 and carrierwave gem to uplaod files. I have a product and assets model. Product has_many assets and a nested form is used to save the assets in product. When i update any product, its assets field get duplicated and when I edit same product again I can see 8 fields in the form in place of 4 fields.
Product.rb
class Product < ActiveRecord::Base
has_many :assets , :dependent => :delete_all
accepts_nested_attributes_for :assets
Asset.rb
class Asset < ActiveRecord::Base
mount_uploader :asset1, AssetUploader
mount_uploader :asset2, AssetUploader
mount_uploader :asset3, AssetUploader
mount_uploader :asset4, AssetUploader
end
Product controller
def new
#product = Product.new
#product.assets.build
end
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to product_product_detail_path(#product), notice: 'Product was successfully created.' }
format.json { render action: 'show', status: :created, location: #product }
else
format.html { render action: 'new' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to sub_category_path(#product.sub_category_id), notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
new_html.erb for product
<%= form_for(#product, html: { multipart: true }) do |f| %>
<%= f.fields_for :assets do |asset| %>
<div class="field">
<%= asset.label :asset, "File #1" %>
<% unless asset.object.asset1.file.nil? %>
<%= image_tag(asset.object.asset1.url) %>
<p><% asset.object.asset1.file.filename %></p>
<p style="float:left">Change File?</p>
<% end %>
<%= asset.file_field :asset1 %>
</div>
<div class="field">
<%= asset.label :asset, "File #2" %>
<% unless asset.object.asset2.file.nil? %>
<%= image_tag(asset.object.asset2.url) %>
<p><% asset.object.asset2.file.filename %></p>
<p style="float:left">Change File?</p>
<% end %>
<%= asset.file_field :asset2 %>
</div>
<div class="field">
<%= asset.label :asset, "File #3" %>
<% unless asset.object.asset3.file.nil? %>
<%= image_tag(asset.object.asset3.url) %>
<p><% asset.object.asset3.file.filename %></p>
<p style="float:left">Change File?</p>
<% end %>
<%= asset.file_field :asset3 %>
</div>
<div class="field">
<%= asset.label :asset, "File #4" %>
<% unless asset.object.asset4.file.nil? %>
<%= image_tag(asset.object.asset4.url) %>
<p><% asset.object.asset4.file.filename %></p>
<p style="float:left">Change File?</p>
<% end %>
<%= asset.file_field :asset4 %>
</div>
<% end %>
<div class="actions">
<%= f.submit :class=> "action-btn" %>
</div>
<% end %>
UPDATE-
def product_params
params.require(:product).permit(assets_attributes: [:asset, :asset1, :asset2, :asset3, :asset4 ])
end
def edit
end
Please help me to solve the issue.
Try (id for update):
def product_params
params.require(:product).permit(assets_attributes: [:id, :asset, :asset1, :asset2, :asset3, :asset4 ])
end
I'm using Bootstrap-Sass along with Formstatic. I thought an error message should be automatically shown next to the field with Formstatic like in this picture:
(source: asciicasts.com)
But even if the user puts an invalid input, my app does not show an error message. This seems to be an easy problem but I cant figure out the reason behind.
PostController
# POST /posts
# POST /posts.json
def create
#post = Post.new(params[:post])
#post.view = 0
#post.like = 0
#post.hate = 0
respond_to do |format|
if #post.save
#posts = Post.paginate(:page => params[:page], order: 'like desc', per_page: 10)
format.html { redirect_to posts_path }
format.json { render json: #post, status: :created, location: #post }
else
format.html { render action: "new" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
PostModel
validates :name, :presence => true
validates :content, :presence => true,
:length => { :minimum => 10, :maximum => 300}
_form (Post)
<% #post = Post.new %>
<%= semantic_form_for #post do |f| %>
<%= f.semantic_errors :name %>
<%= f.inputs do %>
<%= f.input :name, :label => 'name' %>
<%= f.input :content, :label => 'body' %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :button_html => { :class => "btn btn-primary" }, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
In PostController, I tried deleting the following two lines
#format.html { render action: "new" }
#format.json { render json: #post.errors, status: :unprocessable_entity }
and added
render #post.errors
Then, I got
#messages={:name=>["can't be blank"], :content=>["can't be blank", "is too short (minimum is 10 characters)"]}>
So I think the problem might be the way I'm rendering json is wrong. Could someone help me how to fix it?
Rails has its own rendering for validation errors, which doesn’t match the HTML structure or the CSS structure used by Bootstrap.
To solve this porblem you can just add to view your own code block for output errors.
<% if #posts and #posts.errors and #posts.errors.count > 0 %>
<div class="alert alert-danger">
<a class="close" data-dismiss="alert">×<a>
<strong><%= pluralize(#posts.errors.count,"error") %> validation problems found.</strong>
<ul>
<% #posts.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
</div>
Or you can move this block to partial template to avoid dublication of code.
<% if resource and resource.errors and resource.errors.count > 0 %>
<div class="alert alert-danger">
<a class="close" data-dismiss="alert">×<a>
<strong><%= pluralize(resource.errors.count,"error") %> validation problems found.</strong>
<ul>
<% resource.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
</div>
<% end %>
And pass #posts to parameters
<%= render "shared/validation_errors", :resource => #posts %>
Here you can find more information about this problem
http://fizzylogic.nl/2013/12/22/temp-slug-54/
I have a rails app with a has_and_belongs to many association, I keep getting this error whenever I goto the URL for this controller:
Showing /Users/Sam/makrrEdu/app/views/enroll/_form.html.erb where line #1 raised:
undefined method `course_users_path' for #<#<Class:0x10fd9be48>:0x10fd93d60>
Extracted source (around line #1):
1: <%= form_for(#userC) do |f| %>
2: <% if #userC.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(#userC.errors.count, "error") %> prohibited this enroll from being saved:</h2>
Trace of template inclusion: app/views/enroll/new.html.erb
Rails.root: /Users/Sam/makrrEdu
Application Trace | Framework Trace | Full Trace
app/views/enroll/_form.html.erb:1:in `_app_views_enroll__form_html_erb___794935172_2280224940'
app/views/enroll/new.html.erb:3:in `_app_views_enroll_new_html_erb___1934508758_2280307340'
app/controllers/enroll_controller.rb:24:in `new'
Heres my code:
enroll_controller.rb
class EnrollController < ApplicationController
def new
#userC = CourseUser.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #userC }
end
end
def index
end
def create
#userC = CourseUser.new(params[:course])
respond_to do |format|
if #userC.save
format.html { redirect_to #userC, :notice => 'Enroll was successfully created.' }
format.json { render :json => #userC, :status => :created, :location => #userC }
else
format.html { render :action => "new" }
format.json { render :json => #userC.errors, :status => :unprocessable_entity }
end
end
end
end
new.html.erb
<h1>New Enroll</h1>
<%= render 'form' %>
<%= link_to 'Back', courses_path %>
_form.html.erb
<%= form_for(#userC) do |f| %>
<% if #userC.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#userC.errors.count, "error") %> prohibited this enroll from being saved:</h2>
<ul>
<% #userC.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="field">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
course_user.rb (model)
class CourseUser < ActiveRecord::Base
belongs_to :user
belongs_to :course
attr_accessible :course_id, :user_id
end
I forgot to put resources :course_users in my routes.rb file.