actioncontroller

ActionController::UrlGenerationError in Valuations#new

℡╲_俬逩灬. 提交于 2019-12-06 05:58:29
I've read other SO articles relating to UrlGenerationError 's which seem to point to singularization or plurization of a word, but I don't think that's the issue here. It works when I remove from valuations/_form.html.erb: <%= render "comments/comments" %> <%= render "comments/form" %> Submit the _form with :name & :tag_list , readd <%= render "comments/comments" %> <%= render "comments/form" %> and then refresh. What's the deal when nil? routes resources :valuations do resources :comments end comments_controller class CommentsController < ApplicationController before_action :load_commentable

How to render js template from module included in controller?

笑着哭i 提交于 2019-12-05 23:11:33
I have an action in a controller concern, which gets included in a controller. This action does not render a js.erb file as specified under a respond_to block. How do I properly get an action in a controller concern to successfully render a js.erb file (or any view, for that matter)? Is it a problem with my routes? The link for the module action = link_to image_tag("upvote.png"), send("vote_socionics_#{votable_name}_path", votable, vote_type: "#{s.type_two_im_raw}"), id: "vote-#{s.type_two_im_raw}", method: :post, remote: true ** The link for the controller action** = link_to "whatever",

How to use Rails Action Controller Nested Params to Permit a Specific Attributes Hash

折月煮酒 提交于 2019-12-05 17:48:40
There are a lot of questions about Nested Parameters, but I can't seem to find one that addresses my specific, simple situation. I'm trying to permit a nested hash that is NOT an array. I expected this to work: params.require(:book).permit(:title, :description, style: {:font, :color}) But it resulted in a syntax error. This, however, worked: params.require(:book).permit(:title, :description, style: [:font, :color]) But my issue with this, it it seems to permit style values that are arrays of items with attributes :font and :color. I only want to permit a single hash with those 2 attributes. I

Rails 3: Get current namespace?

天涯浪子 提交于 2019-12-05 08:40:48
问题 using a method :layout_for_namespace I set my app's layout depending on whether I am in frontend or backend, as the backend is using an namespace "admin". I could not find a pretty way to find out which namespace I am, the only way I found is by parsing the string from params[:controller]. Of course that's easy, seems to be fail-safe and working good. But I am just wondering if there's a better, prepared, way to do this. Does anyone know? Currently I am just using the following method: def is

Transaction Action with Ruby On Rails

送分小仙女□ 提交于 2019-12-05 08:24:57
I have a complex action inside controller that performs several update queries to the database. How can I make this action acts like transaction without any structural refactoring? MyModel.transaction do begin @model.update_stuff @sub_model.update_stuff @sub_sub_model.update_stuff rescue ActiveRecord::StatementInvalid # or whatever # rollback is automatic, but if you want to do something additional, # add it here end end Here are the docs for the transaction method . It's posible to make all actions in controller transactional at once with: around_filter :transactional def transactional

How to get ActionController::Live streaming working with Thin?

半腔热情 提交于 2019-12-05 06:09:40
Question Can you use thin with ActionController::Live to implement Server Side Events (SSE) and long polling? If so, how? Context Although the title is a repeat of How to get Rails 4 ActionController::Live streaming working with Thin and Ruby 2? And how do Thin and Puma scale with live streaming? , the OP muddied the waters by asking two questions, and this question never got answered. A number of other posts suggest you CAN use thin for Server Side Events (sse) if you start it via exec thin start --threaded : Does Heroku support ActionController::Live? and Is puma the ONLY multi-threaded

how to execute an action if the before_action returns false

三世轮回 提交于 2019-12-04 23:57:38
I know that with the following code: before_action :signed_in?, only: [:new] the action new will be executed if the signed_in? returns true, but instead if I want the new action to be executed when signed_in? returns false what do I have to do? Do I have to create a new method called, for instance, not_signed_in? ? Here it is my signed_in? method def signed_in? !@current_user.nil? end before_action doesn't work as you think - it doesn't prevent action to be executed if callback returns false . I would solve your problem in a little bit different manner, for example: before_action :redirect_to

Ruby on Rails 301 redirection

女生的网名这么多〃 提交于 2019-12-04 21:11:56
问题 I added slugs to some of the models, but because of SEO I need to do 301 redirection from old links: old: http://host.com/foo/1 new: http://host.com/foo/foo_slug question: how to implement 301 redirection in this case? and is it possible to implement 301 redirection from uppercased link? Like this: http://host.com/foo/FOO_SLUG -> http://host.com/foo/foo_slug 回答1: You should be able to redirect with status 301 by adding this to your controller action: redirect_to "http://host.com/foo/foo_slug"

Getting access to :not_found, :internal_server_error etc. in Rails 3

那年仲夏 提交于 2019-12-04 15:17:48
It looks like ActionController::StatusCodes has been removed from Rails 3. I used synonyms for HTTP status codes such as 200 => :ok 404 => :not_found 500 => :internal_server_error For more codes, see here: http://apidock.com/rails/ActionController/Base/render#254-List-of-status-codes-and-their-symbols Where can I find these in Rails 3? It seems that the error codes reside in action_dispatch/middleware/show_exceptions.rb where the symbols are mapped to actual exceptions: 'ActionController::RoutingError' => :not_found, 'AbstractController::ActionNotFound' => :not_found, 'ActiveRecord:

Rails 3: Get current namespace?

删除回忆录丶 提交于 2019-12-03 22:06:31
using a method :layout_for_namespace I set my app's layout depending on whether I am in frontend or backend, as the backend is using an namespace "admin". I could not find a pretty way to find out which namespace I am, the only way I found is by parsing the string from params[:controller]. Of course that's easy, seems to be fail-safe and working good. But I am just wondering if there's a better, prepared, way to do this. Does anyone know? Currently I am just using the following method: def is_backend_namespace? params[:controller].index("admin/") == 0 end Thanks in advance Arne You can use: