I have a problem that has been documented previously but for which I can't solve in my case. This is about the 5.13 section of Getting Started Rails tutorial that deals with deleting an entry.
First here is my Controller :
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path end
My view
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr> <% end %>
As per this thread : Rails 4 link_to Destroy not working in Getting Started tutorial
I have amended link_to to button_to which DOES work. But this doesn't really explain why the tutorial bit doesn't work.
I have also made sure that the 'jquery-rails' gem was in the gemfile. And I checked aswell the follwing from my app/assets/javascript/application.js file:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
As per this thread Delete link sends "Get" instead of "Delete" in Rails 3 view I checked my app/views/layouts/application.html.erb file which looks like :
<!DOCTYPE html>
<html>
<head>
<title></title>
<%= stylesheet_link_tag 'default', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head> <body>
<%= yield %>
</body>
</html>
I tried in both Chrome and Firefox. Still doesnt work.
If the question is why does it have to be button_to instead of link_to, it is pretty straight forward. Modern browswers do not implement verbs other than GET for links(anchor tags). It has to do with the history of the web and browsers. Rails requires a DELETE verb to look for the destroy action. To get around the constraint, button_to generates a form with the correct verb (method in rails speak), and the correct path.
If you are copying and pasting directly from Rails website, there is a chance you got your indentation mixed. Try cleaning up your code and stick to one indentation style e.g. taps or spaces.
Related
My local version runs CKEditor fine.
Edit: Local version does the same thing.
It's runs on heroku as well, but when I first load my create blog post page, the cktext_area simply shows up as a normal text_area box. When I refresh the page, the ck_textarea shows up like it is supposed to.
I'm not sure why this is happening.
This is using rails 4.
Here's my application layout
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>
Here's my application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require turbolinks
//= require ckeditor/init
//= require_tree .
And my blog create form
<%= form_for #article do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title, :size=>'50' %>
</p>
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I know I'm late to the party, but may be it will help someone.
The problem is that ckeditor isn't initialized after visiting a turbolinks link. So the solution is to reinitialize ckeditor every time turbolinks loads pages.
This could be done using turbolinks callback turbolinks:load (page:load for old versions)
The second problem is that turbolinks:load fires every time even on document ready event. So I have to check somehow if ckeditor is initializes or I'll get The editor instance ... is already attached to the provided element.
The final code will be:
$(document).on('turbolinks:load', function() {
$('.ckeditor:visible').each(function() {
var id = $(this).attr('id');
if (!CKEDITOR.instances[id]) {
CKEDITOR.replace(id);
}});
});
OK. Well. This isn't a complete solution, but removing turbolinks solves this issue. I certainly don't need turbolinks for my project, but it would eventually be nice to find a solution that works including turbolinks.
removed //= require turbolinks from application.js
It would seem CKEditor doesn't play nice with turbolinks. Instead of disabling turbolinks, I simply added a data: { 'no-turbolink' => true } to any link that contained a ckeditor.
For example, in my header I had a link to create a new blog post:
<%= link_to "Create New Blog Post", new_article_path, data: { 'no-turbolink'=>true} %>
This solved it for me, without completely removing turbolinks.
For some reason I am unable to get the correct path for my destroy operation of a very simple model. Are my expectations incorrect?
My routes.rb includes:
resources :designs
And my view contains:
<% #designs.each do |design| %>
<%= link_to "Delete", design, :method => :delete %>
<% end %>
Which results in the HTML:
<a data-method="delete" href="/designs.49" rel="nofollow">Delete</a>
Which of course errors on
"No route for [DELETE] for /designs.49"
When I was expecting the rendered HTML to be:
<a data-method="delete" href="/designs/49" rel="nofollow">Delete</a>
Especially considering rake routes shows me:
DELETE /designs/:id(.:format) designs#destroy
My workaround is to replace: link_to "Delete", design... with: link_to "Delete", "/designs/#{design.id}"... (which works fine), but surely I am overlooking something basic, as no one should have to waste this much time to figure out the absolute most fundamental baseline case for a destroy operation.
Your code in the view could read like this using a _path helper:
<% #designs.each do |design| %>
<%= link_to "Delete", design_path(design), :method => :delete %>
<% end %>
But I guess I can see what you're trying to accomplish. To get the show action, you should be able to do this:
<% #designs.each do |design| %>
<%= link_to "Show", design %>
<% end %>
I wonder if this is a bug in Rails? What happens if you do this?
<% #designs.each do |design| %>
<%= link_to "Delete", url_for(design), :method => :delete %>
<% end %>
Try to replacing this in the helper tag.
<%= link_to "Delete", design_path, :method => :delete %>
I have a mongo rails 4 app, with an embedded photo.
I have no problem in the new form, but in the edit form, if the user has not upload any photo, the form will not display a "add photo button".
Here is the form:
<%= f.fields_for :founder_profile_photos do |founder_photo_f| %>
<%= render partial: 'founder_profile_photo_fields', locals: { f: founder_photo_f } %>
<%= link_to_add_association raw('<i class="fi-plus"> add a founder photo</i>'), f, :founder_profile_photos %>
<% end %>
Here is the partial:
<div class="nested-fields">
<% if f.object.file.to_s.empty? %>
<%= f.file_field :file, label: "Upload a founder photo." %>
<% else %>
<%= image_tag f.object.file, class: 'small-10 medium-10 image-previewer' %>
<% end %>
</div>
Thank you so much for your help. I finally found what was wrong, I needed to build the field photo in the edit action of my controller, other the field does not exist and therefore do not appear in the view.
def edit
if #user.photos.empty?
#user.photos.build
end
end
photo is embedded into the user model (one to many relation) if you have a one to one relation you have to do:
if #user.photo.blank?
#user.photo = Photo.new
end
Thank you very much for all your help, still so much to learn.
I'm in the process of building a rails 4 app and I've run into a snag. When I go to the sign in page and enter the information correctly everything works
If I go to sign in but I don't input one of the required fields (name, email, etc) it will put up an error message saying 'Please review the problems below:' but the specific error messages, such as 'email can't be blank' do not show up.
The errors displayed properly before I added styling..
The code is here:
https://github.com/mikejames386/Black-Fret
I'm currently working off the devise branch.
This is the first time I've ever asked a question on stackoverflow so please let me know if there is any other info needed.
<div class="container">
<div id="form_container"><!-- start form_container -->
<h1>New Artist</h1>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<h2>Artist or Band Name</h2>
<div class="form_section">
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<br class="clear_both"/>
</div> <!-- end form_section -->
<h2>Primary Contact</h2>
<div class="form_section">
<div class="field">
<%= f.label :primary_first_name, 'First Name'%>
<%= f.text_field :primary_first_name %>
</div>
<div class="field">
<%= f.label :primary_last_name, 'Last Name'%>
<%= f.text_field :primary_last_name %>
</div>
<div class="field">
<%= f.label :email, 'Email'%>
<%= f.text_field :email %>
</div>
<br class="clear_both"/>
</div> <!-- end form_section -->
<h2>Password</h2>
<div class="form_section">
<div class="field"><%= f.label :password %>
<%= f.password_field :password %></div>
<div class="field"><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<br class="clear_both"/>
</div>
<h2>Members</h2>
<div class="form_section">
<p>member multi-add goes here</p>
</div> <!-- end form_section -->
<%= f.submit "Add Artist", :class =>'btn btn-custom' %>
<% end %>
</div> <!-- end form_container -->
Errors on Devise log-in page are added as flash messages.
In order to capture them, add following lines in app/views/layouts/application.html.erb
<% flash.each do |name, msg| %>
<%= content_tag :div, msg %>
<% end %>
As per Devise Documentation
Remember that Devise uses flash messages to let users know if sign in
was successful or failed. Devise expects your application to call
flash[:notice] and flash[:alert] as appropriate. Do not print the
entire flash hash, print only specific keys. In some circumstances,
Devise adds a :timedout key to the flash hash, which is not meant for
display. Remove this key from the hash if you intend to print the
entire hash.
I have Devise set up on rails 4 with my app and I actually have a Devise User model, a registration controller (which youdon't need) and a Devise views section. You may want to follow this tutorial https://github.com/plataformatec/devise, if you scroll down a bit you will see configuring views. The below will get put into your user model and the validatable is for validations on email and password which may be why the validations are not working properly just a thought. But I hope this tutorial gives you some guidance.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
After you install Devise and add it to your Gemfile, you need to run the generator:
rails generate devise:install
After you install Devise and add it to your Gemfile, you need to run the generator:
The generator will install an initializer which describes ALL Devise's configuration options and you MUST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator:
rails generate devise MODEL
Replace MODEL by the class name used for the applications users, it's frequently User but could also be Admin. This will create a model (if one does not exist) and configure it with default Devise modules. Next, you'll usually run rake db:migrate as the generator will have created a migration file (if your ORM supports them). This generator also configures your config/routes.rb file to point to the Devise controller.
Configuring views
We built Devise to help you quickly develop an application that uses authentication. However, we don't want to be in your way when you need to customize it.
Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after some time you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application:
rails generate devise:views
I have a thumbnail image that links to a blog post entry which is accompanied by a text title. The CSS shades the image slightly and when the user hovers over the image, it re-sizes the image slightly. The user can click on the title or on the thumbnail to take them to the entry.
I cannot get the overlay tag here:
to be generated within rails link_to
<%= link_to(:class=>"overlay overlay-primary", id: entry) %>
The image is part of a figure
<figure class="entry-thumbnail">
<!-- to disable lazy loading, remove data-src and data-src-retina -->
<% if entry.cover_photo_link.blank? %>
<%= image_tag "placeholder.gif" %>
<% else %>
<%= image_tag entry.cover_photo_link %>
<% end %>
<!--fallback for no javascript browsers-->
<noscript>
<% if entry.cover_photo_link.blank? %>
<%= image_tag "placeholder.gif" %>
<% else %>
<%= image_tag entry.cover_photo_link %>
<% end %>
</noscript>
</figure>
The full thumbnail codes is in this gist
How do I get the image to link to use link_to render this?
The solution was simple. Putting the a blank "" in link_to did the trick
<%= link_to "", entry, :class => "overlay overlay-primary" %>
renders:
<a class="overlay overlay-primary" href="/entries/5"></a>
This worked well.