link-to

Generate a `link_to` to the controller action `edit`, dynamically

戏子无情 提交于 2019-12-21 01:06:15
问题 I am using Ruby on Rails 3.0.7 and I would like to generate a link_to to the controller action edit , dynamically . I have to use it in a partial template but the issue is that I am rendering that same partial template for different model data (that is, I pass local variables of different class instances in that). So I can not use the route "magical RoR way" `edit_<singular_name_of_the_resource>_path(<resource_class_instance>)`. I would like to make something like the following: link_to(

link_to :action => 'create' going to index rather than 'create'

久未见 提交于 2019-12-18 11:55:13
问题 I am building a fairly simple recipe app to learn RoR, and I am attempting to allow a user to save a recipe by clicking a link rather than through a form, so I am connecting the user_recipe controllers 'create' function through a link_to. Unfortunately, for some reason the link_to is calling the index function rather than the create. I've written the link_to as <%= "save this recipe", :action => 'create', :recipe_id => @recipe %> this link is on the user_recipes/index.html.erb and is calling

Rails3 - How to send Javascript Variable to a controller's action with the link_to helper?

那年仲夏 提交于 2019-12-18 06:51:36
问题 If i have the following javascript code var myVar = 0; function setNewValforVar(newValue){ myVar = newValue; } and i'm calling setNewValforVar function n times so when I click on a link it'd send myVar value to the controller action in a remote link like <%=link_to "My Link",{:action=>"myAction"},:data=>''sval='+myVar',:remote=>true%> I Rails 2.3.x I'd do this with <%=link_to_remote "My Link",:url=>{:action=>"myAction"},:with=>"'sval='+myVar"%> and i was getting this on the controller's

Rails 3: How to display a warning message when a link is clicked?

佐手、 提交于 2019-12-14 03:16:13
问题 I have the following link in my Rails 3 application: link_to("Invoice", "/jobs/#{job.id}/invoice") When user clicks it, the application shows an invoice. I would like to display a warning message when job.price is missing, asking the user whether to continue or not. Then, it should be redirected to the invoice page only if user chose "yes". What is the easiest way to implement this ? 回答1: This worked for me: link_to("Invoice", "/jobs/#{job.id}/invoice", :confirm => (job.price ? '' : "Job

How to implement this nested linked_to_function?

一笑奈何 提交于 2019-12-13 08:26:09
问题 In my view I want the user to be able to browse a catalog of products. The hierarchy is as following: Category Product AdditionalInfoForm In my main view I render a partial for the categories: #opContent = render :partial => 'op_main_categories' The op_main_categories partial is defined as following: - @controller.getPortalCategories.each do |c| = link_to_function "#{c.name}", :class => 'opCategoryHeaderText' do |page| - partial = escape_javascript(render :partial => 'op_secondary_categories'

Building a nested attribute from multiple parent objects

自古美人都是妖i 提交于 2019-12-13 05:42:06
问题 I'm currently trying to use link_to with a helper method that creates links for creating items dynamically, rather than using the new_object_path methods. Rails 3 allows for polymorphic URLs, which fl00r helped me identify and use in the form I was originally searching for. However, while using Rails' polymorphic_url method as shown here does pass the argument in the http request, the related form field on the returned page isn't filled in, and isn't getting set. I'm trying to build an order

Responding with js in Rails

拜拜、爱过 提交于 2019-12-13 04:24:35
问题 We have a link_to sending an ajax post, and are wondering if there's a way to change the content of the sending tag. We basically want to update the database, then change the icon in the link_to block. <%= link_to add_favorite_path({type: type, id: id}), type:"button", disable_with: '...', :method => :post, :remote => true, class: 'btn btn-default btn-sm', id: "#{type}-#{id}" do %> <i class="fa fa-plus"></i> <% end %> Here's the favorites contoller: class FavoritesController <

Rails link_to action with URL parameters

房东的猫 提交于 2019-12-13 03:56:30
问题 If I have a route like so get 'controller/:id/action/:param' => 'Controller#action' How can I use link_to to create a link like <a href="/controller/12345/action/abcde"> ? So far I have gotten link_to 'Link text', {:action => 'action'} To give me <a href="/controller/12345/action"> but using link_to 'Link text', {:action => 'action', :param => 'abcde'} will give me <a href="/controller/12345/action?param=abcde"> 回答1: You can name your route: get 'controller/:id/action/:param' => 'Controller

Rails 3 link_to rake task

空扰寡人 提交于 2019-12-13 03:12:17
问题 I've created a rake task in my application and now I want the task to be accesible for app users from a link on a menu, but I don't know how to invoke it from there. Something like this...? <%= link_to t('backup'), Rake::Task['backup'].invoke %> 回答1: You can't do it. Link_to can link to something static or controller action. So you need to create some action, where you can invoke your Rake Task. class MyTasksController < ApplicationController def rake_it Rake::Task['backup'].invoke end end <%

Toggling a boolean with link_to

喜你入骨 提交于 2019-12-13 01:24:29
问题 I've read through the relevant Stack questions but still seem to be hitting a routing error with the following code: Routes.rb resources :memberships do put :toggleon put :toggleoff end Memberships_controller.rb def toggleon @membership = Membership.find(params[:id]) @membership.update_attributes(:active => true) if user.id == membership_id redirect_to root_path else redirect_to group end end def toggleoff @membership = Membership.find(params[:id]) @membership.update_attributes(:active =>