Delete action without scaffolding - ruby-on-rails

Status resource (generated by "rails g scaffold Status")
resources :statuses
Link destroy object
<%= link_to "delete", status, :confirm => "are you sure?", :method => :delete%>
layout.html.erb
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Application.js
//= require jquery
//= require jquery_ujs
Rails 4.0.0
When I click on this it redirect me to Rails'scaffold page (Statuses' listing). How I could redirect user to homepage, or better, on previously page?

in statuses_controller.rb you should have destroy action
def destroy
#status = Status.find(params[:id]) # or find(params[:status_id]) depends on how route is built
#status.destroy
respond_to do |format|
format.html { redirect_to statuses_url }
format.json { head :no_content }
end
end
change the redirect_to statuses_url to redirect_to :back it will get you back from where your request started or redirect_to root_url for home page.

Related

Redirect http request to ajax

My app has a modal sign in form that I display using ajax:
<% if !current_user %>
<%= link_to "Sign in", signin_path, remote: true %>
<% end %>
routes.rb
get 'signin', to: 'static_pages#signin', as: 'signin'
static_pages_controller.rb
def signin
respond_to do |format|
format.html { render :action => 'signin.js.erb' }
format.js { render :action => 'signin.js.erb' }
end
end
signin.js.erb
$('#modalPlaceholder').html("<%= j render(partial: '/static_pages/signin') %>");
$('#modalPlaceholder').show();
This works well.
I would now like to intercept actions that require user sign in and display this modal. In my application controller I have:
application_controller.rb
def require_signin!
if current_user.nil?
render :action => '../static_pages/signin.js.erb'
end
end
helper_method :require_signin!
This works when the originating action is remote: :true and rails simply redirects it however when the originating action is a standard request the action is intercepted correctly by the application controller however the modal is displayed on screen in raw html and not loaded correctly.
You can check format type of request and render for it.
def require_signin!
if current_user.nil?
if request.format.html?
#html render file here
else
render :action => '../static_pages/signin.js.erb'
end
end
end

rails simple ajax request

i have a problem with ajax request:
in my app i am trying to configure a simple rank system, and i set it up, but whan i click on rank button - i reload the page and rank is refreshed.
Help my realize that in ajax:
i done that:
in posts/show.html.erb i have that:
<div id='vote_update'>
<%= render 'vote/update' %>
</div>
in vote/_update.html.erb i have :
<% if can? :update, Vote %>
<%= form_for([#post, #post.vote], remote: true) do |f| %>
...
<%= f.submit "vote" %>
in vote/update.js.erb i have:
$('#vote_update').html("<%= j render('vote/update') %>");
and in vote_controller.rb i have:
def update
post = Post.find(params[:post_id])
vote = post.vote
...
respond_to do |format|
if vote.save
format.html {
redirect_to post_path(post),
:notice => "You vote counted!"
}
format.js
end
end
end
if i remove romete: true - everything goes right ( page is reload, i saw (:notice) "You vote counted!" and rating is updated, but if i put remote: true back, i saw nothing ( some mistakes in firebug console ) - but when i reload page - ratign is updated, nd i saw norml result - i think i madesome mistakes in redirecting or i dont know
help please
your ajax code will come in format.js block. where you can update the html where you are showing the rank.
change
respond_to do |format|
if vote.save
format.html {
redirect_to post_path(post),
:notice => "You vote counted!"
}
format.js
end
end
to
if vote.save
respond_to do |format|
format.html {
redirect_to post_path(post),
:notice => "You vote counted!"
}
format.js
end
end
Not sure though

Rails: I want to add index to the resource :symbol route

Users have subscriptions to feeds but when I change (as suggested elsewhere on stackO)
resource :subscriptions
to
resources: subscriptions
it breaks some ajax functionality I already have implemented regarding destroy.
I want to be able to link to /subscriptions/ and have users be able to view all of their subscriptions. The problem is that currently it's bringing me to subscriptions#show when I really want #index.
How should I do this?
Here's my AJAX:
<div id="subscribe" class="shadow">
<% if session[:read_random]
unless is_subscribed?(session[:read_random].last)%>
<%= link_to 'Subscribe', subscriptions_path(feed_id: session[:read_random].last), method: :post, remote: :true %>
<% else %>
<%= link_to 'Unsubscribe', subscriptions_path(feed_id: session[:read_random].last), method: :delete, remote: :true %>
<% end
end %>
</div>
Here's my destroy method
def destroy
if params[:feed_id]
#this is the ajax call
#subscription = Subscription.find_by_user_id_and_feed_id(session[:user_id], params[:feed_id])
else
#this is the index destroy call (unsubscribe)
#subscription = Subscription.find(params[:id])
end
#subscription.destroy
respond_to do |format|
format.html { redirect_to request.referer }
format.js
format.json { head :no_content }
end
end
Using the default resourceful routes for a plural resource, the proper destroy route would be subscription_path(id), method: :delete. Note that subscription is singular. For these routes, the destroy path is generally identical to the show and update path, with the exception of http method used.

Ruby on Rails link_to :method => :delete syntax

This is a nub question. I have a resource Project which has_many Feeds. When I am viewing the list of nested Feeds on the Project page, I have a Delete button to remove that Feed from the Project. It works with this syntax:
<%= link_to 'Delete', [feed.project, feed], :confirm => 'Are you sure?', :method => :delete %>
But that then directs to the page listing all the Feeds, and I instead want it to redirect back to the Project page the user was just at. Is it a matter of changing [feed.project, feed] or is it something else? I don't quite understand the syntax of link_to well enough yet.
EDIT:
In my feeds_controller.rb, I changed the redirect line to :back
def destroy
project = Project.find(params[:project_id])
#feed = project.feeds.find(params[:id])
#feed.destroy
redirect_to :back
respond_to do |format|
format.html { redirect_to :back }
format.xml { head :ok }
end
end
You must have a look at the controller for this resource. It should be somewhere like app/controllers/projects_controller, where's there should be an action named destroy. The code that do the redirect must be in there. You'll have to change the following line:
redirect_to project_feeds_url(project)
to this
redirect_to :back
in your controller
def destroy
#project = Project.find(params[:project_id])
#feed = #project.feeds.find(params[:id])
#feed.destroy
redirect_to #project
end

Why Destroy Action is NOT being fired?

I am using Rails 3 and trying to see why the Destroy action is not being fired! My html fired is defined as follows:
<%= link_to "Delete", :action => "destroy", :id => article, :method => :delete, :confirm => "are u sure?" %>
And here is the ArticlesController:
def destroy
#article = Article.find(params[:id])
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url }
end
end
When I click on the "Delete" link it takes me to the show action. I am not sure why is that?
Your link_to should be this:
<%= link_to "Delete", #article, :method => :delete, :confirm => "are u sure?" %>
This will generate the correct URL for your article and go to the destroy action.
Have you deleted the javascripts directory which resides in the public directory of your rails app?
If yes, create a new rails project and copy the javascripts directory in your actual project
Make sure your application.js has included this line:
//= require jquery_ujs
That solved my issue.
That first line should be
#article = Article.find(params[:id])
Once you make this change, you won't be able to redirect to #article, since it will be gone; you'll probably need to replace that last line with redirect_to articles_url. The whole new method will be:
def destroy
#article = Article.find(params[:id])
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url}
end
end
If you have trouble with stuff like this, try creating a scaffolded model with rails generate scaffold Test to see what the default options are.
It could also be this Why are default javascript files required to create a destroy link in rails?.

Resources