save selection in a cookie - ruby-on-rails

I trying to save the user seleccion in a cookie but I think I'm doing something wrong.
This is link in my view
<%= link_to image_tag("venezuela.png", :height => '74', :width => '111'), {:controller => "landing", :action => "select_country", :country => "venezuela"}, :method => "get" %>
Here is the action of my controller
def select_country
cookies.permanent[:country] = params[:country]
case params[:country]
when "venezuela"
redirect_to "google.co.ve"
end
end
When I make click in the link a get this error:
Unknown action
The action 'show' could not be found for LandingController
and goes to this url
http://localhost:3000/landing/select_country?country=venezuela
Thanks in advance for your help.

Looks like you have a routing problem. Make sure you don't have a route that assumes all links going to /landing/:anything are for the show action ahead of the route that defines select_country as an action on that controller.

Related

How to make a custom route in Rails? By custom I mean one that reacts to params

So essentially I've setup a route to match "products/:product", which seems to respond to a page like baseurl/products/toaster and displays the toaster product. My problem is I can't seem to use link_to to generate this path, and by that I mean I don't know how. Any help on this?
There are several solutions on this one :
<%= link_to 'Toaster', { :controller => 'products', :action => 'whatever', :product => 'toaster' } %>
But it's not really Rails Way, for that you need to add :as => :product at the end of your route. This will create the product_path helper that can be used this way :
<%= link_to 'Toaster', product_path(:product => 'toaster') %>
Within your routes file you can do something like:
match "products/:product" => "products#show", :as => :product
Where the controller is ProductsController and the view is show
within the Products controller your have
def show
#product = Hub.find_by_name(params[:product])
respond_to do |format|
format.html # show.html.erb
end
end
Where whatever is in the products/:product section will be available via params.
Then, since we used :as in your routes you can do this with link_to:
<%= link_to product(#product) %>
Where #product is an instance of a product or a string. This is just an example and the param can be anything you want, the same goes for controller/action. For more info you should check out this.
Hope this helps!

RAILS3: Pass arbitrary parameters via button_to?

I'm trying to do something very simple in my first Rails app (Rails 3) and I'm not sure what I'm doing wrong, or if there's a better approach. Can't find anything on the web or here that has solved it for me despite much searching.
In the app I have WorkRequests and Articles. When viewing an Article, I want a button to create a WorkRequest and, when the new WorkRequest form appears, have the article filled in. Essentially, I'm trying to pass the Article.id to the new WorkRequest.
Works in link_to just by adding the parameter, but I want it to be a button. While it shows up in the Article form's HTML as a query parameter, it never gets to the WorkRequest.new method. This article from 2010 explains the problem in some detail, but the solution does not work for me (see my comment at the end of the page.)
This seems like it should be a fairly easy and common thing to do (once I figure it out, there are several other places in my own app where I want to do the same thing) but I've been banging my head against this particular wall for a few days now. I am new to Rails--this is my first app--so I hope someone more experienced can help!
Thanks in advance.
Just circling back to finish this up. Ultimately I solved this by using link_to but using jQuery to make it look like a button. #kishie, if you're saying you made this work with button_to I'd like to see the code, but as I like jQuery it's solved as far as I'm concerned (for this app, anyway.)
Here's the code in Article#show view. (The class is what makes it look like a button via jQuery.)
<%= link_to "New Request", new_work_request_path(:article_id => #article.id), :class => "ui-button" %>
Here's the code in Work_Request controller's new method:
if !params[:article_id].blank?
#work_request.article = Article.find(params[:article_id])
end
Then the Work_Request#new view renders it properly.
Add this line of code in your routes.rb file.
resources :work_requests do
member do
post 'new'
end
end
It shouldn't be the GET method because you're sending information to the server, via :module_id. This will then work.
<%= button_to("Add WorkRequest", {:controller => "work_request", :action => "new", :article_id => #article.id})%>
I just hit a similar issue and I got it to work by passing the parameter as follows:
<%= button_to("Add WorkRequest", new_work_request_path(:article_id => #article.id), :action => "new", :method => :get)%>
In article#show
<%= button_to("Add WorkRequest", {:controller => "work_request", :action => "new", :article_id => #article.id})%>
In work_requests#new
<%= f.text_field :article_id, :value => params[:article_id]%>
If you nest your resources for :work_requests within :articles in your routes.rb, then pass your params[:id] which would be your article_id and add :method => :get to the button_to call, you should be okay.
# config/routes.rb
resources :articles do
resources :work_requests
end
# app/views/articles/show.html.erb
<%= button_to "Add Work Request", new_article_work_request_path(params[:id]),
:method => :get %>
# app/controllers/work_requests_controller.rb
class WorkRequestsController < ApplicationController
...
def new
#work_item = WorkItem.new
#article = Article.find(params[:article_id])
...
end
...
end

Rails 3: ":method => :post" doesn't work... seems to be 'GET' when it should 'POST'

I'm trying to implement the "Friendship" in my Rails 3 app as described in Railscast 163:Self Referential Assosication
I have everything set up as described. I am using a basic user model that logis in with Authlogic which works fine. However when I try to add a friend using the following link:
<% for user in #users %>
<%=h user.username %>
<%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post %>
<% end %>
I get a redirect to http://localhost:3000/friendships?friend_id=2 and a Unknown action The action 'index' could not be found for FriendshipsController error with no further explanation. This is expecially strange since I have a hard coded redirect back to the "User#show" method for my current user (i.e. redirect back to profile after adding friend).
If it helps, here is my "friendships#create" method:
def create
#friendship = current_user.friendships.build(:friend_id => params[:friend_id])
if #friendship.save
flash[:notice] = "Added friend."
redirect_to :controller => 'users', :action => 'show', :id =>'current_user'
else
flash[:notice] = "Unable to add friend."
redirect_to :controller => 'users', :action => 'show', :id =>'current_user'
end
end
Any idea what could be causing this? I found someone having a similar problem here but I couldn't find a definite fix: Rails 3 and Friendship Models
Thanks in advance for your help!
~Dan
I think that link_to put the arguments into the query string as it is creating with html link even if you put :method => :post if js is disabled.
you could simulte a post with javascript :onclik event.
aniway , use a link_to with method :post is generaly a bad idea.
in rails you could use button_to helper for this pourpose and style it like a link.
edit:
In Rails3 doc seems that link_to have to simulate the same behaviur of button_to when called with params :method=>:post
(dynamically create an HTML form and
immediately submit the form ).
but it's not true for me in Rails 3.0.3 even if javascript is enabled.
I will investigate.
anyway you should be using buttons and forms for anything that isn't a GET; hyperlinks intentionally don't allow for methods other than GET
edit2:
ok,
rails3 don't create a inline form for simulate the post request via link. in Rails3 a data-method=post attribute is added to the tag for manipulate it via javascript function. this way the request gracefully degradate in a get call if js is disabled.
It's a late answer but it's a solution to this problem (at least it works for me):
<%= link_to "Add Friend", {:controller => :friendships, :action => :create, :friend_id => user_id }, :method => :post %>
I know it's long overdue for your problem, but it may help someone :)

link_to_remote wants to render a template - how not to? rails

I want to delete my task ajax-style if some conditions are met. I do that with the help of link_to_remote. The thing is that link_to_remote wants to render a template and i dont want it to.
In my view ( _task_sth.html.erb):
<%= link_to_remote "Delete task", :url => {:controller => 'tasks', :action => 'delete_task', :task_pers_id => sorted_user_task.id}, :complete => "$('#{delete_task_domid}').hide();" %>
In my controller (tasks_controller.rb):
def delete_task
task_pers = TaskPersonalization.find(params[:task_pers_id])
horse_task = task_pers.task
task_pers.destroy
if horse_task.task_personalizations.empty?
horse_task.destroy
end
end
The task gets deleted but i get an error saying: Missing template tasks/delete_task.erb in view path app/views.
How can i make it not search for a template?
I tried with adding :method => :delete at the end of link_to_remote and changing my action name to destroy. I also added render :nothing => true. I also played with routes a bit. But still i allways get the same error.
The question is how can i make it not search for a template because i dont want it to render anything?
Would be very gratefull for any answers, Roq.
Have you got the same error when adding render :nothing => true? That's strange.
Rails is trying to search for a template because there's no render in your action. So to avoid it, you need to explicitly call the method:
def delete_task
task_pers = TaskPersonalization.find(params[:task_pers_id])
horse_task = task_pers.task
task_pers.destroy
if horse_task.task_personalizations.empty?
horse_task.destroy
end
render :nothing => true, :status => 200
end

link_to for calling an action, instead of a button_to

I'd like to call an action with a link_to button without being redirected to another page(I don't want ajax). For you to have an idea, I'm trying to accomplish a sort "link button" in a search page. So, when the link is clicked, the page should be refreshed showing the list ordered as I tell it in the action.
If I do the following in my view, it will ask me for a template called as the action, and I don't want it:
<%= link_to 'MÁS RELEVANTES', search_filter_relevance_path %>
My routes file looks like this:
map.search_filter_relevance "/anuncios/buscar", :controller => 'announcements', :action => 'search_filter_relevance'
EDIT:
If I change the above line to:
map.search_filter_relevance "/anuncios/search_filter_lowest_price", :controller => 'announcements', :action => 'search_filter_relevance'
it access to the action I want it to access, but I can't figure out a way to pass the collection of items I have in the view(it's a search result) to the action, so I can filter them.
And my controller looks like this(I haven't developed the functionality yet):
def search_filter_relevance
end
Any help on this will be appreciated.
I believe you should define what you'll render... an action, a partial, nothing?
def search_filter_relevance
render :action => "some_action"
# or
render :partial => "some_partial"
# or
render :nothing => true
end
Something like that...
Maybe you could explain what you want this action to do exactly...

Resources