For my ruby on rails application the destroy method doesn't work.
This is how the controller.rb looks like:
def destroy
#post = Post.find(params[:id])
#post.destroy!
redirect_to '/posts/new(.:format)'
end
The show.html.erb:
<div class="btn">
<%= link_to "Delete", :method => :delete %>
</div>
And the application.js:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
When the post url is http://localhost:3000/posts/bestpostever
and I click the delete button, I get directed to http://localhost:3000/posts/bestpostever?method=delete but that's it. The post still gets shown.
Related
I need to require both in my application.js file. The problem is if one is above another, parts of my application break. But I need both of them working.
E.G. If I require jquery over jquery_ujs, my log out functionality stops working and I receive a No route matches [DELETE] "/users/sign_out" error.
E.G. Vice Versa, if jquery_ujs is over jquery, some links from devise such as 'destroy' or 'edit' completely disappear.
I believe this to be a problem with devise, but I am unsure what I should do.
Application.js:
//= require jquery_ujs
//= require jquery
//= require bootstrap
//= require turbolinks
//= require masonry/jquery.masonry
//= require_tree .
_header.html.erb:
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "About", about_path %></li>
<% if user_signed_in? %>
<li><%= link_to "New Pin", new_pin_path %></li>
<li><%= link_to "Account Settings", edit_user_registration_path %></li>
<li><%= link_to "Log out", destroy_user_session_path, method: :delete %></li>
<% else %>
<li><%= link_to "Sign in", new_user_session_path %></li>
<% end %>
</ul>
Update: Obviously, when I logout, it doesn't work and gives the error No route matches [DELETE] "/users/sign_out. However, if I refresh the page I get a successful logout :/ everytime.
it seems like you are adding jquery_ujs before jquery. so you need to add jquery first something like
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require masonry/jquery.masonry
//= require_tree .
Try this, paste this code into devise.rb
config.sign_out_via = :get
I am using an emailer with ruby on rails and it has a flash notice on completion. I cannot seem to delete the bootstrap flash notice when I Press the x button listed next to the flash notice. I have tried jquery.
Whenever I put the class close on the button. the button does not even show up to even be clicked. Here is some of my code.
This is my create.html.erb
<div>
<% flash.each do |key, value| %>
<div class="alert-dismissible flash_notice" role="alert">
<button type="button" data-dismiss="alert" aria-label="Close">xxx<span aria-hidden="true">×</span></button>
<%= value %>
</div>
<% end %>
</div>
This is my contact controller that emails me from a contact me form and renders the flash notice
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
#contact.request = request
if #contact.deliver
flash.now[:notice] = 'Thank you for your message. We will contact you soon!'
else
flash.now[:error] = 'Cannot send message.'
render :new
end
end
end
Here is some of my application.js code... it is messy and I have tried all of the following and left them all up to show you what I have done so far in my attempts.
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
$('.delete_notice').click(function() {
$('.alert-dismissible').hide();
});
$('.delete_notice').click(function() {
$('.alert-dismissible').close();
});
$('.delete_notice').click(function() {
$('.flash_notice').hide();
});
$('.delete_notice').click(function() {
$('.delete_notice').close();
});
I am trying out react and Rails. I have a very simple rails app with one Model called "Story".
I installed the react-rails gem:
gem 'react-rails', '~> 1.0'
and followed the suggested installation procedure.
I have scaffolded the story model and made the the 'new' method to root in my routes.rb file. All works fine.
I have added a #stories instance to my 'new' method in storiescontroller that holds all records from the database:
# GET /stories/new -- file: stories_controller.rb
def new
#stories = Story.all
#story = Story.new
end
and in my view I added this line of code
# -- file: new.html.erb
<%= react_component('Story', { :data => #stories }) %>
and in my javascript file I have this code:
# -- file:assets/javascripts/components/story.js.jsx
var Story = React.createClass({
displayName: 'Story',
propTypes: {
title: React.PropTypes.string,
description: React.PropTypes.node
},
render: function() {
return (
<div>
<div>Title: {this.props.title}</div>
<div>Description: {this.props.description}</div>
</div>
);
}
});
I thought this should work. But it don't. When I replace the code in the view page with this:
<%= react_component('Story', { :title => "my title", :description=> "my description" }) %>
Then both the 'title' and 'description' are rendered correctly.
It seems that the #stories instance from my view is not parsed correctly to the react component. Then I tried the old-fashion way with
<% #stories.each do |story| %>
<div class="panel">
<h3><%= story.title %></h3>
<p><%= story.description %></p>
</div>
<% end %>
and that works OK. So no problems with the #stories instance. It holds all records and is accessible form the new.htm.erb file.
I am loading the react files after turbolinks in application.js. And settled with this code
# -- file: application.js
//= require jquery
//= require jquery_ujs
//= require foundation
//= require turbolinks
//= require react
//= require react_ujs
//= require components
//= require_tree .
$(function(){ $(document).foundation(); });
I can't find the answer on the internet so i hope someone can help me with my first attempt to get react running in my rails environment.
If #stories is an array and passed like this
<%= react_component('Story', { :data => #stories }) %>
then that array will be available on a prop named data, ie., this.props.data will be the array of stories. There is no magic unpacking going on here which will allow you to access each storys properties.
<% #stories.each do |story| %>
<%= react_component('Story', { :story => story }) %>
<% end %>
will allow you to access your data like so this.props.story.title and also render a div for each story you have in your array
I have this code...
<%= simple_form_for my_object, url: object_path(object_id), html: {class: "form account-form"}, remote: true do |f| %>
<div class="row">
<div class="col-md-4">
<%= f.input :numero_matricula, label: "Núm. de Matricula" %>
</div>
<div class="col-md-4">
<%= f.input :rg, label: "Rg", required: true %>
</div>
<%= f.button :submit, class: 'btn-default', disabled: false %>
<% end %>
When i click at the submit button, the request is html, but i need to call the update.js.erb. In my applicatin.js i have this:
//= require jquery
//= require jquery_ujs
//= require icheck
//= require chosen
//= require jquery.mask
//= require jquery.maskMoney
//= require bootstrap
//= require smart_listing
//= require bootstrapValidator
//= require bootstrap-datetimepicker
//= require locales/bootstrap-datetimepicker.pt-BR
//= require editable/bootstrap-editable
//= require editable/rails
//= require_tree .
This is the controller:
class FuncionariosController < ApplicationController
respond_to :html, :json, :js
def update
#Object = Funcionario.find(params[:id])
#object.update_attributes(params[:object])
end
end
does anyone know what I'm doing wrong?
If you need more information I am available...
I am trying to delete a record using :method => :delete but its calling GET instead. It redirects to show and displays 404 not found. But if i go back and refresh, the record is acctualy deleted.
<% #photos.each do |photo| %>
<div class='photogallery'>
<%= link_to image_tag(photo.image_url(:thumb)) if photo.image? %>
<div class="name"><%= photo.caption %></div>
<div class="actions">
<%= link_to "edit", edit_admins_photogallery_path(photo) %> |
<%= link_to "remove",admins_photogallery_path(photo), :method => 'delete',:confirm => 'Are you sure?' %> |
</div>
</div>
<% end %>
application.js
//= require jquery
//= require jquery_ujs
//= require jquery-fileupload/basic
//= require jquery-fileupload/vendor/tmpl
//= require dataTables/jquery.dataTables
//= require bootstrap
//= require bootstrap-select
//= require jquery.timepicker
//= require jquery.ui.tabs
//= require jquery.ui.datepicker
//= require jquery.ui.accordion
//= require jquery.ui.autocomplete
//= require strftime-min.js
//= require turbolinks
//= require_tree .
i have included in my layout file and its been loaded fine.
<%= csrf_meta_tag %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
The weird part is it used to work fine few days back,now it has stopped working for all the controllers.
edit: routes.rb
devise_for :users
root 'index#index'
resources :reprint
devise_scope :user do
get "/admins/", :to => "devise/sessions#new"
end
namespace :admins do
resources :location,:dashboard,:lodge,:room,:booking,:rate_calendar,:photogallery
end
resources :index do
collection do
match 'search' => 'index#search', via: [:get, :post], as: :search
match 'location' => 'index#location', via: [:get, :post], as: :location
match 'add_cart' => 'index#add_cart', via: [:get, :post], as: :add_cart
end
end
resources :room_availability
edit: photogallery_controller.rb
def destroy
#photos = Photogallery.find(params[:id])
if #photos.destroy
redirect_to admins_photogallery_path, notice: "Photo was successfully destroyed."
else
render :action => 'index'
flash[:error] = "Photo could not be deleted."
end
end
I ran across the same problem and when I changed link_to to button_to everything started working correctly. Maybe because link_to is mainly used for GET calls and button_to for POST calls (in my case DELETE also).
def destroy
#photos = Photogallery.find(params[:id])
if #photos.destroy
redirect_to admins_photogalleries_path, notice: "Photo was successfully destroyed."
else
render :action => 'index'
flash[:error] = "Photo could not be deleted."
end
end
I believe you have it set up correctly, you just made a typo in the redirect_to call. admins_photogallery_path is the show page. admins_photogalleries_path is the index page. There's no way you're making a GET request and reaching the destroy method unless you specified that specifically in the routes. The GET and DELETE verbs both talk to the same path - if you were indeed using a GET request, you'd simply go to the show page and nothing would happen. That anything got deleted at all suggests that the DELETE request is being made successfully.