Rails 3.1 link_to not showing confirmation or destroying properly - ruby-on-rails-3.1

I've been plowing through the chapters at railstutorial.org and been using Rails 3.1.3 because I'm crazy and/or wanted a challenge. I managed to figure out most version problems easily but this one stumped me for a while.
In 10.4.2, Michael Hartl uses the following code to delete users:
<%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
:title => "Delete #{user.name}" %>
It doesn't work properly if you test it in the browser (chrome) and instead sends you to that user page.
It is supposed to work if you include this:
<%= javascript_include_tag :defaults %>
but it fails with Rails 3.1 (it should work for Rails 3.0 though, or so I hear).

So for all of you pulling out your hair for using Rails 3.1, here's the solution.
<%= javascript_include_tag "application" %>
Using "application" instead of :defaults solves this problem, delete and confirm should work, now get back to coding!
Special thanks to George Shaw for this answer over on https://stackoverflow.com/a/8350158/1127011 .
And it case you were wondering, title is for mouseover only.

The a HTML tag will always fire the GET method. Rails uses the Javascript driver to replace the HTTP verb. The link_to method will fallback any method (post, put and delete) to get (corresponding to the show action) when :
Javascript has been disabled by the user
for some reason, Rails unobtrusive javascript driver is not handling the link properly
See http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
I suspect the second reason to be the issue. Make sure the jquery.js, application.js, jquery_ujs.js file are included.
Instead of link_to, you could try to use button_to which creates a form allowing put, post and delete methods without Javascript enabled.

Related

Carrierwave for image upload with ruby on rails

I am trying to upload image using carrierwave. Everything seems to be correct, but I am repeatedly getting an error stating:
Can't resolve image into URL: undefined method `to_model' for #<PortfolioUploader:0x007f35a5cad3c0>
Did you mean? to_xml
CarrierWave with Rails 5.2 ActiveStorage may have some issues.
You could try to change your classic image_tag.
Assuming you're using something like this :
<%= image_tag(#user.image) %>
Change it to something like this, using .url
<%= image_tag(#user.image.url) %>

How make rails forms submit if using rails-api gem. Undefined method `protect_against_forgery?

I have a rails application which is api only i am using gem rails-api. Now I am trying to create admin panel on it because we realised later that we do need an admin panel. But i think rails-api doesnt have good support for views.
When I try to submit forms it says undefined method protect_against_forgery? maybe because it doesnt supports <%= csrf_meta_tags %>
I tried to define manually this method in my controller but no luck.
def protect_against_forgery?
true
end
How can i submit the forms without getting this error.
in your api controller put following line.
skip_before_filter :verify_authenticity_token
or try with
def protect_against_forgery?
false
end
this may be help you.
According to rails-api, try to set config.api_only = false to config/application.rb file
Or, since protect_from_forgery is a method that belongs to ActionController, try to add require "action_controller/railtie" to application.rb

Error after installing devise in rails

I installed devise for rails 4 and followed all the necessary instructions.
I have the root 'home/index'.
In my home/index view erb is the following.
<%= link_to 'Sign In' , new_user_session_path%><br>
<%= link_to 'Sign Up' , new_user_registration_path%>
So when I click on the Sign_in link. I get the following error.
Errno::ENOENT in Devise::RegistrationsController#new
No such file or directory - getcwd
There was a similar question asked before but did not solve my problem. This problem only started coming up recently and never occured when I used rails for my other apps.
did you generate the devise views?
rails generate devise:views

Devise logout not working, giving routing error

<%= link_to "Sign Out", destroy_user_session_path,:method => :delete%>
destroy_user_session DELETE /users/logout(.:format) devise/sessions#destroy
Routing Error
No route matches [GET] "/users/logout"
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
Your link_to tag includes :method => :delete as it should but your error message says that the request is being made with the GET method. This won't work, since the route is only for DELETE requests.
Did you click the "sign out" link and immediately get the error message? I'd expect that your answer is no. It's more likely that you're trying to visit /users/logout directly in your browser, without using the link. That would make it a GET request instead of DELETE.

Rails app routes not available in engine

I'm having the opposite problem of other related posts here. I'm using a gem that renders a page that uses the application's layout. That layout works fine in the application otherwise. In that layout is a named path, login_logout_path, that causes an undefined local or method error.
Here is a snippet of the view code:
<%= link_to( ApplicationName + ' Home', root_path ) %> |
<%= link_to('Sign Off', login_logout_path ) %> :
<%= get_session_login.iname %>
root_path works fine, logon_logout_path fails.
On complication might be that the layout above and the logon_logout_path is supplied by another gem that contains common code/layouts for the set of web sites.
Short Answer, change
login_logout_path
with:
main_app.login_logout_path
and add main_app to all your routes in the application.html.erb that go to your main app
Long answer:
The problem is that engines can have routes with the same name as a route in your main app and work without crashing, if you want to use a route of your main app inside your engine you have to specify it putting
main_app.
Before the route, and if you want to use a route of your engine in your main app you have to specify it with
engineName.example_path
The root_path works because your engine has defined a root_path, and it's send you to the root path of the engine, not the one in your app, you can fix it with adding main_app before it too.

Resources