I'm trying to display images on the homepage with paperclip and I have unpermitted params and a routing error. I have tried various solutions including trying to pass in an array but I think this is happening because of my own lack of knowledge about rails.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Hdw1RzedMZdUE0cPrAXz0fkctQKfW9HX3S5ZwYh4lr0PwTJhHhVwcrglJv5qrMQF3T5YkcJZ9zBiRRRlCoNCNQ==", "document"=>{"doc"=>[#<ActionDispatch::Http::UploadedFile:0x007feaf2db80f0 #tempfile=#<Tempfile:/var/folders/1q/zfrk5kxj1015crsc13jwwrz40000gp/T/RackMultipart20170608-15326-1pcmtgl.jpg>, #original_filename="P1150645.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"document[doc][]\"; filename=\"P1150645.jpg\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Upload Document"}
Unpermitted parameter: doc
(1.9ms) begin transaction
SQL (2.0ms) INSERT INTO "documents" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-06-08 04:47:55 UTC], ["updated_at", 2017-06-08 04:47:55 UTC]]
(0.8ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 46ms (ActiveRecord: 4.8ms)
Started GET "/" for ::1 at 2017-06-08 12:47:55 +0800
Processing by PagesController#home as HTML
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" LIMIT ? [["LIMIT", 1]]
Rendering pages/home.html.erb within layouts/application
Document Load (3.1ms) SELECT "documents".* FROM "documents" ORDER BY created_at
Rendered documents/_documents.html.erb (56.7ms)
Rendered documents/_new.html.erb (4.2ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" != 3)
Conversation Load (0.2ms) SELECT "conversations".* FROM "conversations" WHERE (conversations.sender_id = 3 OR conversations.recipient_id = 3)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.2ms) SELECT COUNT(*) FROM "messages" WHERE "messages"."conversation_id" = ? [["conversation_id", 4]]
Rendered conversations/_index.html.erb (18.5ms)
Rendered pages/home.html.erb within layouts/application (141.2ms)
Rendered shared/_navbar.html.erb (5.2ms)
Completed 200 OK in 426ms (Views: 401.3ms | ActiveRecord: 6.2ms)
Started GET "/docs/original/missing.png" for ::1 at 2017-06-08 12:47:56 +0800
ActionController::RoutingError (No route matches [GET] "/docs/original/missing.png"):
I've tried nesting the params given that document => doc is passing to an array, even if I leave the params blank it still uploads to the database with null fields and routing error, DocumentsController:
def doc_params
params.require(:document).permit(:id, :doc)
end
I've tried multiple validations in here:
class Document < ApplicationRecord
has_attached_file :doc
do_not_validate_attachment_file_type :doc
end
I wondering if rendering the index is causing the routing error as I got rid of temporarily by re-routing but the image was still null, documents/_index.html.erb
<div class="container">
<div class="row around-xs">
<center>
<% #documents.each do |document| %>
<li class="col-xs-3" id="item-grid">
<%= link_to image_tag(document.doc.url, class: 'media-object', size:"108x152"), document.doc.url, target: '_blank' %>
<% if #users %>
<% if current_user.is_admin? %>
<%= link_to 'Remove', document_path(document), class: 'btn btn-danger', method: :delete, data: {confirm: 'Are you sure?'} %>
<% end %>
<% end %>
</li>
<% end %>
</center>
</div>
</div>
home.html.erb
<section id="about" class="about-section">
<div class="container" id="services">
<div class="col-lg-8 col-md-offset-2" id="progpos"><h1>My Work</h1></div>
<%= render 'documents/index' %>
<br>
<%= render 'documents/new' %>
</div>
</section>
Please please help! thanks! documents/_new.html.erb
<% if #users %>
<% if current_user.is_admin? %>
<%= form_for #document, html: { multipart: true } do |f| %>
<% if #document.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(#document.errors.count, "error")} prohibited this document from being saved:" %>
</h2>
<ul>
<% #document.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group" style="width: 20%">
<%= f.file_field :doc, class: 'form-control', placeholder: "Document", multiple: true %>
</div>
<div class="form-group">
<%= f.submit 'Upload Document', class: 'btn btn-default' %>
</div>
<% end %>
<% end %>
<% end %>
Here's the whole of the controller and I've edited documents/_new.html.erb to how it looks:
class DocumentsController < ApplicationController
def index
#documents = Document.order('created_at')
end
def new
#document = Document.find(params[:id])
end
def create
#document = Document.new(doc_params)
if #document.save
**params[:document][:doc].each do |d| #iterate over multiple attached files
#document.create(doc: d)**
end
flash[:success] = "The document was added!"
redirect_to root_path
else
render '_new'
end
end
def destroy
#document = Document.find(params[:id])
if #document.destroy
flash[:notice] = "Successfully deleted photo!"
redirect_to root_path
else
flash[:alert] = "Error deleting photo!"
end
end
private
def doc_params
params.require(:document).permit(:id, **document: {doc: []}**)
end
end
I've added Pavans code to mine and i've also changed the params at the bottom which now gives me undefined method `create' for #. I think that's progress?
Unpermitted parameter: doc
You have multiple :true set for field doc, so doc should an array to accept the values. You should change the doc_params to below
def doc_params
params.require(:document).permit(:id, doc: [])
end
No handler found for
[#,
#original_filename="P1150645.jpg", #content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"document[doc][]\";
filename=\"P1150645.jpg\"\r\nContent-Type: image/jpeg\r\n">]
You should also set multipart: true to the form to handle file uploads
<%= form_for #document, html: { multipart: true } do |f| %>
ActionController::RoutingError (No route matches [GET]
"/docs/original/missing.png")
Paperclip will try to find missing.png when an object doesn't have an uploaded file and you have tell Paperclip where to find it!
class Document < ApplicationRecord
has_attached_file :doc, :styles => { :medium => "250x250>", :thumb => "150x150>" }, :default_url => "/system/:attachment/:style/missing.jpg"
do_not_validate_attachment_file_type :doc
end
Update:
Your create action should look like below
def create
#document = Document.new(doc_params)
if #document.save
params[:document][:doc].each do |d| #iterate over multiple attached files
#doumnet.create(doc: d)
end
flash[:success] = "The document was added!"
redirect_to root_path
else
render 'new'
end
end
Related
I tried to do something similar to this in a form, but get this error:
Started POST "/opinions" for 127.0.0.1 at 2019-01-03 17:11:12 -0800
Processing by OpinionsController#create as JS
Parameters: {"utf8"=>"✓", "opinion"=>{"content"=>"This is an opinion"}, "type_of"=>"pro", "topicId"=>"{:value=>2}", "commit"=>"Create Opinion"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Topic Load (0.0ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound (Couldn't find Topic with 'id'=):
app/controllers/opinions_controller.rb:6:in `create'
opinions_form.html.erb:
<%= form_for(opinion, :html=> {class:"form-horizontal", role:"form"}, remote: true) do |f| %>
<div class="form-group">
<div class="col-sm-12">
<%= f.text_area :content, rows:4, class: "form-control", placeholder: "Opinion" %>
</div>
</div>
<%= hidden_field_tag 'type_of', typeOf %>
<%= hidden_field_tag :topicId, :value => #topic.id %>
<% puts "ID: " + #topic.id.to_s %>
<div class="form-group">
<div class="col-sm-12">
<%= f.submit %>
</div>
</div>
<% end %>
Relevant code in controller:
def create
#opinion = Opinion.new(opinion_params)
#opinion.user = current_user
#opinion.topic = Topic.find(params[:opinion][:topicId])
if #opinion.save
flash[:success] = 'Opinion Added'
else
puts #opinion.errors.full_messages
flash[:danger] = 'Opinion not Added'
end
end
private
def opinion_params
params.require(:opinion).permit(:content, :type_of)
end
and finally, relevant code in the topic show page:
<td>
<%= render 'opinions/form', opinion: Opinion.new, typeOf: "pro", :topic => #topic %>
</td>
<td>
<%= render 'opinions/form', opinion: Opinion.new, typeOf: "con", :topic => #topic %>
</td>
As you can see in the request parameters:
Parameters: {"utf8"=>"✓", "opinion"=>{"content"=>"This is an opinion"}, "type_of"=>"pro", "topicId"=>"{:value=>2}", "commit"=>"Create Opinion"}
The topicId parameter is not nested under opinion, so you need to change your finder to:
#opinion.topic = Topic.find(params[:topicId][:value])
You can also remove the superfluous value key in your view:
<%= hidden_field_tag :topicId, #topic.id %>
Which would further simplify your finder:
#opinion.topic = Topic.find(params[:topicId])
As an aside. Note that idiomatic ruby calls for snake_case in all identifiers. This won't change how your code works, but it will help other Ruby developers read your code.
I'm developing an application which uses CarrierWave as file manager (upload and download). It's used on my project model. It's kinda working because it upload the file but it doesn't refresh page nor show errors if there is any (but showed in the console -- whitelist error), in the other case (detroy) it shows the message correctly. I'll be really appreciated if you guys help me because i tested several things (redirect to root, etc) and nothing happens.
Project.rb
class Project < ApplicationRecord
mount_uploaders :attachments, AttachmentUploader # Tells raisl to use this uploader with this model
serialize :attachments, JSON # If you use SQLite, add this line.
end
attachments_controller.rb
class AttachmentsController < ApplicationController
before_action :set_project
def create
add_more_attachments(attachments_params[:attachments])
respond_to do |format|
if #project.save
format.html { redirect_to project_path(#project), notice: 'Archivo fue subido existosamente.' }
else
format.html { render partial: 'projects/uploads/new' }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
def download
path = "uploads/#{params[:id]}/#{params[:basename]}.#{params[:extension]}"
send_file path, :x_sendfile=>true
end
def destroy
remove_attachments_at_index(params[:id].to_i)
respond_to do |format|
format.html { redirect_to project_path(#project), notice: 'Archivo fue eliminado existosamente.' }
end
end
private
def set_project
#project = Project.find_by_id(params[:project_id])
end
def add_more_attachments(new_attachments)
attachments = #project.attachments # copy the old images
attachments += new_attachments # concat old images with new ones
#project.attachments = attachments # assign back
end
def remove_attachments_at_index(index)
if #project.attachments.count == 1
#truco para remover ultimo elemento
#project.remove_attachments!
else
remain_attachments = #project.attachments # copy the array
deleted_attachment = remain_attachments.delete_at(index) # delete the target image
#project.attachments = remain_attachments # re-assign back
end
#project.save
end
def attachments_params
params.require(:project).permit({attachments: []}) # allow nested params as array
end
end
Projects#Show (where i upload and remove files)
<div class="medium-8 columns">
<h3>
Archivos
</h3>
<ul>
<% if #project.attachments.present? %>
<% #project.attachments.each_with_index do |attachment,index| %>
<li>
<%= link_to "Descargar #{attachment.file.filename}", "/uploads/#{#project.id}/#{File.basename(attachment.url)}"%>
</li>
<%= link_to "Eliminar", project_attachment_path(#project, index), :method => :delete, data: { confirm: "¿Está seguro de eliminar este archivo?" } %>
<% end %>
<% end %>
</ul>
<%= render(:partial => 'projects/uploads/new') %>
</div>
_new.html.erb (attachment form)
<div class="">
<%= form_with(model: #project, :html => {multipart: true}, url: project_attachments_path(#project), method: :post) do |form| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% #project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form.label :attachments %>
<%= form.file_field :attachments, multiple: true %>
<%= form.submit "Subir", :class => "button" %>
<% end %>
</div>
Console output
Started POST "/projects/3/attachments" for 127.0.0.1 at 2018-03-25 11:51:26 -0300
Processing by AttachmentsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LW7yFaDtZqPSglxM8TSHy2FLBPIrhSl8Gn9rOkZheOWPlj6QDrGOPATlYOj2uQ5exMHp+T+iPbB8lehjY/IzqA==", "project"=>{"attachments"=>[#<ActionDispatch::Http::UploadedFile:0x007fecafc5b900 #tempfile=#<Tempfile:/tmp/RackMultipart20180325-24920-hgufys.pdf>, #original_filename="CAMBIO_ACUMULADOR_RADISSON(1).pdf", #content_type="application/pdf", #headers="Content-Disposition: form-data; name=\"project[attachments][]\"; filename=\"CAMBIO_ACUMULADOR_RADISSON(1).pdf\"\r\nContent-Type: application/pdf\r\n">]}, "commit"=>"Subir", "project_id"=>"3"}
Project Load (0.4ms) SELECT `projects`.* FROM `projects` WHERE `projects`.`id` = 3 LIMIT 1
(0.2ms) BEGIN
Estimate Load (0.4ms) SELECT `estimates`.* FROM `estimates` WHERE `estimates`.`id` = 3 LIMIT 1
SQL (0.6ms) UPDATE `projects` SET `attachments` = '[\"CV_Rodrigo_Cordova.pdf\",\"CAMBIO_ACUMULADOR_RADISSON_1_.pdf\"]', `updated_at` = '2018-03-25 11:51:26' WHERE `projects`.`id` = 3
(2.1ms) COMMIT
Redirected to http://localhost:3000/projects/3
Completed 302 Found in 16ms (ActiveRecord: 3.7ms)
And then... start get but no difference in browser, except when i refresh manually
Started GET "/projects/3" for 127.0.0.1 at 2018-03-25 11:51:26 -0300
Processing by ProjectsController#show as JS
Parameters: {"id"=>"3"}
Project Load (0.5ms) SELECT `projects`.* FROM `projects` WHERE `projects`.`id` = 3 LIMIT 1
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
User Load (0.5ms) SELECT `users`.* FROM `users` INNER JOIN `projects_users` ON `users`.`id` = `projects_users`.`user_id` WHERE `projects_users`.`project_id` = 3
Rendering projects/show.html.erb within layouts/application
Estimate Load (0.4ms) SELECT `estimates`.* FROM `estimates` WHERE `estimates`.`id` = 3 LIMIT 1
(0.4ms) SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 0
(0.4ms) SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 1
(0.4ms) SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 2
(0.3ms) SELECT COUNT(*) FROM `tasks` WHERE `tasks`.`project_id` = 3 AND `tasks`.`status` = 3
Rendered projects/uploads/_new.html.erb (1.9ms)
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered projects/show.html.erb within layouts/application (15.3ms)
Rendered layouts/_navigation_links.html.erb (0.4ms)
Rendered layouts/_nav_links_for_auth.html.erb (1.1ms)
Rendered layouts/_navigation.html.erb (9.8ms)
Rendered layouts/_messages.html.erb (0.4ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 139ms (Views: 124.5ms | ActiveRecord: 3.7ms)
EDIT:
Submit button but no change on listed files
Manually refresh (it works!)
Thanks in advance.
where is your image div?
<div class="medium-8 columns">
<h3>
Archivos
</h3>
<ul>
<% if #project.attachments.present? %>
<% #project.attachments.each_with_index do |attachment,index| %>
<li>
<%= link_to "Descargar #{attachment.file.filename}", "/uploads/#{#project.id}/#{File.basename(attachment.url)}"%>
</li>
<%= link_to "Eliminar", project_attachment_path(#project, index), :method => :delete, data: { confirm: "¿Está seguro de eliminar este archivo?" } %>
<% end %>
<% end %>
</ul>
<%= render(:partial => 'projects/uploads/new') %>
</div>
something like this
<%= image_tag attachment.url %>
but you say it works on refesh?
I found the solution. The form was being submitted by ajax.
So i had to add to my form local: true parameter, like this:
<%= form_with(model: #project,local: true, :html => {multipart: true}, url: project_attachments_path(#project), method: :post) do |form| %>
...
<% end %>
And works like a charm.
Thanks to the readers.
I followed the answer given here to create a simple dynamic form (not nested). The idea is to invite several people with their email. Then I have an "Add Team Member" button to add the fields to enter the coworker email.
I get the following error but I don't know why:
ActiveModel::ForbiddenAttributesError in InvitationsController#create
The parameters:
{"utf8"=>"✓",
"authenticity_token"=>"zLs8DWkzO+bPc2jGhFptgc+BAGAwzr1kcn/hkX/6vQbQ/cDCzuqoGCMGUTcYHs+up7nBzHFiEXVcKustyL1KIA==",
"invitation"=>[{"email"=>"test#test.fr", "_destroy"=>"false", "user_id"=>"1"}, {"email"=>"test2#test2.fr", "_destroy"=>"false", "user_id"=>"1"}],
"commit"=>"Save Team Members"}
My create action and invitation_params in the controller:
def create
puts params[:invitation].to_yaml
params[:invitation].each do |invitation_params|
Invitation.create(invitation_params)
end
redirect_to invitation_path
end
def invitation_params
params.require(:invitation).permit(:id, :email, :user_id, :_destroy, :token)
end
My form:
<%= form_for(invitation) do |f| %>
<% if invitation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(invitation.errors.count, "error") %> prohibited this invitation from being saved:</h2>
<ul>
<% invitation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="container_invitations"></div>
<%= link_to "Add a New Team Member", add_invitation_path, :method => :post, :remote => true %>
<div class="actions">
<%= f.submit "Save Team Members", class: "cta2" %>
</div>
<% end %>
add_invitation controller action (to add the fields dynamically):
def add_invitation
respond_to do |format|
format.js
end
end
add_invitation.js.erb:
$('#container_invitations').append("<%= escape_javascript(render :partial => 'invitation_fields_render') %>");
$('.remove_fields').on('click', function(e) {
$(this).parent().remove();
});
_invitation_fields_render.html.erb (the partial with the fields)
<div class="new_invitation_row">
<%= email_field_tag "invitation[][email]", nil, placeholder: "Team Member Email", :required => 'required' %>
<%= hidden_field_tag "invitation[][_destroy]", nil, value: false %>
<a class="remove-link remove_fields dynamic" href="#"><i class="fa fa-trash fa-lg" title="Remove"></i></a>
<%= hidden_field_tag "invitation[][user_id]", nil, value: current_user.id %>
</div>
My invitation model:
class Invitation < ActiveRecord::Base
has_secure_token
belongs_to :user
end
Thank you.
UPDATE
I added the new params.permit line proposed by IngoAlbers. I still get the error. Here is the full stacktrace of the error:
Started POST "/invitations" for 127.0.0.1 at 2017-12-01 07:49:02 +0000
Processing by InvitationsController#create as HTML Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"pV5wQj3QHXksNEtJ8nZSjWDc8VyHiewVCt4A1+ijH8G5GIyNmgmOh8BBcrhuMvCiCOQw8MYlQAQkiwprX+To5w==",
"invitation"=>[{"email"=>"test#test.fr", "_destroy"=>"false",
"user_id"=>"1"}], "commit"=>"Save Team Members"} User Load (0.4ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY
"users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] CoreBot Load
(0.2ms) SELECT "core_bots".* FROM "core_bots" WHERE "core_bots"."id"
= ? LIMIT ? [["id", 1], ["LIMIT", 1]] Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.6ms)
ActiveModel::ForbiddenAttributesError
(ActiveModel::ForbiddenAttributesError):
app/controllers/invitations_controller.rb:35:in block in create'
app/controllers/invitations_controller.rb:34:ineach'
app/controllers/invitations_controller.rb:34:in `create' Rendering
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout Rendering
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
(5.3ms) Rendering
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.5ms) Rendering
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(1.5ms) Rendered
/Users/nicolasleroux/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout (93.1ms)
The problem is that invitation is an array. So you actually need to permit the array in the invitation_params.
def invitation_params
params.permit(invitation: [:id, :email, :user_id, :_destroy, :token])
end
In addition you have a problem in the create action itself.
You don't actually use the permitted parameters, that was defined.
It should probably look like this:
def create
invitation_params[:invitation].each do |invitation|
Invitation.create(invitation)
end
redirect_to invitation_path
end
I have a controller that is supposed to display a flash message. The flash message is not displaying.
My only problem is the flash messages not showing up. If anyone could please give an explanation for this that would be awesome. I am guessing that my error is noobish but i do not know enough to explain what is happening.
Edit: I added my Log for this section
Edit: When i click the manage button with invalid information nothing happens. When i refresh the page the flash message shows up. I really just need the current page refreshed but it is not working. If anyone has any ideas please share them.
Edit: I have tried changing things to
redirect_to #department, :notice => "invalid Password"
I am still not able to get the flash message to show up.
EDIT: From this site it says flash now will be displayed in the view that you are rendering. But my rendered view is a partial. Can anyone at least let me know if i am on the right track. A little direction or thought other than my own?
This is the create method for my relationship.
def create
#department = Department.find(params[:manager_relationship][:department_id])
if #department.authenticate(params[:manager_relationship][:password])
current_user.manage!(#department)
respond_to do |format|
format.html { redirect_to #department }
format.js
end
else
redirect_to department_path#department, :alert => "Invalid Password"
end
end
This same code is used successfully in my sessions controller.
SessionsController
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
It is called from a partial
department/show.hmtl.erb
<% provide(:title, #department.department_name) %>
<div class="span8">
<%= render 'manage_form' if signed_in? %>
</div>
_manage_form.html.erb
<div id="manage_form">
<% if current_user.managing?(#department) %>
<%= render 'unmanage' %>
<% else %>
<%= render 'manage' %>
<% end %>
</div>
_manage.html.erb
<%= form_for(current_user.manager_relationships.build(department_id: #department.id), remote: true) do |f| %>
<div>
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div><%= f.hidden_field :department_id %></div>
<%= f.submit "Manage department", class: "btn btn-large btn-primary" %>
<% end %>
application.html.erb
<!DOCTYPE html>
<html>
<head>
.
.
.
...
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
Here is the log for an unmanage and manage http request
Started DELETE "/manager_relationships/22" for 127.0.0.1 at 2013-03-24 11:08:20 -0700
Processing by ManagerRelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y3I7qY4LBfChjdYAUlJ1eDh23YkcJRwSRfa6s2wavoI=", "commit"=>"Unmanage", "id"=>"22"}
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = '9J1RAJy16Tooz5wMmzQohw' LIMIT 1[0m
[1m[35mManagerRelationship Load (0.0ms)[0m SELECT "manager_relationships".* FROM "manager_relationships" WHERE "manager_relationships"."id" = ? LIMIT 1 [["id", "22"]]
[1m[36mDepartment Load (1.0ms)[0m [1mSELECT "department".* FROM "departments" WHERE "department"."id" = 1 LIMIT 1[0m
[1m[35mManagerRelationship Load (0.0ms)[0m SELECT "manager_relationships".* FROM "manager_relationships" WHERE "manager_relationships"."user_id" = 1 AND "manager_relationships"."department_id" = 1 LIMIT 1
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mSQL (0.0ms)[0m DELETE FROM "manager_relationships" WHERE "manager_relationships"."id" = ? [["id", 22]]
[1m[36m (26.0ms)[0m [1mcommit transaction[0m
Rendered departments/_manage.html.erb (2.0ms)
Rendered manager_relationships/destroy.js.erb (5.0ms)
Completed 200 OK in 86ms (Views: 11.0ms | ActiveRecord: 27.0ms)
Started POST "/manager_relationships" for 127.0.0.1 at 2013-03-24 11:08:23 -0700
Processing by ManagerRelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y3I7qY4LBfChjdYAUlJ1eDh23YkcJRwSRfa6s2wavoI=", "manager_relationship"=>{"password"=>"[FILTERED]", "department_id"=>"1"}, "commit"=>"Manage Bar"}
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."remember_token" = '9J1RAJy16Tooz5wMmzQohw' LIMIT 1
[1m[36mDepartment Load (0.0ms)[0m [1mSELECT "departments".* FROM "departments" WHERE "departments"."id" = ? LIMIT 1[0m [["id", "1"]]
Redirected to http://localhost:3000/departments/1
Completed 406 Not Acceptable in 91ms (ActiveRecord: 0.0ms)
First, i can't see in your views where do you show the flash. You should do something like this in the view you want to display the message:
<% if flash[:success] %>
<p>
<%= flash[:success] %>
</p>
<% end %>
<% if flash[:error] %>
<p>
<%= flash[:error] %>
</p>
<% end %>
Second, when you do a redirection you should use flash[:success] = 'message' instead of flash.now[:success]. Use flash.now is you are not redirecting the user but just rendering a view.
Hi I've set set of an appreciation controller that handles user's liking different posts. Each post has a like button and when I click it looks like the request goes through but the page doesn't refresh and update the button.
When I click like this is the log, it shows the unlike partial being returned but nothing changes:
Started POST "/appreciations" for 127.0.0.1 at 2011-04-22 05:47:28 -0700
Processing by AppreciationsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zQQJeXZiAPFeQ/7AEy9hvQac01+jq929XUXHrd6eSOE=", "appreciation"=>{"liked_id"=>"3"}, "commit"=>"Like"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 4) LIMIT 1
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE ("posts"."id" = 3) ORDER BY posts.created_at DESC LIMIT 1
SQL (0.5ms) INSERT INTO "appreciations" ("created_at", "liked_id", "liker_id", "updated_at") VALUES ('2011-04-22 12:47:28.642264', 3, 4, '2011-04-22 12:47:28.642264')
Redirected to http://localhost:3000/posts/3
Completed 302 Found in 185ms
Started GET "/posts/3" for 127.0.0.1 at 2011-04-22 05:47:28 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"3"}
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE ("posts"."id" = 3) ORDER BY posts.created_at DESC LIMIT 1
User Load (1.2ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 4) LIMIT 1
Appreciation Load (0.3ms) SELECT "appreciations".* FROM "appreciations" WHERE ("appreciations".liker_id = 4) AND ("appreciations"."liked_id" = 3) LIMIT 1
CACHE (0.0ms) SELECT "appreciations".* FROM "appreciations" WHERE ("appreciations".liker_id = 4) AND ("appreciations"."liked_id" = 3) LIMIT 1
Rendered posts/_unlike.html.erb (49.4ms)
Rendered users/_like_form.html.erb (77.7ms)
Rendered posts/show.html.erb within layouts/application (208.9ms)
Completed 200 OK in 248ms (Views: 212.4ms | ActiveRecord: 5.2m
appreciations controller
class AppreciationsController < ApplicationController
before_filter :authenticate_user!
def create
#post = Post.find(params[:appreciation][:liked_id])
current_user.like!(#post)
redirect_to #post
end
def destroy
#post = Appreciation.find(params[:id]).liked
current_user.unlike!(#post)
redirect_to #post
end
end
_like_form.html.erb
<% unless current_user?(#user) %>
<div id="like_form">
<% if current_user.likes?(#post) %>
<%= render 'posts/unlike' %>
<% else %>
<%= render 'posts/like' %>
<% end %>
</div>
<% end %>
_like.html.erb
<%= form_for(current_user.appreciations.
build(:liked_id => #post.id),
:remote => true) do |f| %>
<div><%= f.hidden_field :liked_id %></div>
<div class="actions"><%= f.submit "Like" %></div>
<% end %>
_unlike.html.erb
<%= form_for(current_user.appreciations.find_by_liked_id(#post),
:html => { :method => :delete },
:remote => true) do |f| %>
<div class="actions"><%= f.submit "Unlike" %></div>
<% end %>
Should the result of POST "/appreciations" be a redirect to http://localhost:3000/posts/3? You're using the :remote => true option of the form so I believe you need to change your controller to this:
respond_to do |format|
format.html { redirect_to #post}
format.js
end
And create a *.js.erb partial that does something like this:
<% unless current_user?(#user) %>
<% if current_user.likes?(#post) %>
$("#post_form").html("<%= escape_javascript(render('posts/unlike'))%>");>
<% else %>
$("#post_form").html("<%= escape_javascript(render('posts/like'))%>");
<% end %>
<% end %>
Basically I believe you're seeing no change because you're making an AJAX POST, but not doing anything with the result. You would need JavaScript that updates the innerHTML of a DOM element with the result of your post.
This post might be helpful: http://www.stjhimy.com/posts/7-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript