Creating a new link_to for a nested resource - ruby-on-rails

I'm trying to use the link_to helper function to create a new order for a particular product. Here is my:
product model
class Product < ActiveRecord::Base
has_many :orders
end
routes.rb
resources :products, :only => [:show, :new, :create, :index, :update, :destroy] do
resources :orders, :only => [:create]
end
view for product/show.html.erb
<%= link_to 'New Order', new_product_orders_path(#product) %>
controller for orders
class OrdersController < ApplicationController
def create
#order = Order.new
end
end
relevant rake routes:
product_orders POST /products/:product_id/orders(.:format) orders#create
But when I do that I get undefined method `new_product_orders_path'
Whats the correct way to do this in Rails 4?

In your routes add new action here
resources :orders, :only => [:create, :new]
Also your controller is missing new action, in your create action you need to save your record
class OrdersController < ApplicationController
before_filter :set_product
def new
#order = #product.orders.new
end
def create
#order = #product.orders.new(params[:order])
#order.save
end
private
def set_product
#product = Product.where("id =?", params[:product_id]).first
end
end

I think you need
resources :products, :only => [:show, :new, :create, :index, :update, :destroy] do
resources :orders, :only => [:create, :new]
end
You can also check your routes by typing '/rails/info/routes' at the end of your server path.

Related

Rails 4: Public Activity Gem and Rails API

I am using the Public Activity Gem to track post for my app. Out of the box Public Activity gives me a ActivitiesController. I want to convert this controller to an API Controller so that I can make request to it from a mobile app. Below is the code I'm currently using. How can I rewrite the controller.
class ActivitiesController < ApplicationController
before_action :authenticate_user!
def index
#activities = PublicActivity::Activity.order("created_at desc")
end
end
class Api::V1::ActivitiesController < Api::BaseController
respond_to :json
def index
#activities = PublicActivity::Activity.order("created_at desc")
return render json: {message: "Activity Success"}
end
end
Rails.application.routes.draw do
namespace :api do
scope module: :v1 do
# devise_scope :api_v1_user do
resources :users, :only => [:show, :create, :update, :destroy]
# devise_for :users, :controllers => {passwords: 'api/v1/passwords'}
resources :activities, :only => [:index]
# end
end
end
Take a look here:
http://edgeguides.rubyonrails.org/api_app.html#creating-a-new-application
Focus on the "render json:" parts...

Rails error Couldn't find Blog with 'id'=new

I have a weird error when I want to redirect users to the root_url when they try to access blogs/new url in my app.
My routes are
resources :blogs, only: [:index, :show] do
resources :comments, only: [:create]
end
namespace :admin do
resources :blogs
resources :users, only: [:index, :show]
resources :comments, only: [:create, :new, :destroy]
end
My non-admin blogs controller looks like this:
class BlogsController < ApplicationController
before_action :set_blog, only: [:show]
def show
unless #blog
redirect_to blogs_path
flash[:notice] = "You are not authorized to create a post."
end
end
def index
#blogs = Blog.all
end
private
def set_blog
#blog = Blog.find(params[:id])
end
end
I get the error Couldn't find Blog with 'id'=new.
In rails, the priority of routes goes from top to bottom. Meaning, when you try to hit /blogs/new, the route gets matched with the show action of blogs defined in the top of your routes.rb.
blogs/new gets matched with /blogs/:id which is mapped to blogs#show action.
In the set_blog method, params[:id] is new and since there is no record with the id of new, you're getting that weird error.
How to get around this? Change the priority of your routes.
Move the following block below the admin namespaced routes.
namespace :admin do
resources :blogs
resources :users, only: [:index, :show]
resources :comments, only: [:create, :new, :destroy]
end
resources :blogs, only: [:index, :show] do
resources :comments, only: [:create]
end
By the way, your question says that you want to avoid non-admin users to access blogs#new. If that's the case, you should try to hit /admin/blogs/new and not /blogs/new.
If you had done that, you wouldn't have gotten the error in the first place. But still, its good to know about the priority of routes in rails.
Hope this helps!

I cant see my attachments from redmine plugin

Im haveing problems with seeing the attachments from plugin, it shows me the message : You are not authorized to access this page. I used the attachments helper to upload things in my plugin. Here is the code:
Init.rb(giving permissions)
project_module :plugin_name do
permission :view_attachments, :attachments => [:show, :download, :thumbnail, :upload]
The model class, in my case Unit.rb
class Unit < ActiveRecord::Base
belongs_to :project
acts_as_attachable :view_permission => :view_attachments
units_controller.rb
class UnitsController < ApplicationController
helper :attachments
include AttachmentsHelper
menu_item :overviews, :only => [:index, :show, :edit, :new, :update, :create]
before_filter(:find_project, :authorize, :only => [:index, :show, :edit, :new, :update, :create])
...
def create
#unit = Unit.new(unit_params)
if(#unit.save)
attachments = Attachment.attach_files(#unit, params[:attachments])
redirect_to(:action => "index")
flash[:notice] = l(:createMessage)
else
render("new")
end
end
units show.html.erb
<%= link_to_attachments #unit %>
P.S- when i remove this part of code in attachments_controller then i can see my attachments, see below:
before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
read_authorize action:
def read_authorize
#attachment.visible? ? true : deny_access
end
I think, you should not reinit permission for attachments.
Try to use special permission from Unit:
acts_as_attachable :view_permission => :view_unit

Nested routing and authorization using CanCanCan in Rails

There is the following routing:
resources :accounts, only: [:update] do
get 'search', on: :collection
resources :transactions, only: [:create]
end
Abilities:
can [:update, :search], Account
can [:create, :index], Transaction
Controller:
# Web API controller for actions on Transaction
class Api::V1::Web::TransactionsController < Api::V1::Web::ApplicationController
load_and_authorize_resource :account
load_and_authorize_resource :transaction, through: :account
def create
render json: params and return
end
end
When I try to create a new transaction I get an error:
CanCan::AccessDenied
in Api::V1::Web::TransactionsController#create
What am I doing wrong? How can I fix it? Thanks in advance.

[Rails]Routes doesn't work with some nouns

(I'm french)
I've a problem with my Rails (3.2.11) application. When i set my routes with some nouns, it doesn't work !
I explain:
Way::Application.routes.draw do
scope(:path_names => { :new => "nouveau", :edit => "edition" }) do
scope "/admin" do
resources :news, except: [:show, :index], path: '/articles'
resources :users, except: [:show]
resources :pages, except: [:show]
resources :events, except: [:show, :index]
resources :projects, except: [:show,:index], path: '/projets'
resources :galleries, except: [:index, :show] do
resources :paintings, except: [:index, :show]
end
end
end
end
The resources :projects doesn't work when i set it to "/projets".
What doesn't work is: when i want to create a new project in my form, i click on submit, it simply redirect me to "/projets", without doing something !
But when i set the route to "/poneys" for example, it works ! I really don't understand.
Thanks for your help.
https://github.com/khcr/way
class ProjectsController < ApplicationController
def new
#project = Project.new
render layout: 'admin'
end
def create
#project = Project.new(params[:project])
if #project.save
flash[:success] = "Projet créé"
redirect_to project_path(#project)
else
render 'new', layout: 'admin'
end
end
end

Resources