Rendering Rendering Action in different controllers

So, I have a controller called "Music" with one action, which is an index. On the page music/index.htmlI list the group of songs (through the song model), and below I have the opportunity to create a new song.

The song has checks that I tested and work perfectly. When a new song saves the "Songs" action in the controller, create me redirect_to => 'music_index_path'so that the person can see the new song in the list. However, when the Song does not save (does not pass validation), I cannot use it redirect_to, since the error_messages form is not portable. I need to use render, but can not say render :controller => 'music', :action => 'index.

My goal is to show error messages for the song form on the music / index.html page.

How can I do it? I am open to other ideas (e.g. changing controllers).

Thank!

+3
source share
7 answers

It seems to me that Music should be part of the Song, or vice versa. You can always use routes to disguise them as one or the other for the user. for me, the song / index sounds as if it should display all the songs that are all the same anyway.

+2
source

, . , RESTful, , ... ? , Song # create? , , , , ?:)

, . (, , .)

<hack> -, / /_form, music/index, render: partial = > songs/_form, render: action = > '../music/index '( -.) , # @song = Song.new </hack>

+1

, Flash ​​... .

, , , . , , .

def index
  setup_for_index
end

def create
   @song = Song.new(params[:song])
   @song.save
   #...
   #on failure
     setup_for_index
     render :controller => "music", :action => "index"
end

def setup_for_index
  @songs = Song.all
  #etc
end

, , form_remote_for , . RJS .

+1

, , , , , :template:

render template: 'music/index'
+1

, request.post? , ?

0

You can also try using flash[:song_save_error]to transfer error conditions back to your music controller.

http://api.rubyonrails.org/classes/ActionController/Flash.html

0
source

You can try render :file => ...

0
source

Source: https://habr.com/ru/post/1726382/


All Articles