I need to add an id element into a session array in my rails application. I have the id when I bring up this view, product.id, and i have my session, called session[:cart], waiting for input.
I want to push that id into the session when I click on the link below.
<%= link_to "Add to Cart", controller: "my_cart" %>
Is there more to this link I add? i have been looking all over the internet and have not come up with exactly what I'm looking for.
Please help
You need to pass your product.id to some controller/action where that controller/action will add the product id to the session. It cant be done on the browser side as Sessions are server driven
<%= link_to "Add to Cart", add_product_to_cart_path(:product_id => product.id) %>
and method would be something like
def add_product_to_cart
session[:cart] << params[:product_id]
end
what you can do is make a action in controller where you can put product.id in your session[:cart] and put the path of that action into link_to for example:-
<%= link_to "Add to Cart",your_action_path %>
and to see the path of your action you can run rake routes in terminal, just put that path in there and it should do it
Related
I have the following in my projects_controller.rb:
def destroy
#project = Project.find_by_slug(params[:id])
#project.destroy
redirect_to projects_url
end
And I have the following in my routes.rb file:
delete "projects/:id", to: "projects#destroy", as: "destroy_project"
I have the following link (inside the show.html.erb file):
<%= link_to destroy_project_path(#project), method: :delete, class: "btn-gradient btn-red" do %>
<span>Delete Project</span>
<% end %>
Upon clicking the button, the page reloads. The show action is called upon clicking the button. I've added console logs in each method, and it is clear that the destroy action is never called.
Can anyone point me in the right direction?
the link_to helper receives 2 different sets of hashes for the options.
The first set is for things like the http method, and the second for the html attributes (class, id and so on)
The way you wrote it, you probably have method=delete in your query params, which is wrong. You have to explicitly enclose the method: :delete within its own options hash:
<%= link_to destroy_project_path(#project), { method: :delete }, class: "btn-gradient btn-red" do %>
<span>Delete Project</span>
<% end %>
If you use rails 7 with turbo framework you can try below for buttons.
<%= button_to "Delete this project", destroy_project_path(#project), method: :delete %>
Or you can try below for links.
<%= link_to destroy_project_path(#project.id) , data: { turbo_method: :delete } do %>
<span>Delete Project</span>
<% end %>
the problem that you are having is with the JS. there is a problem where something is not loading properly.
For testing try to remove your JS and use something like this
<%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js" %>
When you get the delete method working, then you can debug where in your JS there is a problem.
at this time there is not sufficient info on your question to be able to know what it is.
I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."
Im trying to make an app with Rails 4.
I have a projects model and profiles model.
My objective is to make the name of the project creator a link to the profile page of the user who made the project.
In my projects view, I have:
<%= link_to '
<%= #creator_profile.title %> <%= "#{#creator.first_name} #{#creator.last_name}" %>
', #creator_profile_path %> </span>
In my projects controller, I have defined creator as the user who creates the project (so the link doesnt go to the user profile that is logged in):
def show
#authorise #project
#project = Project.find(params[:id])
#creator = User.find(#project.creator_id)
#creator_profile = #creator.profile
end
My attempt is incorrect, but I can't figure out how to link these. Can anyone see what I've done wrong?
<%= link_to "#{#creator_profile.title} #{#creator.first_name} #{#creator.last_name}", #creator_profile %>
You just want one string interpolation. You don't need the extra erb tags as you already have them on the link_to. The alternative way would be to pass a block to link_to
<%= link_to #creator_profile do %>
<%= "#{#creator_profile.title} #{#creator.first_name} #{#creator.last_name}" %>
<% end %>
I assume you have routes setup so that rails can correctly "guess" the profile link based of the profile object.
I'm having some trouble understanding the redirect_to statement.
I have a model "Book" (with an boolean attribute "read")and a controller "books". Now I created a second controller "Admins" having to methods: index and change.
The index view just renders a list off all Books with a link to the change method:
<% #Books.each do |d| %>
<%= d.title %><br>
<% if d.read==true %>
<%= link_to "mark unread", change_path(:id=>d.id)%>
<% else %>
<%= link_to "mark read", change_path(:id=>d.id)%>
<%end %>
Now the change method just changes the "read" attribute:
#book=Book.find(params[:id])
if #book.read==true
#book.update_attributes(:read => false)
else
#book.update_attributes(:read => true)
end
redirect_to action: "index"
The Problem is: rails tries to redirect me to the show action using the :id as a parameter...(perhaps because the change_url is /admins/change?id=3)
But I just want to be directed back to the index view "/admins"
is there a way? it seems like rails always tries to redirect to the view action if there is an id as a parameter
Thanks alot
PS: the routes.rb contains resources:admins and resources:books
Use this
redirect_to :controller => 'admins', :action => 'index'
Or
redirect_to admins_url
The above two will direct you to the index page of AdminsController. There is no way that Rails would route it to show action UNLESS you are redirecting to show action from the index action of AdminsController. Since, you did not share index action code of AdminsController, I would recommend you to check there.
If you want a clear explanation of redirect_to ... checkout
https://gist.github.com/jcasimir/1210155
I had a kind of similar issue some days ago. I would suggest to do this within the form where you list the books and the mark/unmark checkboxes.
<%= form_for #book,:url => book_index_path do |f| %>
This worked fine for me, when I set up a site where you create data and the user is immediately redirected to the same page (incl. success/error message).. to do a kind of
human batch-processing.
I have on my holder (think of it as a relationships table holding all the questions that belong to a certain holder and the holder specific data like name) show page a list of questions and a link to New Question link.
<% #questions.each do |question| %>
<%= question.question %>
<%= question.answer %>
<%= link_to "Edit Question", edit_question_path(question) %>
<br />
<% end %>
<%= link_to "New Question", new_question_path %>
The goal of this is to set the holder_id when the new question is created. I have the belongs_to and has_many setup in the model if that matters.
I've tried a few different things such as <%= link_to "New Question", new_question_path(#holder) %> but that just sets the format: to the #holder.id. I suppose, I could abuse that to make it work, but that's a very ugly hack.
I've heard people over use nested routes, and not to go more than 2 or 3 deep. So there's got to be a way to do this without using nested routes.
Am I correct in that there is a way to do this without using nested routes? What is it?
Why would nested routes be a good or bad idea for this example?
I suppose you can do something like this.
In your link to new:
<%= link_to "New Question", new_question_path(:holder_id => #holder.id)
This link will make your request send holder_id as a parameter to the new action in your question_controller.rb. So you can use it like this:
def new
holder_id = params[:holder_id]
# Do something with this id
# ...
end